ScienceComputer Science

Huffman Coding
calculator.

Build the Huffman code for a text: short codes for common letters, long codes for rare ones.

How it's worked out

Count how often each symbol appears. Make one leaf per symbol, weighted by its count. Then repeat one step until a single tree is left: take the two lightest nodes and join them under a new node whose weight is their sum.

Read each code from the root down: a left branch adds a 0 and a right branch adds a 1. RFC 1951, the DEFLATE format behind ZIP and gzip, labels the branches the same way. Symbols sit only at the leaves, so no code is the start of another, and a decoder can split the bits without any gaps between codes.

The average code length is the total bits over the number of symbols, where cic_i is how often symbol ii appears and ℓi\ell_i is the length of its code:

L‾\displaystyle \bar{L}=∑ciℓi∑ci\displaystyle {}= \frac{\sum c_i \, \ell_i}{\sum c_i}

Shannon (1948) showed that no code can average fewer bits per symbol than the entropy, where pip_i is the share of the text that is symbol ii:

H\displaystyle H=−∑pilog2⁡pi\displaystyle {}= -\sum p_i \log_2 p_i

The tool shows both, and their ratio as the efficiency. When counts tie, the tool joins the node made first, so the same text always gives the same tree.

Worked examples

Priya codes the classic example sentence

The sentence "this is an example of a huffman tree" has 36 characters and 16 different symbols. The space is the most common, with 7, so it gets a 3-bit code, 111. Rare letters such as x get 5 bits.

The whole sentence takes 135 bits, against 288 as 8-bit characters, which is 53% smaller. A fixed-length code with 4 bits per symbol would need 144.

The average is 154\frac{15}{4} bits per symbol, or 3.75. The entropy is 3.71 bits, so the code is 99% efficient.

Open this example in the tool

Tom builds the tree for aaaabbc by hand

The counts are a 4, b 2 and c 1. The two lightest are c and b, so they join first: 1 + 2 = 3. That node joins a: 3 + 4 = 7, the root.

Reading down gives a = 1, b = 01, c = 00. The message takes 10 bits instead of 56 at 8 bits each, or 14 with a fixed 2-bit code, a saving of 29% on the fixed code.

Open this example in the tool

Shannon's four-letter source hits the limit exactly

Shannon's 1948 paper uses a source where A, B, C and D appear half, a quarter, an eighth and an eighth of the time. AAAABBCD has those shares. Huffman gives A = 0, B = 10, C = 110, D = 111, the same code Shannon prints.

The average is 74\frac{7}{4} bits per symbol, and the entropy is also 1.75. Every share is a power of one half, so the code meets the limit with no waste, an efficiency of 100%.

Open this example in the tool

Reading your result

Efficiency. This is the entropy over the average code length. It reaches 100% only when every symbol's share is a power of one half. Roweis's lecture notes note a Huffman code can be as much as one bit per symbol worse than the entropy. Coding blocks of symbols together closes that gap.

Kraft sum. Add 1 over 2 to the power of each code length. For a full Huffman tree it is exactly 1, which shows no shorter code was left unused. For the classic sentence it is 1.

When it saves little. Eight different letters used once each, abcdefgh, give 24 bits, the same as a fixed 3-bit code. Huffman helps only when some symbols are much more common than others. Mississippi, with 4 symbols, takes 21 bits against 22 for a fixed code.

Common mistakes

  • Forgetting the code table. A file must also store the code. For short text that can cost more than it saves: hello takes 10 bits of data, but the table for its 4 symbols has to be sent too.
  • Expecting one right answer. When counts tie, different joins give different codes. Every valid Huffman code for a text has the same total length, so check the total, not the codes.
  • Skipping spaces and line breaks. They are symbols too, and the space is often the most common one.
  • Comparing with 8 bits only. A fixed-length code with just enough bits for the distinct symbols is the fairer comparison, and the savings over it are smaller.

Questions people ask

Ties in the counts can be joined in more than one order, and either branch can take the 0. Both trees are valid Huffman codes, and the total number of bits is the same. With counts 1, 1, 2, 2, one order gives four 2-bit codes and another gives lengths 1, 2, 3 and 3, and both total 12 bits.

Sources