Huffman Encoding Visualizer
3px
Greedy Algorithms Compression Binary Tree

Huffman Encoding Visualizer

What is Huffman Encoding

  • Goal: Minimize total encoded length by assigning shorter codes to frequent characters.
  • Data Structure: Use a min-heap to repeatedly pick the two smallest frequencies.
  • Tree Rule: Left edge adds bit 0, right edge adds bit 1.
  • Property: Huffman codes are prefix-free, so decoding is unambiguous.
  • Optimality: Greedy merge of least frequent nodes gives optimal weighted path length.

The problem

Given arrays chars and freq, where freq[i] is the frequency of chars[i], construct Huffman codes and return the code for each character.

Required Output: Prefix-free binary strings where higher-frequency characters generally get shorter codes.

Example 1:

Input: chars = [a,b,c,d,e,f], freq = [5,9,12,13,16,45]
Output (one valid set): f:0, c:100, d:101, a:1100, b:1101, e:111
Explanation: More frequent f gets shortest code.

Example 2:

Input: chars = [A,B,C,D], freq = [10,15,30,16]
Output (one valid set): C:0, D:10, A:110, B:111
Explanation: Codes may differ by tie-breaking but weighted cost stays minimal.

Constraints:

  • 1 <= chars.length <= 10^5
  • 1 <= freq[i] <= 10^9
  • All characters are distinct.

greedy_strategy.txt

1// Huffman Greedy Strategy
21. Build min-heap of leaf nodes (char, freq)
32. Pop two minimum-frequency nodes
43. Merge into internal node with sum frequency
54. Push merged node back to heap
65. Repeat until heap size becomes 1 (root)
76. DFS root to assign 0/1 codes

ratio_anatomy.svg

Heap Priority: Always merge the two smallest frequencies first.

Encoding: Left child adds bit 0, right child adds bit 1.

Compression Benefit: Frequent characters stay near root, reducing total bits.

huffman_tree.svg - Huffman Construction Visualization

Merged in current step
Picked from heap
Remaining nodes
Ready to build

Selection State

heap_state.log

Heap not initialized.

codes.log

Codes will appear after complete tree build.

Step Info

Waiting to build...

Algorithm & Pseudocode

huffman_encoding.txt

1function buildHuffman(chars, freq):
2heap = minHeap of leaf nodes
3while heap.size > 1:
4left = heap.popMin()
5right = heap.popMin()
6parent = Node('$', left.freq + right.freq)
7parent.left = left, parent.right = right
8heap.push(parent)
9root = heap.popMin()
10dfs(root, "") to generate codes

complexity.md

AspectComplexity
Build Heap + MergesO(n log n)
DFS Code GenerationO(n)
SpaceO(n)

Implementation

huffman_encoding.c

1// Min-heap based Huffman in C (outline)
2struct Node { char ch; int freq; struct Node *left, *right; };
3while (heapSize > 1) {
4left = extractMin(heap); right = extractMin(heap);
5parent = newNode('$', left->freq + right->freq);
6parent->left = left; parent->right = right;
7insertMin(heap, parent);
8}
9printCodes(root, arr, top);

Explanation

Heap Operations: extractMin and insertMin maintain smallest frequencies at top.

Merge Step: Internal parent node frequency is sum of two children.

Code Generation: DFS accumulates 0/1 path bits from root to leaves.