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
| Huffman | Hu-Tucker | |
|---|---|---|
| Leaf order | free — reorder as you like | fixed |
| Method | repeatedly merge the two smallest | three-phase, order-respecting |
| Time | ||
| Use | data compression | ordered 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)
- 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.
- Level assignment. Read off , the depth of leaf in the combination tree.
- 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
| Approach | Time | Optimal? |
|---|---|---|
| Interval DP | yes | |
| Interval DP + Knuth optimization | yes | |
| Hu-Tucker | yes | |
| Garsia-Wachs | yes — simpler to implement | |
| Mehlhorn’s greedy heuristic | within 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
- Huffman Coding — the unordered version
- Knuth Optimization — the DP route
- Scheduling — where exchange-argument greedy proofs live
- Interval DP — the general framework for this problem shape