Course: 113-1 Introduction to Algorithms. Lecturer: Huilan Chang. 2024.11.28 Reference: Optimal Binary Search Trees (Section 3.5 in Foundations Of Algorithms by Neapolitan)

Preliminaries


Example.

Probability of being the search key is not uniform

Example. $n=3, p_1=0.7, p_2=0.2, p_3=0.1$, keys A<B<C. All binary search trees:

② Brute-force method is not efficient.

Dynamic Programming


<aside>

Definition. Let $A[i][j]$ be the average search time for an optimal binary search trees with keys $K_i$ through $K_j$.

</aside>

<aside>

🌴 🌴 🌴  Optimal Substructure 🌴 🌴 🌴  If $T$ be an optimal tree with keys $K_1$ through $K_n$ with root $K_k$, then its left subtree is an optimal tree with keys $K_1$ through $K_{k-1}$, and its right subtree is an optimal tree with keys $K_{k+1}$ through $K_n$.

</aside>

For $i=1,2,\cdots, n$, $A[i][i] = p_i$ $A[i][i-1]=A[i+1][i]=0$.

Compute an optimal solution in a bottom-up fashion.