The displayed prefix code has expected codeword length ternary digits per source symbol. Instead assign
This 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 is
The 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.
Figure 1.
An original ternary prefix tree with expected length 1.25 ternary digits per source symbol
. Details.
First assume a full-support finite distribution, as stated above. Let be the largest codeword length and take a tree leaf at depth . Every existing tree child of its tree parent must also be a tree leaf: an internal tree child would have a descendant deeper than . If the chosen tree leaf were its tree parent's only tree child, moving its symbol to that tree parent would shorten its codeword by one, preserve the prefix code property, and reduce expected codeword length by its positive probability. That contradicts optimality. The tree parent therefore has at least two tree children, both tree leaves. A second codeword has the same first digits and a different last digit, proving the required deepest sibling codeword property for a ternary prefix code.
For the literal wording allowing zero probabilities, take and the prefix code . Its expected codeword length is one, which is minimal: with more than one message an empty codeword is impossible, so the positive-probability message must have length at least one. Yet the deepest tree leaf has no sibling codeword. Thus positivity or an additional convention removing redundant zero-weight branches is needed.
For a finite source alphabet with at least two symbols and strictly positive weights, every ternary prefix code minimizing weighted codeword length has all internal vertices of outdegree three, except possibly one of outdegree two whose tree children are deepest tree leaves.
To prove this, let be the greatest tree leaf depth. A missing tree child at any depth would permit moving a deepest tree leaf into that vacant branch, strictly reducing its positive weighted cost. Thus all internal vertices above depth have three tree children. Tree parents at depth have only tree leaves as tree children; one tree child would permit contraction and strict improvement. If two such tree parents each had two tree children, move one tree child of the second into the first tree parent's third slot, preserving its depth, and contract the second tree parent's remaining tree child. This again strictly improves cost. There is therefore at most one binary tree parent. If the number of symbols is an odd number, the leaf-count formula for a full ternary tree forces to be an even integer; since , there is no binary tree parent.
Strict positivity matters. For probabilities , the prefix code minimizes expected codeword length at one trit, yet has unary vertices and a deepest tree leaf without a sibling codeword. Removing or rearranging zero-weight branches can select a reduced minimizer, but arbitrary minimizers need not have the positive-weight shape.
Ternary Huffman coding 2026-10-25
The three-symbol output version of Huffman coding. For an odd number of source symbols, repeatedly merge the three least current weights, replacing them by their sum. Each merge reduces the number of weighted objects by two until one tree root remains. Label each tree parent's outgoing edges to obtain a ternary prefix code. The merge weights sum to the expected codeword length in trits.
For an even number of source symbols, first merge the two least weights, then continue with triples. Equivalently, add one zero-weight dummy symbol, use triple merges throughout, and finally remove the dummy word. Ties may be resolved arbitrarily. This minimizes expected codeword length. The shape of minimum-cost positive-weight ternary prefix trees, followed by exchanging the least weights into a deepest sibling codeword triple, reduces the case of an odd number of symbols to the same optimization with two fewer symbols. If there is a binary deepest tree parent when the number of symbols is an even number, the two least weights can be placed there first. Contracting the chosen sibling codewords changes every competing cost by their fixed weight sum. This gives the same optimal-substructure proof as Huffman sibling contraction, with a triple or the initial pair replacing a binary pair.