Mental Model
Huffman encoding builds a tree to assign shorter codes to frequent letters and longer codes to rare letters, saving space when storing data.
Analogy: Imagine packing a suitcase where you put your most used clothes on top for easy access and less used clothes at the bottom; Huffman encoding puts frequent letters closer to the root for shorter codes.
Characters with frequencies:
[a:5] [b:9] [c:12] [d:13] [e:16] [f:45]
Initial nodes:
[a:5] [b:9] [c:12] [d:13] [e:16] [f:45]
After building tree:
[100]
/ \
[45:f] [55]
/ \
[25] [30]
/ \ / \
[12:c][13:d][14] [16:e]
/ \
[5:a][9:b]