Proof. We use induction on \(n\). For \(n = 2\), every prefix code gives each value a codeword of at least one bit, and the Huffman code gives them the codewords \(0\) and \(1\), so no prefix code is shorter.
Let \(n > 2\), and suppose the claim holds for \(n - 1\) values. Let \(x\) and \(y\) be two values with the smallest probabilities, the pair that the first step of the construction merges.
First, some prefix code with the least expected length has \(x\) and \(y\) as siblings, meaning that their codewords have the same length \(m\) and differ only in the last bit. To see this, take a prefix code with the least expected length.
- If \({\mathbb{p}\mathopen{}\left(u\right)\mathclose{}} > {\mathbb{p}\mathopen{}\left(v\right)\mathclose{}}\), then \({\ell\mathopen{}\left(u\right)\mathclose{}} \le {\ell\mathopen{}\left(v\right)\mathclose{}}\). Otherwise, exchanging the codewords of \(u\) and \(v\) changes \(L\) by \(({\mathbb{p}\mathopen{}\left(u\right)\mathclose{}} - {\mathbb{p}\mathopen{}\left(v\right)\mathclose{}})({\ell\mathopen{}\left(v\right)\mathclose{}} - {\ell\mathopen{}\left(u\right)\mathclose{}}) < 0\).
- A longest codeword \(c\) has a sibling. Otherwise, deleting the last bit of \(c\) leaves a string that neither begins another codeword nor is begun by one, which gives a prefix code with a smaller \(L\).
Let \(u\) be a value with a longest codeword, and let \(v\) be the value of its sibling. Both codewords have length \(m\), the largest codeword length. By the first bullet, exchanging the codeword of \(x\) with that of \(u\) does not increase \(L\), because \({\mathbb{p}\mathopen{}\left(x\right)\mathclose{}} \le {\mathbb{p}\mathopen{}\left(u\right)\mathclose{}}\) and \({\ell\mathopen{}\left(u\right)\mathclose{}} = m\) is the largest length. The same holds for exchanging \(y\) with \(v\). If \(x\) or \(y\) is already one of \(u\) and \(v\), leave it where it is, and exchange the other with the remaining one of the pair. If \(x = v\) and \(y \ne u\), for example, exchange \(y\) with \(u\). Ties among the smallest probabilities do not matter, because every value other than \(x\) and \(y\) has probability at least \({\mathbb{p}\mathopen{}\left(y\right)\mathclose{}}\), and the exchanges use only that. These exchanges give a prefix code with the least expected length in which \(x\) and \(y\) are siblings.
Next, let \(X'\) be the random variable that merges \(x\) and \(y\) into one value \(z\) with \(\mathbb{p}'\mathopen{}\left(z\right)\mathclose{} = {\mathbb{p}\mathopen{}\left(x\right)\mathclose{}} + {\mathbb{p}\mathopen{}\left(y\right)\mathclose{}}\) and keeps every other value and probability. Any prefix code for \(X\) in which \(x\) and \(y\) are siblings turns into a prefix code for \(X'\) by deleting the last bit of their common codeword to make the codeword of \(z\). The two codes differ only in the lengths of \(x\), \(y\) and \(z\), so their expected lengths satisfy
\[L= L' + {\mathbb{p}\mathopen{}\left(x\right)\mathclose{}} + {\mathbb{p}\mathopen{}\left(y\right)\mathclose{}}.\]
The Huffman code for \(X\) is built from a Huffman code for \(X'\) by replacing the codeword of \(z\) with two codewords, one that ends in \(0\) and one that ends in \(1\). By the induction hypothesis, a Huffman code for \(X'\) has the least expected length \(L'\) among prefix codes for \(X'\). So no prefix code for \(X\) in which \(x\) and \(y\) are siblings has an expected length below that of the Huffman code for \(X\). By the first part, a prefix code with the least expected length is among these, so the Huffman code has the least expected length among all prefix codes.