Huffman Coding Step by Step: Build the Tree, Codes and Entropy
How Huffman coding compresses data, how to build the tree and code table by hand, how to find average length, entropy and efficiency, and why ties differ.

Huffman Coding Step by Step: Build the Tree, Codes and Entropy
Huffman coding is one of the classic algorithms of computer science, taught in courses on data structures, information theory and communications. It is also still at work inside ZIP files, PNG images and JPEG photos. The free Huffman Coding Calculator builds the code for any text or frequency table and shows every number you need to check your work.
The idea
Ordinary text uses the same number of bits for every character, such as 8 bits in ASCII. But characters are not equally common: in English, e appears far more often than z. Huffman coding gives frequent symbols short codes and rare symbols long ones, so the total length of a message shrinks.
Building the tree by hand
Take the textbook example with six symbols and these frequencies: A 45, B 13, C 12, D 16, E 9, F 5.
- List the symbols as single-node trees, sorted by frequency.
- Take the two lightest, F (5) and E (9), and join them under a new node of weight 14.
- Repeat with the two lightest remaining: C (12) and B (13) make 25.
- Then 14 and D (16) make 30; then 25 and 30 make 55; finally A (45) and 55 make 100.
- Label every left branch 0 and every right branch 1. Each symbol's code is the path from the root.
A, the most common symbol, ends up with a one-bit code, while E and F get four bits.
Measuring the result
- Average code length L = Σ p × length. For the example, 2.24 bits per symbol.
- Entropy H = −Σ p log₂ p, the theoretical minimum for any code: about 2.22 bits here.
- Efficiency = H ÷ L, here above 99%.
- Redundancy = L − H, the bits per symbol wasted compared with the ideal.
- Saving compared with a fixed-length code: six symbols need 3 bits each, so Huffman saves about 25%.
Shannon's source coding theorem guarantees H ≤ L < H + 1 for Huffman codes.
Prefix-free codes
No Huffman code is the beginning of another. Reading the bit stream from the start, as soon as the bits match a code you know which symbol it is, so no separators are needed. The calculator shows the encoded bit stream for text input so you can see this in action.
Why your answer may differ
When two weights are equal, you can join either first, and textbooks differ on whether the lighter tree goes left or right. Different choices give different codes, but every Huffman code for the same frequencies has the same minimum average length. If your codes differ from a model answer, compare the code lengths and the average.
Limits of Huffman coding
Each symbol needs at least one bit, so Huffman wastes space when one symbol is very common. Arithmetic coding and asymmetric numeral systems (used in modern compressors such as Zstandard) get closer to the entropy. Real compressors also find repeated phrases before applying Huffman coding, which is why they compress text much more than character-by-character Huffman does.
Decoding by hand
To decode, start at the root and follow 0 or 1 for each bit until you reach a leaf, write down its symbol, and return to the root.
Step by step
- Choose text or a frequency list.
- Enter the data, one symbol and weight per line for frequencies.
- Read the code table with probabilities, codes and lengths.
- Compare the average length with the entropy.
- Check the saving over a fixed-length code.
Privacy
The calculator runs in your browser.


