Definition · Cost of a treeFor a full binary tree T whose leaves carry the symbols, write B(T) = Σ wᵢ · dᵢ, where dᵢ is the depth of symbol i. A prefix code corresponds to such a tree and its total bit count is B(T), so minimising the code is minimising B over trees and over assignments of the symbols to leaves.
Theorem · Lemma 1: the two lightest can be made deepest siblingsLet x and y be two symbols of smallest weight. Some optimal tree has x and y as sibling leaves at maximum depth.
Take any optimal tree T and let a and b be two sibling leaves at maximum depth — they exist because the tree is full, so the deepest leaf has a sibling, and that sibling is also a leaf at the same depth.
Swap a with x. Writing d for depths, the cost changes by (w_a − w_x)(d_x − d_a). Since x is of smallest weight, w_a − w_x ≥ 0; since a is at maximum depth, d_x − d_a ≤ 0. The product is at most 0, so the cost did not increase, and T was optimal, so it did not decrease either.
Swap b with y in the same way. The result is an optimal tree in which x and y are sibling leaves at maximum depth.
□
Theorem · Lemma 2: merging reduces the problemLet x and y be two symbols of smallest weight and let z be a new symbol of weight w_x + w_y replacing them. If T' is an optimal tree for the reduced alphabet, then the tree T obtained by replacing the leaf z with an internal node whose children are x and y is optimal for the original alphabet.
First, the costs are related by a constant. In T, x and y sit one level below where z sat, so B(T) = B(T') − w_z d_z + (w_x + w_y)(d_z + 1) = B(T') + w_x + w_y, using w_z = w_x + w_y. The same identity holds for any tree of the reduced alphabet and its expansion.
Suppose some tree U for the original alphabet had B(U) < B(T). By the first lemma we may assume x and y are sibling leaves in U. Contract that pair into a single leaf z to get a tree U' for the reduced alphabet, with B(U') = B(U) − w_x − w_y.
Then B(U') = B(U) − w_x − w_y < B(T) − w_x − w_y = B(T'), contradicting the optimality of T'. So no such U exists and T is optimal.
□
The induction is now immediate. On two symbols the only full binary tree gives both a one-bit codeword and is optimal. For n symbols, Huffman's first merge takes the two lightest, the rest of its run is Huffman on the reduced alphabet, which by induction is optimal there, and the second lemma lifts that optimality back. So Huffman's code is optimal for every alphabet.
What the enumeration adds
The panel does something the proof does not: it builds every full binary tree on n leaves and, for each, tries every assignment of the weights to its leaves, then reports the smallest total. The number of shapes on n leaves is the Catalan number C(n−1), so four leaves give 5 shapes, five leaves 14, and six leaves 42; with n! assignments each, that is 120, 1680 and 30,240 (shape, assignment) pairs respectively.
The lab opens on four equal weights, a:10, b:10, c:10, d:10. All 5 shapes and all 120 pairs are tried, the best is 80 bits, and Huffman's code costs 80. On the six-symbol alphabet the best of the 30,240 pairs is 224 bits, which is again what Huffman produced. On the skewed and Fibonacci alphabets, 170 and 25 bits respectively, matched both times.
Trying every assignment as well as every shape is deliberate, and it is where the check earns its place. A search that placed the heaviest weight on the shallowest leaf of each shape would be assuming the rearrangement inequality — the same inequality the first lemma is built on — and would therefore be checking the proof against itself. Enumerating the assignments assumes nothing, so the two can disagree, and on four alphabets they do not.
Where the search stops, and why the page says so
Seven leaves is 132 shapes and 5,040 assignments each, and the panel refuses rather than running it. When it refuses it prints why, and the expected length above stays exact: what is missing is the confirmation that nothing beats it. A page that silently dropped the check would look identical to a page that had run it, which is the failure mode this whole path is built against.
So the honest reading of this page has three layers rather than two. Measured: on the four shipped alphabets, the minimum over every shape and every assignment equals Huffman's cost — 80, 224, 170 and 25 bits. Proved: Huffman is optimal for every alphabet, by the two lemmas and the induction. Refused: on seven or more symbols the search does not run, and the page says which of the two sentences it can still stand behind.