Huffman Coding
calculator.
Build the Huffman code for a text: short codes for common letters, long codes for rare ones.
- Average (bits)
- 154
- Entropy (bits)
- 3.714
- Efficiency
- 99%
- Fixed-length
- 144 bits
The 36 symbols take 135 bits, 53% fewer than at 8 bits each and 6% fewer than a fixed 4-bit code. The most common symbol, space, gets a 3-bit code. No code can average fewer bits than the entropy, 3.714 bits per symbol. The code table is not counted, and a real file has to store it.
The tree, merge by merge
Slide or use the arrow keysMerge 15 of 15: join the two lightest, a node of 16 and a node of 20, into a node of 36. That node is the root; read each code from it, 0 for left and 1 for right.
Working
- 1Count each symbolspace 7, a 4, e 4, f 3, h 2, i 2, m 2, n 2, … (16 symbols)
- 2Join the two lightest nodes again and again, until one tree is left1 + 1 = 2 (l, o)1 + 1 = 2 (p, r)1 + 1 = 2 (u, x)2 + 2 = 4 (h, i)⋮16 + 20 = 36
- 3Read each code from the root: 0 for a left branch, 1 for a right branchspace = 111, a = 000, e = 001, f = 1101, h = 0100, i = 0101, …
- 4Add up count × code length7 × 3 + 4 × 3 + 4 × 3 + 3 × 4 + 2 × 4 + … = 135 bits
- 5Check: decode the bits with the code table; it gives back the text135 bits → this is an example of a …
Every symbol's code
| Symbol | Count | Code | Bits |
|---|---|---|---|
| space | 7Bits 21 | 111 | 21 |
| a | 4Bits 12 | 000 | 12 |
| e | 4Bits 12 | 001 | 12 |
| f | 3Bits 12 | 1101 | 12 |
| h | 2Bits 8 | 0100 | 8 |
| i | 2Bits 8 | 0101 | 8 |
| m | 2Bits 8 | 0110 | 8 |
| n | 2Bits 8 | 0111 | 8 |
| s | 2Bits 8 | 1000 | 8 |
| t | 2Bits 8 | 1001 | 8 |
Decode bits with this code
The whole text encodes to: 100101000101100011101011000111000011111100111001000011010110101000011111010111011110001110100110001101110101100000111111100110111001001
How it's worked out
Count how often each symbol appears. Make one leaf per symbol, weighted by its count. Then repeat one step until a single tree is left: take the two lightest nodes and join them under a new node whose weight is their sum.
Read each code from the root down: a left branch adds a 0 and a right branch adds a 1. RFC 1951, the DEFLATE format behind ZIP and gzip, labels the branches the same way. Symbols sit only at the leaves, so no code is the start of another, and a decoder can split the bits without any gaps between codes.
The average code length is the total bits over the number of symbols, where is how often symbol appears and is the length of its code:
Shannon (1948) showed that no code can average fewer bits per symbol than the entropy, where is the share of the text that is symbol :
The tool shows both, and their ratio as the efficiency. When counts tie, the tool joins the node made first, so the same text always gives the same tree.
Worked examples
Priya codes the classic example sentence
The sentence "this is an example of a huffman tree" has 36 characters and 16 different symbols. The space is the most common, with 7, so it gets a 3-bit code, 111. Rare letters such as x get 5 bits.
The whole sentence takes 135 bits, against 288 as 8-bit characters, which is 53% smaller. A fixed-length code with 4 bits per symbol would need 144.
The average is bits per symbol, or 3.75. The entropy is 3.71 bits, so the code is 99% efficient.
Tom builds the tree for aaaabbc by hand
The counts are a 4, b 2 and c 1. The two lightest are c and b, so they join first: 1 + 2 = 3. That node joins a: 3 + 4 = 7, the root.
Reading down gives a = 1, b = 01, c = 00. The message takes 10 bits instead of 56 at 8 bits each, or 14 with a fixed 2-bit code, a saving of 29% on the fixed code.
Shannon's four-letter source hits the limit exactly
Shannon's 1948 paper uses a source where A, B, C and D appear half, a quarter, an eighth and an eighth of the time. AAAABBCD has those shares. Huffman gives A = 0, B = 10, C = 110, D = 111, the same code Shannon prints.
The average is bits per symbol, and the entropy is also 1.75. Every share is a power of one half, so the code meets the limit with no waste, an efficiency of 100%.
Reading your result
Efficiency. This is the entropy over the average code length. It reaches 100% only when every symbol's share is a power of one half. Roweis's lecture notes note a Huffman code can be as much as one bit per symbol worse than the entropy. Coding blocks of symbols together closes that gap.
Kraft sum. Add 1 over 2 to the power of each code length. For a full Huffman tree it is exactly 1, which shows no shorter code was left unused. For the classic sentence it is 1.
When it saves little. Eight different letters used once each, abcdefgh, give 24 bits, the same as a fixed 3-bit code. Huffman helps only when some symbols are much more common than others. Mississippi, with 4 symbols, takes 21 bits against 22 for a fixed code.
Common mistakes
- Forgetting the code table. A file must also store the code. For short text that can cost more than it saves: hello takes 10 bits of data, but the table for its 4 symbols has to be sent too.
- Expecting one right answer. When counts tie, different joins give different codes. Every valid Huffman code for a text has the same total length, so check the total, not the codes.
- Skipping spaces and line breaks. They are symbols too, and the space is often the most common one.
- Comparing with 8 bits only. A fixed-length code with just enough bits for the distinct symbols is the fairer comparison, and the savings over it are smaller.