Skip to main content

Huffman tree definition and example

Short answer

A Huffman tree is a full binary tree that minimizes weighted external path length for optimal prefix codes. Build: repeatedly merge the two lowest-weight nodes; n leaves, n‑1 internal nodes. Example: merging A5,B9→14, C12,D13→25, 14+E16→30, 25+30→55, F45+55→100 gives codes F=0, C=100, D=101, A=1100, B=1101, E=111.

Define Huffman tree with its properties and example.

A Huffman tree is a binary tree built from a set of symbols with weights (frequencies), such that the weighted external path length Σ (weight × depth of leaf) is minimum. It is used to produce an optimal prefix code for lossless data compression: frequent symbols get short codes and rare symbols get long codes.

Properties

  1. It is a strictly (full) binary tree: every internal node has exactly two children. For n symbols there are n leaves and n − 1 internal nodes.
  2. Each symbol is a leaf. An internal node holds the sum of the weights of its two children; the root weight is the total frequency.
  3. Symbols of higher frequency are at a smaller depth (shorter code); the two least frequent symbols are siblings at the deepest level.
  4. Labelling left edges 0 and right edges 1 gives a prefix code: no code is the prefix of another, so decoding is unambiguous.
  5. The tree is not unique when weights tie, but all Huffman trees give the same minimum average code length.
  6. Built greedily in O(n log n) with a min-heap.

Example

Symbols with frequencies: A = 5, B = 9, C = 12, D = 13, E = 16, F = 45.

StepMergeNew weightRemaining
1A(5) + B(9)14C12, D13, 14, E16, F45
2C(12) + D(13)2514, E16, 25, F45
314 + E(16)3025, 30, F45
425 + 3055F45, 55
5F(45) + 55100root
            100
           /    \
        F:45     55
                /    \
              25      30
             /  \    /   \
          C:12 D:13 14    E:16
                   /  \
                A:5   B:9

Codes (left = 0, right = 1): F = 0, C = 100, D = 101, A = 1100, B = 1101, E = 111.

Average length = (45×1 + 12×3 + 13×3 + 5×4 + 9×4 + 16×3)/100 = 224/100 = 2.24 bits per symbol, against 3 bits for fixed-length codes.

✓ Answer checked by Claude Opus 5.5 (high effort)

All Data Structure and Algorithm chapter 6 questions →