Full ternary tree 2026-10-25
A rooted tree in which every internal vertex has exactly three tree children. For a finite rooted tree with tree leaves and internal outdegrees , counting edges in two ways givesIndeed, the number of edges is the total number of vertices minus one and also the sum of the internal outdegrees. Thus a full ternary tree with internal vertices has tree leaves, always an odd number. More generally, if every internal vertex has either two or three tree children, . This parity controls the initial merge in ternary Huffman coding.
The displayed prefix code has expected codeword length ternary digits per source symbol. Instead assignThis is a ternary prefix code: the first two codewords end at distinct tree children of the tree root, and the other two start with and end at distinct tree children below it. No codeword is a prefix of another. Its expected codeword length isThe reduction is trit per source symbol, which already disproves optimality of the displayed code. The ternary Huffman coding construction below will produce this same improved code tree.
Start with one tree leaf for each source symbol, weighted by its probability. While more than one weighted object remains, choose the three objects with least weights, create their common tree parent with weight equal to their sum, and replace the three by that tree parent. Newly created tree parents participate in subsequent choices. Each merge reduces the number of objects by two; an odd number of objects therefore reaches three and then one, never an unmergeable pair. The final object is the tree root of the code tree.
Label the three outgoing edges at each internal vertex by in any order. The digits along the root-to-leaf graph path give each symbol's codeword. Tree leaves cannot be ancestors of other tree leaves, so this is a prefix code. Ties can be resolved arbitrarily. The expected codeword length is measured in trits, and equals the sum of all merge weights, because each merge adds one digit to every codeword below it.
The structural reason for merging three is that the positive-weight, odd-leaf case has a full ternary tree. Its deepest sibling codeword triple can receive the three least weights by exchanging tree leaf labels, and contracting that triple reduces the problem by two symbols. This explains the recursive construction; a separate optimality proof is not required here.
For an even integer , merge the two least weights once, then repeatedly merge three least weights until only the tree root remains. The initial binary merge leaves an odd number of weighted objects, so the preceding algorithm applies. The initial merged tree parent has two tree children; subsequent tree parents have three. Equivalently, add one zero-weight dummy symbol, run the odd-leaf ternary Huffman coding algorithm, and remove its codeword afterwards. The dummy contributes nothing to expected codeword length.
Here the first merge joins the two weights , producing . The three remaining weights are , and merge directly into the tree root. Assigning the first two tree root branches to the original symbols of weights and givesThe merge-weight check gives as well. For , the first merge is already the tree root; for , outside this case with an even number of symbols, the usual temporary reduced-problem convention would permit an empty word.
