Huffman's algorithm repeatedly merges the two least probable current symbols, labels the two new edges , and replaces the pair by a compound symbol whose probability is their sum. Reading paths from the final root gives a prefix code.
For optimality, first observe that in some optimal full binary tree the two least probable symbols are sibling leaves at maximum depth: exchange them with any deepest sibling pair, putting the smaller probabilities at no smaller depths, without increasing expected length. Contract that sibling pair to one symbol of combined probability. The original expected length equals the contracted tree's expected length plus the pair's combined probability. Induction on the alphabet size now proves that choosing the two least probabilities and recursing is optimal, which is precisely Huffman's algorithm.
If a symbol has length one, it must be . At the three-weight stage, the other two weights must be merged while survives, so . Thus , proving . Therefore forces every length to be at least two.
For the other bound, suppose is first merged at the three-weight stage, with weights , where . The preceding merge created from two weights no larger than the surviving , so . Since also , we have , and henceThus this can happen only when . If , it survives until the final merge and receives a length-one word. At equality, ties can support either tree shape.
Solved by gpt-5.6-sol high.
Codex Wiki