Optimal Binary Search tree Optimal binary search trees e.g. binary search trees for 3, 7, 9, 12; 3 7 3 7 9 7 12 3 12 9 9 9 12 3 7 12 (a) (b) (c) Algorithm Analysis and Design CS 007 BE CS 7th Semester (d) 2 Optimal binary search trees n identifiers : a1 <a2 <a3 <…< an Pi, 1in : the probability that ai is searched. Qi, 0in : the probability that x is searched where ai < x < ai+1 (a0=-, an+1=). n n P Q i 1 i i 1 i 1 Algorithm Analysis and Design CS 007 BE CS 7th Semester 3 10 5 14 4 E0 8 E1 E2 11 E3 E4 E7 12 E5 Identifiers : 4, 5, 8, 10, 11, 12, 14 Internal node : successful search, Pi External node : unsuccessful search, Qi E6 The expected cost of a binary tree: n n P level(a ) Q (level(E ) 1) n 1 i i n 0 i i The level of the root : 1 Algorithm Analysis and Design CS 007 BE CS 7th Semester 4 The dynamic programming approach Let C(i, j) denote the cost of an optimal binary search tree containing ai,…,aj . The cost of the optimal binary search tree with ak as its root : k 1 n C(1, n) min Pk Q0 Pi Qi C1, k 1 Q k Pi Qi Ck 1, n 1 k n i 1 i k 1 P1...Pk-1 Q0...Qk-1 a1...ak-1 ak Pk+1...Pn Qk...Qn ak+1...an Algorithm Analysis and Design CS 007 BE CS 7th Semester C(1,k-1) C(k+1,n) 5 General formula k 1 C(i, j) min Pk Qi-1 Pm Q m Ci, k 1 i k j m i j Q k Pm Q m Ck 1, j m k 1 j min Ci, k 1 Ck 1, j Qi-1 Pm Q m i k j m i P1...Pk-1 Q0...Qk-1 a1...ak-1 ak Pk+1...Pn Qk...Qn ak+1...an Algorithm Analysis and Design CS 007 BE CS 7th Semester C(1,k-1) C(k+1,n) 6 Computation relationships of subtrees e.g. n=4 C(1,4) C(1,3) C(1,2) C(2,4) C(2,3) C(3,4) Time complexity : O(n3) when j-i=m, there are (n-m) C(i, j)’s to compute. Each C(i, j) with j-i=m can be3computed in O(m) time. O( m(n m)) O(n ) 1Algorithm m n Analysis and Design CS 007 BE CS 7th Semester 7
© Copyright 2026 Paperzz