Skip to content

Feature/huffman - #375

Open
IncludeLuisFerreira wants to merge 2 commits into
kelvins:mainfrom
IncludeLuisFerreira:feature/Huffman
Open

IncludeLuisFerreira wants to merge 2 commits into
kelvins:mainfrom
IncludeLuisFerreira:feature/Huffman

Conversation

@IncludeLuisFerreira

Copy link
Copy Markdown
Contributor

Description

Adds a Huffman compression/decompression implementation in
src/c/HuffmanAlgorithm.c.

Features

  • Reads an input text file and builds a frequency table for all characters.
  • Builds the Huffman tree using a sorted list of nodes (lowest frequency
    first) and computes the encoding dictionary (0 = left, 1 = right).
  • Encodes the text and packs the bits into a single binary output file
    (compactado.huf) that stores:
    • the Huffman tree serialized in pre-order in the file header, so it can be
      rebuilt during decompression;
    • the bit-packed compressed data;
    • a trailing byte with the number of valid bits in the last byte.
  • Decompresses by rebuilding the tree from the header and traversing it bit by
    bit, printing the original characters.
  • Handles edge cases: empty files, single-character files (leaf root) and
    binary bytes (e.g. 0xFF).

How to test

  1. Compile: gcc -Wall -Wextra -Werror -DLINUX -o huffman src/c/HuffmanAlgorithm.c
  2. Create a Texto.txt file with some content.
  3. Run ./huffman and choose option 1 to compress → produces compactado.huf.
  4. Run ./huffman and choose option 2 to decompress → prints the original text.

Verified with byte-for-byte round-trip tests (plain text, multi-line, UTF-8,
binary bytes, single-character files) and no memory leaks under Valgrind.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant