Module - 4 - Part4
Module - 4 - Part4
40
20 60
10 30
50 70
20
10
Binary Search Tree
Now if along with the keys their is also frequency of search defined, then we need to find
the optimal tree structure for the given frequency.
E.g. Keys 10 20 30
Frequency 3 5 7
Index 1 2 3 4
Keys 10 20 30 40
Frequency 4 2 6 3
Optimal Binary Search Tree
j
0 1 2 3 4
0
0 4 81 203 263
1
0 2 103 163
i
2
0 6 123
3
0 3
4
0
Optimal Binary Search Tree
Step 1: Set all diagonal elements C[0,0] — C[4,4] = 0
Step 2: Fill all the diagonal elements j - i = 1 i.e. C[0,1], C[1,2], C[2,3], C[3,4]
Step 3: Fill all the diagonal elements j - i = 2 i.e. C[0,2], C[1,3], C[2,4]
= 0 + 2 + (4 +2) = 8
= 4 + 0 + (4 + 2) = 10
Creating the
optimal BST
Optimal Binary Search Tree
Time Complexity calculation is similar to Matrix Chain multiplication and time
complexity O(n^3)