Visualize Huffman Coding step by step. Watch the min-heap merge characters by frequency and assign shorter codes to frequent characters for lossless compression.
Huffman coding assigns variable-length binary codes to characters. Frequent characters get shorter codes. No code is a prefix of another, enabling unambiguous decoding.
Build a min-heap of (frequency, node) pairs. Repeatedly extract the two minimum nodes, merge them into a parent node with combined frequency, and push it back. Repeat until one tree remains.
DFS the Huffman tree: assign "0" for left edges and "1" for right edges. Leaf nodes receive their final codes. Shorter codes = higher frequency = fewer bits transmitted.
O(N log N): N-1 heap operations, each O(log N). Optimal prefix code for the given frequencies, proven by the greedy exchange argument.