Optimal Binary Search Tree Optimal(理想) + Binary (二元 ) + Search (搜尋) + Tree (樹) = OBST Preface of OBST It is one special kind of advanced tree. It focus on how to reduce the cost of the search of the BST. It may not have the lowest height. It needs 3 tables to record. Purpose In order to promote the efficiency of the search , we can let the cost of the search minimum or mean the average of compare minimum It has n keys in sorted order (representation k1,k2,…,kn ) ( k1<k2<…<kn) some searches may be for values not in ki, so we also have n+1 “dummy keys” d0,d1,…,dn representating not in ki. d0 = all values < k1. dn = all values > kn. APPLE Bird Cat Dog Egg Fish dummy keys (d0,d1,…,dn) Keys Grape (k1,k2,..kn) For each key ki, a pi which means the probability searching for ki. For each key di, a qi which means the probability searching for di. elements kn = key pn = probability of searching kn dn = dummy key qn = probability of searching dn Useful statement Success probability = (i=1~n) ∑ pi Failure probability = (i=0~n) ∑ qi (i=1~n) ∑ pi + (i=0~n) ∑ qi = 1 Success cost = (i=1~n) ∑ pi * (depth(ki)+1) Failure cost= (i=0~n) ∑ qi * (depth(di)+1) E[search cost in ] = Success+ Failure =(i=1~n) ∑ pi * (depth(ki)+1) +(i=0~n) ∑ qi * (depth(di)+1) = (i=1~n) ∑ pi+(i=0~n) ∑ qi +(i=1~n) ∑ pi * depth(ki) +(i=0~n) ∑ qi * depth(di) = 1+(i=1~n) ∑ pi * depth(ki) +(i=0~n) ∑ qi * depth(di) Example#1 K2 K2 K1 d0 d0 d1 0 1 d1 K5 K3 d2 i K1 K4 d3 d4 2 3 d5 4 K5 K3 d4 5 pi 0.15 0.1 0.05 0.1 0.2 qi 0.05 0.1 0.05 0.05 0.05 0.1 d2 d5 K4 d3 Cost=Probability * (Depth+1) K2 K4 K1 d0 d1 K3 K5 d2 d3 d4 d5 i pi qi 0 0.05 1 0.15 0.1 2 0.1 0.05 3 0.05 0.05 4 0.1 0.05 5 0.2 0.1 K1=2*0.15=0.3 K2=1*0.1 =0.1 K3=3*0.05=0.15 K4=2*0.1 =0.2 K5=3*0.2 =0.6 d0=3*0.05 =0.15 d1=3*0.1 =0.3 d2=4*0.05 =0.2 d3=4*0.05 =0.2 d4=4*0.05 =0.2 d5=4*0.1 =0.4 all cost=2.8 K2 K1 Cost=Probability * (Depth+1) K5 d0 d1 K4 d5 K3 d4 d2 d3 i pi qi 0 1 2 3 4 5 0.05 0.15 0.1 0.1 0.05 0.05 0.05 0.1 0.05 0.2 0.1 K1=2*0.15 K2=1*0.1 K3=4*0.05 K4=3*0.1 K5=2*0.2 d0=3*0.05 d1=3*0.1 d2=5*0.05 d3=5*0.05 d4=4*0.05 d5=3*0.1 =0.3 =0.1 =0.2 =0.3 =0.4 =0.15 =0.3 =0.25 =0.25 =0.2 =0.3 all cost=2.75 Picture#1=2.8 Picture#2=2.75 SO Picture#1 cost more than Picture#2 Picture#2 is better. The depth of Picture#1 is 3 The depth of Picture#2 is 4