This project implements the Huffman Coding algorithm in C++ for file compression and decompression.
Huffman Coding is a popular lossless method that assigns variable-length binary codes based on character frequency, minimizing file sizes.
| File/Folder | Purpose |
|---|---|
huffman.hpp |
Huffman tree and compressor class definitions |
huffman.cpp |
Huffman class member functions implementation |
encode.cpp |
Main program for compression (encoding) |
decode.cpp |
Main program for decompression (decoding) |
output.txt |
(Example) Output file from runs |
- C++17 compiler (g++)
- Works on Linux, Windows, Mac
Only compile one main file at a time (each has its own main()).
g++ -std=c++17 encode.cpp huffman.cpp -o huffman_encode
g++ -std=c++17 decode.cpp huffman.cpp -o huffman_decode
./huffman_encode input.txt output.huff
input.txt: file to compressoutput.huff: compressed output
./huffman_decode output.huff recovered.txt
output.huff: compressed (encoded) filerecovered.txt: decoded/restored file
- Frequency Table: Reads the file, counts ASCII 0–127 occurrences.
- Heap/Min-Tree: Builds a min-heap for fast huffman tree construction.
- Huffman Tree: Combines least-frequent nodes into a binary tree.
- Code Generation: Assigns binary codes (left=
0, right=1) to each character. - Encoding: Converts input to bit strings and writes as bytes with meta-data/header.
- Decoding: Reconstructs tree from header, traverses codes to restore the original data.
- 1st Byte: Number of unique characters in tree.
- Next N bytes: Each node's char + code (with separator and padding).
- Main segment: Encoded data in bytes.
- Last Byte: Padding meta (number of bits padded).
- GeeksforGeeks: Huffman Coding in C++
- Programiz: Huffman Coding Algorithm
- W3Schools DSA: Huffman Coding