Understanding the Huffman code generator — concept and principle
Huffman coding assigns short bit strings to frequent characters and longer ones to rare characters, producing the shortest possible prefix code for a given set of symbol frequencies. This tool builds the code tree for your text, lists each character's code and counts the encoded length. Huffman coding is a building block of DEFLATE (ZIP, PNG, gzip) and baseline JPEG.
How it is calculated
- Count how often each character appears.
- Put every character in a list as a leaf weighted by its count.
- Repeatedly remove the two lightest nodes and join them under a new node whose weight is their sum, until one tree remains.
- Read each code from the root: a left branch adds 0, a right branch adds 1.
- Encoded bits = Σ count(x) × code length(x)
- Size vs 8-bit ASCII = encoded bits ÷ (characters × 8) × 100, the share of plain 8-bit storage; Saving = 100% minus that.
The size of the code table itself is not included.
The principle
Because no code is a prefix of another, a decoder can read the bit stream left to right without separators. Merging the two least frequent nodes first pushes the rarest symbols deepest in the tree, where long codes cost little because they are seldom used. David Huffman proved in 1952 that this greedy choice gives an optimal prefix code: no other symbol-by-symbol code has a smaller total. The average code length lies within one bit of the Shannon entropy.
Worked example
- Text: "abracadabra", 11 characters. Counts: a = 5, b = 2, r = 2, c = 1, d = 1.
- Merge c(1) + d(1) = 2; then b(2) + r(2) = 4; then cd(2) + br(4) = 6; finally a(5) + 6 = 11.
- Codes: a = 0, c = 100, d = 101, b = 110, r = 111.
- Encoded bits: 5×1 + 1×3 + 1×3 + 2×3 + 2×3 = 23 bits.
- ASCII needs 11 × 8 = 88 bits, so the size is 23 ÷ 88 = 26.14% and the saving 73.86%.
How to read the result
A lower percentage means better compression. Average bits per character here are 23 ÷ 11 ≈ 2.09, just above the entropy of about 2.04 bits. Ties can be broken differently, so another tool may give different codes, but the total length of an optimal code is the same.
Common mistakes
- Forgetting that the decoder also needs the code table, which can outweigh the savings on short text.
- Expecting identical codes from every implementation; only the total is guaranteed.
- Mixing up the two percentages: 26.14% is the size that remains, 73.86% the part saved.
Frequently asked questions
Is Huffman coding lossless?
Yes. Decoding the bit stream with the same tree returns exactly the original text.
How is Huffman different from arithmetic coding?
Huffman uses a whole number of bits per symbol; arithmetic coding can spend fractional bits and gets closer to the entropy, especially for skewed distributions.
Why are Huffman codes prefix-free?
Symbols sit only at the leaves of the tree, so the path to one symbol never passes through another.
Sources: D. A. Huffman, "A Method for the Construction of Minimum-Redundancy Codes", Proceedings of the IRE, 1952 · RFC 1951, DEFLATE Compressed Data Format Specification version 1.3