Canonical Huffman codes for the six-symbol set — Huffman coding

coresame lengths, standard assignment

Answer

a=0 b=100 c=101 d=110 e=1110 f=1111

Why this example is worth doing

The code lengths are forced by the algorithm but the actual bit patterns are not — swapping the two children of any node gives a different, equally good code. Canonical Huffman fixes an assignment by sorting on length and then symbol, which means a decoder needs only the lengths rather than the whole tree. That is why real formats such as DEFLATE transmit lengths, and it is why this page always shows the canonical form alongside its own tree.

Try your own input in the Huffman coding. Build the tree from text or from frequencies, with the merge order and canonical codes.

How the answer is reached

Codebook

Codebook — columns symbol, code
symbolcode
a0
b100
c101
d110
e1110
f1111

Merge order

Merge order — columns step, left, right, weight
stepleftrightweight
15914
2121325
3141630
4253055
54555100

Compare with

Open this example in the Huffman coding

The field arrives filled in with this example’s input.

Note:

Notation this page assumes

  • Bit strings are written most significant bit first, and bit 0 is the least significant bit.
  • A width is stated explicitly wherever it changes the answer; nothing is silently sign-extended or truncated.
  • Ties in the merge queue are broken by the fully specified key the engine documents, so the tree is reproducible rather than merely valid.

Sources

  • Huffman, “A Method for the Construction of Minimum-Redundancy Codes” (1952)
  • Shannon, “A Mathematical Theory of Communication” (1948)