Watch each symbol turn into bits, decode the stream and compare the Huffman payload with fixed-width or UTF-8 text. Try BANANA, a longer English or Chinese passage, or your own text.
Interactive Huffman coding
Huffman tree
0left1right- B
- A
- N
- A
- N
- A
10Symbols encoded 1 / 6
For B, follow the highlighted path and append 10 to the stream.
Full encoded stream9 bits
10 0 11 0 11 0
Build a Huffman tree with BANANA
The page opens on Encode text, paused just after the first B in BANANA. Its highlighted branches have written 10 to the stream. Choose Tree construction to see where that code comes from. The six letters include three A’s, two N’s and one B. Each leaf shows the symbol and its frequency.
Why merge B and N first?
- The priority queue starts with B (1), N (2), A (3). The two amber cards mark the smallest weights. The dashed node previews their sum. Click Next step to join B and N under a new parent of weight 1 + 2 = 3.
- Advance again. The queue now contains A (3) and the new subtree (3). Both take part in the next round. The last merge creates a root of weight 6, the length of the input.
Moving two subtrees under a parent adds one bit to every occurrence below them. In an optimal binary prefix tree, two least-frequent symbols can occupy a deepest pair of sibling leaves. Replacing that pair with a symbol of their combined weight leaves a smaller problem of the same kind. This is the greedy choice that Huffman’s algorithm repeats.
Read the codewords and check the nine-bit total
In Encode text, use the slider or Next step to stop on the first B. Its path goes right for 1, then left for 0, giving 10. Advance once. The next letter, A, takes a single left branch, so its 0 is appended to the stream. Written bits now reads 100. Go back one step to remove just that last bit.
| Symbol | Count | Codeword | Payload bits |
|---|---|---|---|
| B | 1 | 10 |
1 × 2 = 2 |
| A | 3 | 0 |
3 × 1 = 3 |
| N | 2 | 11 |
2 × 2 = 4 |
In the original order the codewords are 10 0 11 0 11 0, giving 9 bits. Open Full encoded stream to see the codeword boundaries. The bit stream itself is 100110110.
How is the compression ratio calculated?
Whole-text compression compares the complete payload with a baseline you select. Divide the baseline size by the Huffman size to get the compression ratio. Divide the bits saved by the baseline size to get the percentage saved.
| Baseline for BANANA | Baseline size | Huffman size | Ratio | Space saved |
|---|---|---|---|---|
| Fixed width for three symbols | 6 symbols × 2 bits = 12 bits | 9 bits | 1.33:1 | 25% |
| UTF-8 text | 6 bytes × 8 bits = 48 bits | 9 bits | 5.33:1 | 81.25% |
The average code length is 9 ÷ 6 = 1.5 bits per symbol. A self-contained compressed file also needs a codebook, headers and byte padding. On a short message these can cost more than the payload saves.
For a longer example, select The last train. Its 181 symbols come from an alphabet of 27, so fixed width costs 905 bits. The Huffman payload takes 751 bits, saving 17.02%. Switch to UTF-8 text and the baseline becomes 1,448 bits. Try The morning ferry to see how multibyte Chinese text changes the UTF-8 comparison.
Decode without separators
Click Decode bit by bit. The first 1 reaches an internal node, so the output is empty. The next 0 reaches B and the decoder emits the first letter. Start the next codeword at the root. Bit three, another 0, yields A on its own. Pause here and select B’s codeword to trace its path. Decoded text still reads BA. Choose Next step to read bit four or Play to continue to BANANA.
A codeword ends at a leaf, so no complete codeword is the prefix of another. This lets the decoder recognize a symbol as soon as it reaches a leaf. The DEFLATE specification illustrates the same prefix-code traversal. RFC 1951 on Huffman coding
Try other inputs
- Tied frequencies. Select Equal counts · ABCD. Each of the four symbols uses two bits. A tie goes to the node created earlier, and the first node taken becomes the left child. A different tie rule can produce different codewords with the same total length.
- A wide tree. Enter
ABCDEFGHIJKLMNOPon a phone. If the two nodes are too far apart to fit, Current merge shows them together with their parent. When a long passage produces a tree several screens wide, the encoding and decoding views follow the current path and keep the root in place. Choose Show full tree to explore the other branches at the same step. - A single symbol. Enter
AAAand click Build tree. The root is itself a leaf. This visualizer assigns it0, so decoding000recovers all three occurrences. - Space and Unicode. Select Chinese and emoji, or enter a sentence with spaces. The visualizer counts Unicode code points, including whitespace. An emoji composed of several code points can contribute several symbols.
To explore how a priority queue retrieves a minimum, try the min-heap visualizer. Return to the Huffman tree
