0% found this document useful (0 votes)
45 views10 pages

Optimal Binary Search Tree: Kalyani C.Waghmare

The document discusses Optimal Binary Search Trees (OBST). It defines OBST as a binary search tree with the minimum average cost of searching. It provides formulas to calculate the total cost of a tree based on search probabilities and node levels. It describes using dynamic programming to efficiently find the OBST for a given set of search keys by calculating weights and costs of all possible subtrees and choosing the subtree with the minimum cost at each step to build the optimal tree.

Uploaded by

atharva soman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views10 pages

Optimal Binary Search Tree: Kalyani C.Waghmare

The document discusses Optimal Binary Search Trees (OBST). It defines OBST as a binary search tree with the minimum average cost of searching. It provides formulas to calculate the total cost of a tree based on search probabilities and node levels. It describes using dynamic programming to efficiently find the OBST for a given set of search keys by calculating weights and costs of all possible subtrees and choosing the subtree with the minimum cost at each step to build the optimal tree.

Uploaded by

atharva soman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Optimal Binary Search Tree

By:
Kalyani C.Waghmare
OBST Definition
OBST:- The Binary search Tree having minimum average
cost of searching.
Example - Consider input sequence of 3 identifier (do, if,
while) , what is its Optimal Binary Search Tree

do while

while do

if if
Fig.1Total cost = 1*1 + 2*1 + 3*1 = 6 Fig. 2 Total cost = 1*1 + 2*1 + 3*1 = 6

while do
if
if if
do while
do while
Fig. 5 Total cost = 1*1 + 2*2 = 5
Fig. 3 Fig. 4
Total cost = 1*1 + 2*1 +3*1 = 6 Total cost = 1*1 + 2*1 +3*1 = 6

Fig. 5 is OBST as it is having least average cost of searching


Formula to calculate cost of Tree
𝑛

𝑇𝑜𝑡𝑎𝑙 𝑐𝑜𝑠𝑡 = 𝐿𝑒𝑣𝑒𝑙𝑖 + 1


𝑖=1

• While constructing OBST we should consider how


many successful and unsuccessful searching's are
happening on data.

• BST is modified to extended BST


• In Extended BST Null links is represented as box
representing unsuccessful node
• Total cost of extended BST is calculated as follows

𝑇𝑜𝑡𝑎𝑙 𝑐𝑜𝑠𝑡 = 𝑛
𝑖=1 𝑃𝑖 ∗ 𝐿𝑒𝑣𝑒𝑙𝑖 + 𝑛
𝑖=0 𝑄𝑖 ∗ 𝐿𝑒𝑣𝑒𝑙𝑖

Where Pi is successful probability of ith identifier


Qi is unsuccessful probability of ith identifier
OBST continued..
• Example of extended BST Successful nodes

if

do while

Unsuccessful nodes Fig.6 Extended BST

• If we consider probability of successful search and


unsuccessful search same(1) then total cost of
• Fig. 1total cost = 1*1+2*2+3*2+ 4*2 = 19
• Fig. 2total cost = 1*1+2*2+3*2+ 4*2 = 19
• Fig. 3,4 total cost = 1*1+2*2+3*2+ 4*2 = 19
• Fig. 5total cost = 1*1+2*2+3*4 = 17
• By considering same probability of successful and unsuccessful
search total cost of fig. 5 is minimum and it is OBST.
• But if probability is different then answer may be different
OBST continued..
• Example consider 3 identifiers (do, if,while)
• P= { 0.1, 0.2, 0.4 }, Q = {0.05, 0.05,0.1,0.1}
• Fig. 1
total cost = 0.1*1+0.4*2+0.05*2 + 0.2*3+ 0.05*3 + 0.1*4
+0.1*4 = 2.55
• Fig. 2
total cost = 0.4*1+0.1*2+0.1*2 + 0.2*3+ 0.05*3 + 0.05*4
+0.1*4 = 2.15
• Fig. 3
total cost = 0.4*1+0.2*2+0.1*2 + 0.1*3+ 0.1*3 + 0.05*4
+0.05*4 = 2.00
• Fig. 4
total cost = 0.1*1+0.2*2+0.05*2 + 0.05*3+ 0.4*3 + 0.1*4
+0.1*4 = 2.75
• Fig. 5
total cost = 0.2*1+0.1*2+0.4*2 + 0.05*3+ 0.05*3 + 0.1*3
+0.1*3 = 2.10
Fig. 3 is OBST as it is having least average cost of searching
by considering probability of successful and unsuccessful
search
OBST
• It is very difficult, time consuming and impossible to
find all BSTs of combinations of given identifiers

