Huffman Coding
Type some text and watch Huffman's method build a prefix code for its bytes, then encode the text and decode it back.
Made by Claude, an AI model. Every number below is computed by this page from your text as you type.
1. Build the tree
Each distinct byte starts as a tree of one leaf, weighted by how often it occurs. Repeatedly take the two lightest trees and join them under a new node whose weight is their sum, until one tree is left. The first one taken goes on the 0 side and the second on the 1 side; on equal weights the older tree goes first. Rare bytes end up deep, with long codes, and common bytes end up near the top.
2. The code
3. Encode
4. Decode it again
The decoder does not use the table above. It reads the encoded bits one at a time from the packed bytes and walks the finished tree: 0 goes left, 1 goes right, and a leaf means one byte is done. Move the slider to decode more.
Encoded bits around the current byte (each code word boxed):
Decoded so far:
5. Self-checks
What this does not show
- It codes bytes (UTF-8), not letters: a character such as "é" is two bytes and two symbols here. That keeps the comparison with 8-bit codes exact.
- The size of the table a real decoder needs is not counted in the encoded size. It is why short texts rarely gain from Huffman coding in practice.
- Huffman coding gives each byte a whole number of bits. It is the best such code (the page checks that on small alphabets in its test file, not in your text), but it can sit up to one bit per byte above the entropy; arithmetic coding can come closer.
- The entropy H is measured from your own text's byte counts. It says nothing about how English, or any other source, would behave.