Professional Documents
Culture Documents
Optimal Binary Search Tree: Kalyani C.Waghmare
Optimal Binary Search Tree: Kalyani C.Waghmare
By:
Kalyani C.Waghmare
OBST Definition
OBST:- The Binary search Tree having minimum average
cost of searching.
Example - Consider input sequence of 3 identifier (do, if,
while) , what is its Optimal Binary Search Tree
do while
while do
if if
Fig.1Total cost = 1*1 + 2*1 + 3*1 = 6 Fig. 2 Total cost = 1*1 + 2*1 + 3*1 = 6
while do
if
if if
do while
do while
Fig. 5 Total cost = 1*1 + 2*2 = 5
Fig. 3 Fig. 4
Total cost = 1*1 + 2*1 +3*1 = 6 Total cost = 1*1 + 2*1 +3*1 = 6
𝑇𝑜𝑡𝑎𝑙 𝑐𝑜𝑠𝑡 = 𝑛
𝑖=1 𝑃𝑖 ∗ 𝐿𝑒𝑣𝑒𝑙𝑖 + 𝑛
𝑖=0 𝑄𝑖 ∗ 𝐿𝑒𝑣𝑒𝑙𝑖
if
do while
0 1 2 3
0 W00=0.1 W11=0.04 W22=0.09 W33=0.07
C00=0 C00=0 C00=0 C00=0
R00=0 R00=0 R00=0 R00=0
2 W02=0.73 W13=0.5
C02=0.96 C13=0.73
R02=1 R13=3
3 W03=1 A1
C03=1.93
R03=1 A3
A2
Create OBST
Node * Procedure create_tree(int i, int j)
If(i!=j)
k = r[i][j];
t = new node( input[k])
t->lc = create(i,k-1);
t->rc = create(k,j);
Return(t)
Else
Return(NULL);
Endif