A complete Huffman coding implementation in Python with a full graphical interface.
- Compress any text file using Huffman coding
- Interactive tree visualisation — pan, zoom, edge labels (1=left, 0=right)
- Step-by-step replay of every sort & merge during tree construction
- Decompress
.hufffiles and verify round-trip integrity - Supports any character set (not limited to 6 characters)
Huffman/
├── huffman.py # Pure engine — tree, codes, compress, decompress, file I/O
├── gui.py # Tkinter GUI (4 tabs)
├── main.py # Entry point (CLI + GUI)
└── texte.txt # Sample text (30 lines × 80 chars, characters a–f)
- Python 3.10+
tkinter(included with standard Python on Windows/macOS/Linux)
No third-party packages needed.
python main.py
# or
python main.py --guipython main.py texte.txtExample output:
==================================================
HUFFMAN CODING TABLE (left=1, right=0)
==================================================
Char Count Code Len
──────────────────────────────────────────────
\n 29 00011 5
a 687 01 2
b 483 10 2
c 384 001 3
d 441 11 2
e 227 0000 4
f 178 00010 5
==================================================
COMPRESSION RESULTS
==================================================
Original size : 19,432 bits
Compressed size : 6,317 bits
Bits saved : 13,115 bits
Compression ratio : 67.49 %
Avg code length : 2.6007 bits/char
Round-trip check : PASS OK
| Tab | Description |
|---|---|
| Compress | Load any .txt file → frequencies, coding table, bit-string preview, stats |
| Tree View | Interactive Huffman tree canvas; zoom & drag to explore |
| Steps | Replay every merge step of the tree construction |
| Decompress | Load a .huff file → recover original text → save |
Compressed files are saved as .huff (JSON):
{
"codes": { "a": "01", "b": "10", ... },
"bits": "0101011110..."
}- Count character frequencies
- Build a min-heap of leaf nodes
- Repeatedly merge the two lowest-weight nodes → build the tree bottom-up
- Assign codes: left edge = 1, right edge = 0
- Encode the text using the generated code table
- Decode by traversing the tree bit by bit