Skip to content

About

Huffman coding compression & decompression in Python — with an interactive tree visualizer, step-by-step construction replay, and a dark-themed Tkinter GUI.

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Huffman Compression Studio

A complete Huffman coding implementation in Python with a full graphical interface.

Features

  • 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 .huff files and verify round-trip integrity
  • Supports any character set (not limited to 6 characters)

Project Structure

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)

Requirements

  • Python 3.10+
  • tkinter (included with standard Python on Windows/macOS/Linux)

No third-party packages needed.

Usage

Graphical Interface (default)

python main.py
# or
python main.py --gui

Command-Line Interface

python main.py texte.txt

Example 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

GUI Tabs

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

Compression Format

Compressed files are saved as .huff (JSON):

{
  "codes": { "a": "01", "b": "10", ... },
  "bits": "0101011110..."
}

Algorithm

  1. Count character frequencies
  2. Build a min-heap of leaf nodes
  3. Repeatedly merge the two lowest-weight nodes → build the tree bottom-up
  4. Assign codes: left edge = 1, right edge = 0
  5. Encode the text using the generated code table
  6. Decode by traversing the tree bit by bit

About

Huffman coding compression & decompression in Python — with an interactive tree visualizer, step-by-step construction replay, and a dark-themed Tkinter GUI.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages