2. Huffman Coding
Use fewer bits for common symbols
In standard ASCII, every character uses exactly 8 bits. The letter 'e' — the most common letter in English — uses the same 8 bits as 'z', which barely appears at all. That feels wasteful. What if we could use fewer bits for common characters and more bits for rare ones? On average, we'd use fewer bits per character.
That's exactly what Huffman coding does. Invented by David Huffman in 1952 as a graduate student's term paper, it's one of the most important algorithms in computer science.
A Motivating Example
The string: ABRACADABRA
11 characters × 8 bits = 88 bits in ASCII
Character frequencies
| Character | Count | Frequency |
|---|---|---|
| A | 5 | 45.5% |
| B | 2 | 18.2% |
| R | 2 | 18.2% |
| C | 1 | 9.1% |
| D | 1 | 9.1% |
A appears almost half the time. If we could encode A with just 1 bit instead of 8, that alone would save a lot. But we need to be careful about how we assign the codes...
The Prefix-Free Property
The Ambiguity Problem
Suppose we assign: A = 0, B = 01. Now when the decoder
sees 01, is that "AB" (0 then 1...) or "B" (01)? We can't tell!
The solution: a prefix-free code — no code is the beginning
(prefix) of any other code. If A = 0, then no other code can start
with 0. This guarantees unambiguous decoding without needing
separators between codes.
Huffman's algorithm constructs an optimal prefix-free code by building a binary tree. The tree structure naturally enforces the prefix-free property: every symbol is a leaf, so no symbol's path is a prefix of another's.
Building the Huffman Tree
Here's the algorithm applied to ABRACADABRA:
-
Count frequencies and create leaf nodes
One node per symbol, labeled with its frequency:
[A:5] [B:2] [R:2] [C:1] [D:1] -
Combine the two lowest-frequency nodes
C (1) and D (1) are tied for lowest. Combine them into a new internal node with frequency 2.
[A:5] [B:2] [R:2] [CD:2] / \ [C:1] [D:1] -
Repeat: combine the two lowest
B (2), R (2), and CD (2) are tied. Pick B and R. Combine into BR with frequency 4.
[A:5] [BR:4] [CD:2] / \ [B:2] [R:2] -
Repeat again
CD (2) and BR (4) combine into BRCD with frequency 6.
[A:5] [BRCD:6] / \ [BR:4] [CD:2] / \ / \ [B:2][R:2][C:1][D:1] -
Final merge — the root
A (5) and BRCD (6) combine into the root with frequency 11.
[Root:11] / \ [A:5] [BRCD:6] / \ [BR:4] [CD:2] / \ / \ [B:2][R:2][C:1][D:1] -
Assign codes: left = 0, right = 1
Read the path from root to each leaf:
Symbol Path Code Bits A left 01 B right → left → left 1003 R right → left → right 1013 C right → right → left 1103 D right → right → right 1113
Encoding the Message
Encoded bitstream
0 100 101 0 110 0 111 0 100 101 0
Total: 5×1 + 2×3 + 2×3 + 1×3 + 1×3 = 23 bits
ASCII
88 bits (8 bits × 11 chars)
Huffman
23 bits — 74% smaller
Why Huffman Is Optimal (and Where It Isn't)
Huffman coding is provably optimal among prefix-free codes. No other prefix-free code can encode the same data in fewer bits.
Shannon Entropy — The Theoretical Limit
Information theory tells us the minimum average bits per symbol is:
H = -Σ p(x) × log₂(p(x))
For ABRACADABRA: H ≈ 2.04 bits/symbol. Our Huffman code averages 23/11 ≈ 2.09 bits/symbol — very close!
But there's a limitation: Huffman assigns an integer number of bits per symbol. Arithmetic coding (page 7) overcomes this by encoding the entire message as one number, achieving fractional bits per symbol.
Where Huffman Coding Is Used
| Format / System | Role of Huffman |
|---|---|
| DEFLATE / gzip / zip | Encodes the LZ77 output (literals, lengths, distances) |
| JPEG | Encodes the quantized DCT coefficients |
| MP3 | Encodes the quantized frequency data |
| PNG | Via DEFLATE (which uses Huffman internally) |
| HTTP/2 (HPACK) | Huffman-encodes HTTP header strings |
Key Takeaways
- Huffman coding assigns shorter bit-codes to more frequent symbols and longer codes to rare ones
- The prefix-free property ensures unambiguous decoding — no code is a prefix of another
- The tree is built bottom-up by repeatedly merging the two lowest-frequency nodes
- It's provably optimal among prefix-free codes
- Limitation: it can only assign whole-number bit lengths — arithmetic coding overcomes this
- Used in JPEG, MP3, gzip, zip, PNG, and countless other formats
Try It Yourself — Interactive Huffman Encoder
Type any string below to see the full Huffman encoding process: frequency table, tree construction steps, visual tree, code assignments, encoded bitstream, and compression statistics.
Character Frequencies
Tree Construction Steps
Huffman Tree
Code Assignments
Encoded Bitstream
Compression Statistics
Huffman coding exploits the fact that some symbols are more common than others. But there's a completely different kind of redundancy in data: repeated sequences. The string "the" appears thousands of times in a book — what if we could say "same as before" instead of encoding it fresh each time? That's LZ77.