Optimal Binary Search Tree
Optimal Binary Search Tree
Key Points
Dynamic Algorithms
dkrk
dk
O(n)
O(nn) algorithm
2O(n)
Proof
If a sub-tree of an optimal tree is not optimal,
then a better search tree will be produced if the sub-tree
is replaced by an optimal tree.
A B C D E F G H I J K L M N O P ..
23 10 8 12 30 5 14 18 20 2
4 11 7 22 22 10 ..
Key Problem
Which key should be placed at the root?
If we can determine this, we can ask the same
question for the left and right subtrees.
4 11 7 22 22 10 ..
8x1 + 12x2 = 32
D
C
Min
8x2 + 12x1 = 28
Cjj
Costs of
one-element trees
Below diagonal
C[j,k]
Costs of best tree
j to k
Cost of best tree
C-G
Zero
x
Below diagonal
C[j-1,j]
Costs of best
2-element trees
j-1 to j
Below diagonal
best[j-1,j]
Root of best
2-element trees
j-1 to j
4 11 7 22 22 10 ..
Next slide
We already
know this is
best for C,D
and stored
its cost
Root = C
Root = D
D
C
B
B
C
Best B,C
Costs
Roots
Choose B as root
A-A is in C[0,0]
Best C-D is known
Choose C as root
A-B is in C[0,1]
D is in C[3,3]
Choose D as root
A-C is in C[0,2]
Use 0 in C[4,3] for right
Final
cost
best[0,n-1]
If r0 = best[0,n-1],
Left subtree root is
best[0,r0-1],
best[r0+1,n-1]
Root = E
H
D
G
F
I
J
O(k2)
n
k2 = O(n3)
k =1