Huffman Code Generator
Huffman coding is the reason common letters can be stored in fewer bits than rare ones, and it is provably the best you can do with whole-bit codes per symbol. This builds the code for text you supply, shows every character's bit pattern and count, and then decodes the encoded bits back to your original text — because a code that cannot be decoded is not a code, and prefix-freedom is easy to claim and easy to get wrong.
What this generator does
Counts every character, merges the two lightest nodes repeatedly to build the Huffman tree, and reads the codes off it. The result is checked for prefix-freedom, decoded back to the original text, and compared against a fixed-width code over the same alphabet.
How to use this tool
- Type or paste the text to encode.
- Run, and read each character's frequency and bit pattern.
- Compare the total bits against the fixed-width figure.
- Notice that no code is a prefix of any other — that is what makes it decodable.
Understanding the controls
- Text
- Whatever you want encoded. Text with very uneven letter frequencies compresses far better — try a passage of English against a random string of the same length.
Common use cases
- Working through a Huffman coding example with your own text
- Teaching why frequency-based codes beat fixed-width ones
- Checking a Huffman implementation against a known-good output
- Showing what a prefix-free code actually looks like
- Demonstrating why compression rarely pays on very short inputs
How this generator works
Every character becomes a leaf weighted by how often it occurs. The two lightest nodes are repeatedly merged into a parent until one tree remains, and each character's code is the path down to its leaf. That construction guarantees the code is prefix-free and that no other whole-bit code is shorter. Before display, every pair of codes is checked so none is a prefix of another, the bits are decoded back and compared against your text character for character, and the total is confirmed never to exceed the fixed-width equivalent.
Randomness and fairness
Nothing here is random. Ties between equally light nodes are broken by insertion order, so the same text always produces exactly the same tree and codes.
For how randomness is produced across the whole site, see how Generate Random works.
Limitations and good to know
- The size of the tree itself is not counted, and a real compressed file has to store it — which is why Huffman coding often loses on short inputs.
- Codes are whole numbers of bits per symbol, so arithmetic coding can beat this on skewed distributions.
- Each character is coded independently, so repeated words and phrases gain nothing here.
- Different tie-breaking rules give different but equally short codes, so another implementation may produce different bit patterns of the same total length.
Privacy and your data
Your text is encoded and decoded entirely in your browser. Nothing is uploaded, stored, or included in analytics.
Related generators
- Run-Length Encoding GeneratorEncodes text as runs of repeated characters, decodes it back to prove it is exact, and says plainly when the result is longer.
- Edit Distance CalculatorThe fewest single-character edits that turn one word into another, listed step by step and proved by replaying them.
- Text Diff GeneratorCompares two texts line by line and shows the smallest set of changes, checked by applying them back to the original.
- Random Binary String GeneratorA string of 0s and 1s of a fixed length — not a converted number.