Frequencies a:45 b:13 c:12 d:16 e:9 f:5 — Huffman coding

introthe textbook six-symbol example

Answer

average 2.2400 bits/symbol, 224 bits total

Why this example is worth doing

The standard example from Cormen, Leiserson, Rivest and Stein, included so that a student can check this tool against the book they already own. The algorithm repeatedly merges the two least frequent nodes, so the rarest symbols end up deepest and therefore longest. Against a fixed three-bit code the saving is about a quarter, and the page shows the merge order as a numbered list because the order is what an exam asks you to reproduce.

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
c100
b101
f1100
e1101
d111

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)