Purpose: Build an optimal alphabetic binary tree in — the minimum-cost binary tree over weighted leaves where the leaves must appear in their original left-to-right order.

Hu-Tucker vs Huffman

HuffmanHu-Tucker
Leaf orderfree — reorder as you likefixed
Methodrepeatedly merge the two smallestthree-phase, order-respecting
Time
Usedata compressionordered search trees, alphabetic codes

The constraint changes everything. Huffman’s greedy “merge the two smallest” is invalid here, because the two smallest weights may not be adjacent, and merging them would reorder the leaves.

Algorithm (three phases)

  1. Combination. Repeatedly merge the pair of nodes with minimum among all pairs that are “tentatively adjacent” — meaning every node strictly between them has already been merged away. Ties are broken by leftmost position. Record the level each original leaf ends up at. This phase does not build the final tree; it only determines the depths.
  2. Level assignment. Read off , the depth of leaf in the combination tree.
  3. Reconstruction. Rebuild a tree with the leaves in their original order and the depths found in phase 1. Such a tree always exists (the depths satisfy the Kraft equality) and is optimal.

The surprising theorem is that the depths from the unconstrained-looking phase 1 are exactly realisable in order — this is the whole content of the Hu-Tucker result and its proof is not short.

Complexity

  • Time: with a priority queue over tentatively-adjacent pairs
  • Space:

Alternatives

ApproachTimeOptimal?
Interval DP yes
Interval DP + Knuth optimizationyes
Hu-Tuckeryes
Garsia-Wachsyes — simpler to implement
Mehlhorn’s greedy heuristicwithin 2 bits of optimal

Garsia-Wachs

If you ever need this in practice, implement Garsia-Wachs instead: it is a cleaner reformulation of the same three-phase idea, with a much shorter description and the same bound. The typical contest version of this problem (merging adjacent piles of stones with cost = sum) is usually small enough for Knuth optimization anyway.

Applications

  • Optimal alphabetic codes — prefix codes where the code order must match the symbol order (so that comparing codes compares symbols)
  • Optimal binary search trees with only successful searches — closely related; Knuth’s algorithm is the standard solution
  • Merging ordered files / piles of stones — the recurring contest form: merge adjacent piles, cost = combined size, minimise total
  • Circuit design and code trees where physical ordering is fixed

Variants / Use Cases