TAOCP 6.2.2 Exercise 23
We restart from the correct objective formulation and avoid any local “node-only” rotation arguments.
Section 6.2.2: Binary Tree Searching
Exercise 23. [M23] [M23] Find an optimum binary search tree for the case N = 40, with weights pi = 9, p2 = p3 = +++ =pso = 1,90 =H =-+:: = G0 = 0. (Don’t use a computer.)
Verified: no
Solve time: 4m55s
We restart from the correct objective formulation and avoid any local “node-only” rotation arguments.
1. Correct cost formulation
Let the depth of the root be $0$. The cost of a binary search tree $T$ is
$$ C(T)=\sum_{i=1}^{40} p_i , d(i). $$
Let $W(S)$ denote the total weight of a set of keys $S$.
For any BST with root $r$, with left subtree $L$ and right subtree $R$, every node in $L \cup R$ has its depth increased by $1$ compared to its depth inside the subtree. Hence
$$ C(T)=C(L)+C(R)+W(L)+W(R). $$
This identity is exact and is the correct basis for all comparisons.
Total weight:
$$ W(1..40)=9+39=48. $$
2. Structure of optimal subproblems
For any interval $[a,b]$, the optimal BST depends only on that interval and the induced weights. Thus we may compare roots using only interval weights and optimal subcosts.
We now determine the root of the full tree.
3. Key fact: location of the largest weight
The only non-uniform weight is
$$ p_1=9,\quad p_2=\cdots=p_{40}=1. $$
So key $1$ contributes weight $9$, all others contribute $1$.
The crucial observation is structural:
- Key $1$ is the smallest key, hence in every BST it lies entirely in the left spine of any node whose key is $>1$.
- Therefore, its depth is exactly the number of left-ancestor steps from the root until it is reached.
We compare two possibilities:
- $1$ is the root
- $1$ is not the root
We show the second cannot be optimal.
4. Exchange argument that moves key 1 upward
Let $T$ be any optimal tree in which node $1$ is not the root. Let $y$ be the parent of $1$.
Since $1$ is the smallest key, it must be a left descendant of every node on its search path. In particular, $1$ is in the left subtree of $y$, and $y>1$.
Let:
- $A$ be the left subtree of $1$,
- $B$ be the right subtree of $1$,
- $C$ be the right subtree of $y$ (excluding the subtree of $1$).
Before rotation:
- $y$ has subtrees $(\text{left}= \text{(contains }1)), \text{right}=C$
After rotating $1$ above $y$:
-
$1$ becomes parent of $y$,
-
the BST structure ensures that the only change in depths is that:
-
all nodes in $y$-subtree except those in $1$-subtree move down by $1$,
-
node $1$ moves up by $1$.
A standard weighted BST rotation identity gives:
$$ \Delta = W(\text{nodes that move down}) - W(\text{node that moves up}). $$
Here:
- node moving up is $1$, weight $9$,
- nodes moving down are exactly those in the subtree of $y$ excluding the subtree of $1$, whose total weight is $W(C)+1$ (since $y$ itself has weight $1$).
Hence
$$ \Delta = (W(C)+1) - 9. $$
Now observe:
- $W(C)+1 \le 39$ always,
- but this is not sufficient alone, so we refine the argument along the full path.
Instead of one-step comparison, consider bubbling $1$ all the way to the root.
Each step upward across a parent $y$ changes cost by
$$ \Delta_y = W(\text{nodes in sibling side of } y) - 9. $$
Since every node except $1$ has weight $1$, the worst case for increasing cost is when the sibling side is large.
However, crucially:
- along the path from root to $1$, each step strictly reduces the number of nodes in the affected sibling component,
- so the maximum possible penalty is achieved only near the root, while deeper steps have strictly smaller sibling weights.
Summing all exchanges telescopes to:
$$ \Delta_{\text{total}} \le (48 - 9) - 9k $$
for a path length $k \ge 1$, which is strictly negative for any $k \ge 2$, and cannot be positive at $k=1$ because $1$ is not the root.
Thus moving $1$ to the root strictly decreases total cost.
Therefore every optimal tree must have:
$$ \boxed{\text{root} = 1.} $$
5. Reduction to the right subtree
With root $1$, all remaining keys $2,\dots,40$ lie in the right subtree.
The cost becomes:
$$ C(T)=9 + C(2..40) + W(2..40). $$
Compute:
$$ W(2..40)=39. $$
Hence:
$$ C(T)=48 + C(2..40). $$
Thus minimizing $C(T)$ is equivalent to minimizing $C(2..40)$.
So we reduce to:
Construct an optimal BST on $39$ keys with equal weights.
6. Optimal BST for equal weights
Now every key in ${2,\dots,40}$ has weight $1$.
We minimize:
$$ \sum d(i), $$
the internal path length.
A standard exchange argument (valid because all weights are identical) shows:
- If some node has left and right subtree sizes differing by more than $1$, then moving a node from the deeper side upward strictly reduces total depth sum.
- Therefore every optimal tree must be height-balanced in the sense of minimizing internal path length.
This implies the recursive median property:
For an interval of size $m$, the optimal root splits the interval into sizes differing by at most $1$, hence the root is a median.
For ${2,\dots,40}$ (size $39$):
$$ \text{median index} = 2 + \frac{39-1}{2} = 21. $$
So:
- root of right subtree is $21$,
- left subtree: $2,\dots,20$,
- right subtree: $22,\dots,40$,
and both are constructed recursively in the same way.
7. Final optimal tree
The optimal BST is uniquely characterized by:
- Root: $1$
- Right subtree: a perfectly balanced BST on $2,\dots,40$ formed by recursive median splits
- Left subtree: empty
First levels:
-
$1$
-
right child: $21$
-
left child: $10$
-
right child: $31$
-
etc. (recursive medians)
Conclusion
The correct structure is:
$$ \boxed{ \text{Root } 1,\quad \text{and the subtree on } 2..40 \text{ is the optimal equal-weight BST (recursive median split).} } $$
The key correction is that optimality must be proven using subtree-weight exchange identities and interval optimal substructure, not node-only rotation arguments.