- 2072 Magh · 6 marks
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
- 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.
- 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.
- Symbols of higher frequency are at a smaller depth (shorter code); the two least frequent symbols are siblings at the deepest level.
- Labelling left edges 0 and right edges 1 gives a prefix code: no code is the prefix of another, so decoding is unambiguous.
- The tree is not unique when weights tie, but all Huffman trees give the same minimum average code length.
- 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.
| Step | Merge | New weight | Remaining |
|---|---|---|---|
| 1 | A(5) + B(9) | 14 | C12, D13, 14, E16, F45 |
| 2 | C(12) + D(13) | 25 | 14, E16, 25, F45 |
| 3 | 14 + E(16) | 30 | 25, 30, F45 |
| 4 | 25 + 30 | 55 | F45, 55 |
| 5 | F(45) + 55 | 100 | root |
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)
Related questions
- B-tree properties; order 5 for 3, 14, 7, ... · Data Structure and Algorithm, chapter 6 · asked 2×
- Spanning tree properties; Round Robin MST · Data Structure and Algorithm, chapter 10
- BST properties and recursive search · Data Structure and Algorithm, chapter 6
- MST definition; Kruskal's MST on 6-vertex graph · Data Structure and Algorithm, chapter 10
- Huffman code tree steps with example · Multimedia System, chapter 5 · asked 7×
- Define tree; properties and applications · Discrete Structure, chapter 4
- Flow net: definition, properties, uses · Soil Mechanics, chapter 7 · asked 7×
- Standard normal: definition, properties, importance · Probability and Statistics, chapter 3 · asked 2×