o ptimal b inary s earch t ree

14
Optimal Binary Search Tree

Upload: grant-mcfarland

Post on 30-Dec-2015

31 views

Category:

Documents


0 download

DESCRIPTION

O ptimal B inary S earch T ree. O ptimal ( 理想 ). +. B inary ( 二元 ). OBST. +. =. S earch ( 搜尋 ). +. T ree ( 樹 ). 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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: O ptimal  B inary  S earch  T ree

Optimal Binary

Search Tree

Page 2: O ptimal  B inary  S earch  T ree

Optimal( 理想 )

Binary ( 二元 )

Search ( 搜尋 )

Tree ( 樹 )

OBST

+

+

+

=

Page 3: O ptimal  B inary  S earch  T ree

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.

Page 4: O ptimal  B inary  S earch  T ree

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

Page 5: O ptimal  B inary  S earch  T ree

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.

Page 6: O ptimal  B inary  S earch  T ree

APPLE

Bird

Cat Dog

Egg

Fish Grape

dummy keys (d0,d1,…,dn)

Keys(k1,k2,..kn)

Page 7: O ptimal  B inary  S earch  T ree

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.

Page 8: O ptimal  B inary  S earch  T ree

elements kn = key

pn = probability of searching kn

dn = dummy key

qn = probability of searching dn

Page 9: O ptimal  B inary  S earch  T ree

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)

Useful statement

Page 10: O ptimal  B inary  S earch  T ree

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)

Page 11: O ptimal  B inary  S earch  T ree

Example#1

i 0 1 2 3 4 5

pi   0.15

0.10.05

0.10.2

qi0.05

0.1

0.05

0.05

0.05

0.1

K1 K1

K2 K2

K3

K3

K4

K5K4

K5d0d0

d1

d2 d3 d4 d5

d1

d2 d3

d4

d5

Page 12: O ptimal  B inary  S earch  T ree

K2

d0

i 0 1 2 3 4 5

pi   0.15

0.10.05

0.1 0.2

qi0.05

0.10.05

0.05

0.05

0.1

K1=2*0.15=0.3K2=1*0.1 =0.1K3=3*0.05=0.15

K4=2*0.1 =0.2K5=3*0.2 =0.6d0=3*0.05=0.15

d1=3*0.1 =0.3d2=4*0.05=0.2d3=4*0.05=0.2d4=4*0.05=0.2d5=4*0.1 =0.4

K4K1

K3 K5d1

d2 d5d4d3

Cost=Probability * (Depth+1)

all cost=2.8

Page 13: O ptimal  B inary  S earch  T ree

i 0 1 2 3 4 5

pi   0.15

0.10.05

0.1 0.2

qi0.05

0.10.05

0.05

0.05

0.1

K1=2*0.15=0.3K2=1*0.1 =0.1K3=4*0.05=0.2K4=3*0.1 =0.3K5=2*0.2 =0.4d0=3*0.05=0.15

d1=3*0.1 =0.3d2=5*0.05=0.25

d3=5*0.05=0.25

d4=4*0.05=0.2d5=3*0.1 =0.3

Cost=Probability * (Depth+1)

K1

K2

K5

K4

K3

d0 d1 d5

d4

d2 d3

all cost=2.75

Page 14: O ptimal  B inary  S earch  T ree

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