• So we following steps to construct OBST

Let Tij is OBST having identifiers Ai+1,Ai+2…Aj


e.g. T04 is having identifiers (A1, A2, A3, A4)
Find weight of Tree varying I from 0 to n and j=0 to n

Wij= 𝑛𝑖=𝑖+1 𝑃𝑖 + 𝑛𝑖=0 𝑄𝑖


Wij= 0 𝑖𝑓 𝑖 == 𝑗
Find cost of Tree varying I from 0 to n and j=0 to n

𝐶𝑖𝑗 = 𝑊𝑖𝑗 +min{i<k>=j} 𝐶𝑖 𝑘 − 1 + 𝐶𝑘𝑗


=0 if i==j
Rij =k
Rij is root of Tij having cij is minimum
where left subtree will have Ti(k-1) and right subtree Tkj
Node structure
Class obst; //forward declaration
Class node{
int data;
node *lchild, *rchild;
friend class obst;
public:
node(int x)
{
data = x;
lchild = rchild = null;
}
}
class obst
{
class node *root;
public:
obst(){ root = null;}
void calculate_wt( double [], double [], int);
void create_tree(int,int)
};
OBST
• Procedure calculate_wt(double *p, double *q, int n)
• p is array of probability of successful search, q is array of unsuccessful
search, n is number of identifiers
for (int i = 0 ; i < n ; i++)
w[i][i] = q[i] ; r[i][i] = c[i][i] = 0; //calculate wts, costs of null tree and tree with one node
w[i][i+1] = q[i] + q[i+1] + p[i+1]; r[i][i+1] = i+1; c[i][i+1] = w[i][i+1];
End for
w[n][n] = q[n] ; r[n][n] = c[n][n] = 0;
for (m = 2 ; m <= n ; m++) //calculate wt and cost of tree having more than one node
for (int i = 0 ; i <= n-m ; i++)
Min = 999; j= i+m;
W[i][j] = w[i][j-1] +p[j] + q[j];
for (int i1 = i+1 ; i1<j ; i1++)
sum1 = c[i][i1-1] + c[i1][j];
if (sum1<min) min = sum1; k= i1; endif
End for
C[i][j] = w[i][j] + c[i][k-1] + c[k][j]
r[i][j] = k;
End for
Root = Create_tree(0,n)
Identifiers A1,A2,A3
p= {0.4, 0.1, 0.2 } q = {0.1,0.04,0.09,0.07}

0 1 2 3
0 W00=0.1 W11=0.04 W22=0.09 W33=0.07
C00=0 C00=0 C00=0 C00=0
R00=0 R00=0 R00=0 R00=0

1 W01=0.54 W12=0.23 W23=0.36


C01=0.54 C12=0.23 C23=0.36
R01=1 R12=2 R23=3

2 W02=0.73 W13=0.5
C02=0.96 C13=0.73
R02=1 R13=3

3 W03=1 A1
C03=1.93
R03=1 A3

A2
Create OBST
Node * Procedure create_tree(int i, int j)
If(i!=j)
k = r[i][j];
t = new node( input[k])
t->lc = create(i,k-1);
t->rc = create(k,j);
Return(t)
Else
Return(NULL);
Endif

You might also like