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) 7 -6

(b)

(c)

(d)

Optimal binary search trees  n identifiers : x1
where xi < x < xi+1 (x0=-, xn+1=). n

n

 P  Q i 1

7 -7

i

i 0

i

1

10

 Identifiers : 4, 5, 8, 10, 11, 12, 14  Internal node : successful search,

5

14

4

E0

8

E1

E2

11

E3

E4

E7

12

E5



Pi  External node : unsuccessful search, Qi

E6

The expected cost of a binary tree: n

n

 P  level(x )  1   Q  (level(E )) i 1

 7 -8

i

i

i 0

The level of the root : 0

i

i

 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  Pm  Qm   C1,k 1  Qk  Pm  Qm   Ck 1,n 1kn m1 mk1     

P1...Pk-1 Q0...Qk-1

7 -9

ak

Pk+1...Pn Qk...Qn

a1...ak-1

ak+1...an

C(1,k-1)

C(k+1,n)

General formula k 1    C(i, j)  min  Pk  Q i-1   Pm  Q m   Ci, k  1 ik j m i    j    Q k   Pm  Q m   Ck  1, j  m  k 1   j    min C i, k  1  Ck  1, j  Qi-1   Pm  Q m  ik j m i  

P i...P k-1 Qi-1...Q k-1

7 -10

ak

P k+1...P j Qk...Q j

ai...ak-1

ak+1...aj

C(i,k-1)

C(k+1,j)

Time complexity  Time complexity : O(n3)

(n-m) C(i, j)’s are computed when j-i = m.  Each C(i, j) with j-i = m can be computed in O(m) time. 

O(



m(n  m) )  O(n 3 )

1 m  n

7 -11

Example

Dr Vijendra

Example

Dr Vijendra

Example

Dr Vijendra

Optimal binary search trees -

Optimal binary search trees. ❖ n identifiers : x. 1. <x. 2. <x. 3. <…< x n. ❖ P i. , 1≤i≤n : the probability that x i is searched. ❖ Q i. , 0≤i≤n : the probability that x is ...

365KB Sizes 0 Downloads 250 Views

Recommend Documents

No documents