You have probably read that ZIP and PNG use Huffman coding, and that it gives common letters short codes. That summary leaves the real question open. How does anyone decide which symbol gets which code, and how many bits does it really save?

This guide builds a Huffman code by hand for a 14-character string. Every step is small enough to check with a pencil, and the final bit count is compared with the usual 8 bits per character.

The idea: spend fewer bits on common symbols

Standard text storage gives every character the same 8 bits. A variable-length code gives common symbols fewer bits and rare ones more, so the average drops. The catch is ambiguity. If A were 0 and N were 01, the bits "01" could mean N, or A followed by something that starts with 1.

The fix is the prefix rule: no code may be the beginning of another code. Huffman's 1952 paper states the requirement as no message being coded so that its code appears, digit for digit, as the first part of any longer code. With that rule a decoder never needs separators. Huffman's method, from the paper's summary, builds a code that minimizes the average number of coding digits per message.

Building the tree: count, then merge

First, count the symbols. The example string is BANANA BANDANA, which has 14 characters including one space.

Symbol Count
A 6
N 4
B 2
space 1
D 1

The counts add up to 14. At 8 bits per character, the string takes 14 × 8 = 112 bits.

Second, merge the two lightest, again and again. Treat each symbol as a small tree with a weight equal to its count. Repeatedly take the two lightest trees, join them under a new parent, and give the parent their combined weight. Stop when one tree remains.

Step Two lightest New weight
1 space (1) and D (1) 2
2 B (2) and the space-D tree (2) 4
3 N (4) and the B-space-D tree (4) 8
4 A (6) and the N-B-space-D tree (8) 14

The last weight, 14, equals the string length, which is a handy check. In this example no step involves a tie, so the tree shape is forced.

Why the two lightest? Huffman's paper reasons that in an optimum code the least probable messages get the longest codes, and that the two least probable can be given codes that differ only in their last digit. Joining them into one composite message leaves a smaller problem of the same kind. Repeating that step is the whole procedure. It also explains the tree: the first join, space and D, ends up deepest, because those two symbols are the rarest.

Reading the codes off the tree

Label each join with 0 for the first tree named and 1 for the second. A symbol's code is the path of labels from the root down to it. Frequent symbols sit close to the root, so they get short codes.

Symbol Count Code Bits each Total bits
A 6 0 1 6
N 4 10 2 8
B 2 110 3 6
space 1 1110 4 4
D 1 1111 4 4

Worked example. Total bits: 6 + 8 + 6 + 4 + 4 = 28. Fixed 8-bit text: 14 × 8 = 112 bits. Ratio: 112 ÷ 28 = 4:1. Percent saved: (1 − 28 ÷ 112) × 100 = 75%. A fairer baseline is a fixed-length code sized for five symbols, which needs 3 bits each: 14 × 3 = 42 bits. Against that, 42 ÷ 28 = 1.5:1, or 33.3% saved. Check either pair in the compression ratio calculator; any two sizes in the same unit give the same ratio.

The full message, B-A-N-A-N-A-space-B-A-N-D-A-N-A, encodes as 1100100100111011001011110100, which is 28 bits. Here is a second check on that count. Each join adds one bit to every occurrence of the symbols under it, so the total equals the sum of all the join weights: 2 + 4 + 8 + 14 = 28.

Decoding needs no separators. A decoder reads bits until they match a code, outputs that symbol and starts over. The first bits are 1, 11 and then 110. The first two are not codes, but 110 is, so the first symbol is B. The next bit, 0, is a code by itself, so A. Then 10 gives N, and the rest follows the same way. Because no code starts another, only one reading is possible.

What the example leaves out

A decoder cannot rebuild this message from 28 bits alone. It also needs the code table, or at least each symbol's code length, because a fixed rule can turn lengths into codes. DEFLATE uses such a rule: codes of one length are consecutive values in symbol order, and shorter codes come before longer ones. Example: storing each of the 5 symbols as 8 bits plus a 3-bit length costs 5 × 11 = 55 bits. The total becomes 28 + 55 = 83 bits against 112, so 25.9% saved instead of 75%. The table is a fixed cost, so it matters less as the message grows. In DEFLATE's dynamic blocks, a pair of Huffman code trees, themselves stored in compact form with Huffman coding, comes first, followed by the data they describe.

Huffman coding also looks only at how often each symbol occurs, not at the order. It cannot notice that a whole phrase repeated. That job belongs to the back-references described in how repeated patterns let files shrink. RFC 1951 combines the two in DEFLATE, the method behind ZIP, gzip and PNG, covered in how DEFLATE works. The JPEG standard also lists Huffman coding as one of two entropy coding options.

Common mistakes

  • Assuming the code is unique. Swapping 0 and 1 at any join gives a different code with the same lengths and the same 28 bits. Ties between equal weights can also change the tree.
  • Assigning codes by hand without the prefix rule. Short codes handed out freely lead to the ambiguity shown above. Building from the merges guarantees the rule.
  • Ignoring the table. The 28 bits are only decodable with the code table. Count it on small inputs.
  • Expecting gains when symbols are equally common. With four symbols that each appear once, the merges give every symbol a 2-bit code, the same as a plain fixed-length code.
  • Treating Huffman coding as a full compressor. It handles symbol frequencies only. Formats pair it with another step.

FAQ

Who invented Huffman coding?

David Huffman described it in the paper "A Method for the Construction of Minimum-Redundancy Codes," published in the Proceedings of the IRE in 1952. The paper lists him at the Massachusetts Institute of Technology.

Is Huffman coding lossless?

Yes. Every code maps back to exactly one symbol, so decoding returns the original sequence exactly.

Is a Huffman code the best possible?

Among prefix codes for a given set of symbol frequencies, yes. RFC 1951 describes a Huffman code as an optimal prefix code that uses the fewest bits of any prefix code for that alphabet.

Where is Huffman coding used today?

The DEFLATE format uses it, so ZIP files can use it and PNG images use it. The PNG specification says its compression is a deflate method. The JPEG standard offers Huffman coding as one of two entropy coding procedures.

Sources

  1. Huffman, D. A. (Proceedings of the IRE, 1952; copy hosted by the University of Crete), A Method for the Construction of Minimum-Redundancy Codes. Accessed November 6, 2026.
  2. Princeton University, Sedgewick and Wayne, 5.5 Data Compression (Algorithms lecture slides). Accessed November 6, 2026.
  3. IETF, RFC 1951: DEFLATE Compressed Data Format Specification version 1.3. Accessed November 6, 2026.
  4. ITU-T (copy hosted by W3C), Recommendation T.81: Digital compression and coding of continuous-tone still images. Accessed November 6, 2026.
  5. W3C, Portable Network Graphics (PNG) Specification (Third Edition). Accessed November 6, 2026.
  6. PKWARE, APPNOTE.TXT: .ZIP File Format Specification, version 6.3.10. Accessed November 6, 2026.