Huffman Coding Visualizer

Build Huffman trees, encode text, and visualize compression

Input Text

About Huffman Coding

Huffman Coding is a lossless data compression algorithm that assigns variable-length codes to characters based on their frequencies.

Algorithm Steps:

  1. Calculate Frequency: Count the occurrence of each character in the input text
  2. Create Leaf Nodes: Create a leaf node for each character with its frequency
  3. Build Priority Queue: Insert all nodes into a min-heap ordered by frequency
  4. Build Tree: Repeatedly extract two nodes with minimum frequency, create a parent node with combined frequency, and insert back into the queue
  5. Generate Codes: Traverse the tree from root to leaves, assigning 0 for left edges and 1 for right edges
  6. Encode: Replace each character in the original text with its Huffman code

Key Properties:

  • Prefix-free: No code is a prefix of another code
  • Optimal: Produces the shortest average code length among all prefix codes
  • Variable-length: More frequent characters get shorter codes
  • Lossless: Original data can be perfectly reconstructed