Huffman Coding
Huffman coding constructs a binary prefix code that minimizes weighted codeword length for a known set of symbol frequencies:
where is a symbol frequency and its code length.
Construction
- Put one leaf per positive-frequency symbol into a min-priority queue.
- Remove the two least frequent nodes.
- Join them under a parent whose weight is their sum.
- Reinsert the parent and repeat until one tree remains.
- Label left/right edges with bits; each root-to-leaf path is a codeword.
Leaves cannot prefix one another, so concatenated codewords decode unambiguously given the tree or an equivalent canonical-code description.
Why the greedy merge is safe
In some optimal full binary prefix tree, two least frequent symbols can be made deepest siblings by exchanging leaf labels without increasing weighted length. Contracting those siblings produces the same problem on one combined frequency, which justifies the recursive greedy step.
Cost and boundary
For symbols, heap construction and merging take time and space. Ties can produce different but equally optimal code lengths.
The encoded stream must also represent the codebook, padding, and original length as needed. Huffman coding is optimal for the stated prefix-code model, not universally optimal compression; block structure and richer probabilistic models can exploit dependencies it ignores.