Huffman Trees
Short codes for frequent lettersNormal text uses 8 bits for every letter. Huffman coding counts how often each letter appears and builds a tree so that frequent letters get short codes and rare letters get long ones. Build the tree, then encode a text into bits and decode the bits back.
- Greedyalways join the two smallest counts
- Coderoot → leaf path: left 0, right 1
- Prefix-freeno code starts another code
- 5how often the letter appears
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
Every operation, in plain steps
These are exactly the steps the visualizer animates.
What does a Huffman node store?
A leaf stores a letter and how many times it appears. An inner node stores only a count — the sum of its children. The tree is FULL: every inner node has exactly two children. Letters live only in the leaves, which is why no code can be the beginning of another code.
struct Node {
char ch; // the letter (leaves only)
int freq; // how often it appears
struct Node *left; // edge labelled 0
struct Node *right; // edge labelled 1
};
// ABRACADABRA: A 5, B 2, R 2, C 1, D 1
// codes: A = 0 B = 110 R = 111
// C = 100 D = 101 → 23 bits, not 88
1. Count
- Count how often each letter appears.
- Every letter becomes a one-node tree.
- Put all trees in a priority queue…
- …with the smallest count first.
2. Merge
- Take out the two smallest trees.
- Make a new node: count = their sum.
- First one = left child, second = right child.
- Put it back; repeat until one tree is left.
3. Read the codes
- Walk from the root to every leaf.
- Going left adds a 0, going right adds a 1.
- Frequent letters sit high → short codes.
- Rare letters sit deep → long codes.
4. Encode
- Replace every letter by its code.
- Write the codes one after another.
- No separators are needed.
- Total bits = Σ count × code length.
5. Decode
- Start at the root.
- Bit 0 → go left, bit 1 → go right.
- At a leaf, write its letter…
- …and jump back to the root.
Why it works
- Letters are only in leaves → prefix-free codes.
- Joining the two smallest is a greedy choice…
- …that is proven to give the fewest total bits.
- Used inside ZIP, JPEG, PNG and MP3.
Pseudocode
Language-independent steps. The visualizer shows the same logic in C, Python and Java.
How fast is each operation?
k = number of different letters, n = length of the text (or of the bits). With a min-heap each of the k − 1 merges costs O(log k).
| Operation | Best | Average | Worst | Speed |
|---|---|---|---|---|
| Count letters | O(n) |
O(n) |
O(n) |
Grows with n |
| Build the tree (heap) | O(k log k) |
O(k log k) |
O(k log k) |
Grows with n |
| Encode | O(n) |
O(n) |
O(n) |
Grows with n |
| Decode (per bit) | O(1) |
O(1) |
O(1) |
Instant |
ABRACADABRA: fixed-length vs Huffman codes
| Letter | Count | ASCII code | Huffman code | Bits (Huffman) |
|---|---|---|---|---|
| A | 5 | 01000001 | 0 | 5 × 1 = 5 |
| B | 2 | 01000010 | 110 | 2 × 3 = 6 |
| R | 2 | 01010010 | 111 | 2 × 3 = 6 |
| C | 1 | 01000011 | 100 | 1 × 3 = 3 |
| D | 1 | 01000100 | 101 | 1 × 3 = 3 |
| Total | 11 | 88 bits | — | 23 bits (74% smaller) |
Even a 3-bit fixed code (enough for 5 letters) would need 33 bits.
Why prefix-free matters
| Codes | Bits 0110 | Can it be decoded? |
|---|---|---|
| A = 0, B = 01, C = 10 (not prefix-free) | A C A? or B C? … | No — 0 is the start of 01 |
| A = 0, B = 10, C = 11 (Huffman) | A · C · A | Yes — exactly one way |
Check your understanding
Answer each question — you will see the explanation straight away.
Which two trees does Huffman's algorithm join at every step?
It always joins the two trees with the smallest counts — a greedy choice that gives the shortest total encoding.
A letter appears very often. Where will it be in the Huffman tree?
Frequent letters are joined last, so they stay close to the root and get short codes.
Codes are A = 0, B = 10, C = 11. What does 11010 decode to?
11 → C, 0 → A, 10 → B. The text is CAB.
Why can Huffman bits be decoded without separators between letters?
Letters are only at leaves, so reaching a leaf always means the code is complete.
The text has 6 different letters. How many merges does building the tree need?
Every merge turns two trees into one, so going from 6 trees to 1 takes 6 − 1 = 5 merges.
Counts: A 5, B 2, R 2, C 1, D 1. How many bits does A need?
A is joined last with the tree of all the others, so it is a child of the root: code 0, just 1 bit.
Trees quiz
Test yourself: 5 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.