Part A: try every root
For keys with frequencies , the best tree has key 2 (frequency 6) at the root, key 0
under it on the left with key 1 below that, and key 3 on the right. That costs
. Every other tree costs more, which the
dynamic program below checks without listing them all.
Part B: popular is not the same as central
With frequencies the popular-root rule puts key 0 at the root and the other three
in a chain to its right: . A tree rooted at key 1 costs 74, so the rule
pays times as much. A root pushes everything on its side one level deeper, so
it should split the weight, not just be heavy. With 4 keys the worst the rule can do is
: equal frequencies, where its tie-breaking builds a chain.
Part C: the interval dynamic program
Let be the cost of the best tree on keys , and their total
frequency. If key is the root, both subtrees hang one level lower, and every key in the range
pays one more comparison:
Fill the table by increasing range length and remember the best for each range. Then rebuild
the tree from downward. That is ranges with choices each, in
all. Returning the tree, not just the number, makes the answer checkable: the grader rebuilds
it, recomputes its cost and compares it with the optimum.
Three rules that sound reasonable fail the grader:
- The popular root fails as soon as frequencies are equal.
- The middle key ignores the frequencies.
- Splitting the total frequency evenly comes close, but not exactly: about 8% over the best
on the grader's 40 random keys.
Going further
- Knuth's speed-up. The best roots are monotone:
.
Searching only that window makes the whole table ; the grader uses it. The same idea (the quadrangle inequality) speeds up many interval DPs.
- Near-optimal in linear time. Choosing the root that splits the weight most evenly is within
a small additive term of optimal (Mehlhorn), which is why part C's third rule is close.
- Without knowing the frequencies, splay trees are within a constant factor of the best static
tree on every access sequence (Sleator and Tarjan's static optimality theorem). See
Splay Trees.
References
- D. E. Knuth. Optimum binary search trees. Acta Informatica 1, 1971.
- K. Mehlhorn. Nearly optimal binary search trees. Acta Informatica 5, 1975.
- D. D. Sleator, R. E. Tarjan. Self-adjusting binary search trees. Journal of the ACM 32(3),
1985.