Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views43 pages

Optimal Binary Search Tree

Uploaded by

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

Optimal Binary Search Tree

Uploaded by

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

OBST

Step 1: write key values starting from k0


Step 1: write key values starting from k0

Step 2: Add column values except 1st column as


one value is missing
Step 3:
• Calculate n = total number of keys given e.g.
here it is n=6
• Create probability matrix w = nxn
• Create Expected matrix e = nxn
• Create root matrix R
• Finally create OBST
Part A : Creating matrix w :
probability Matrix
Part A : Create probability matrix w = nxn
1. Write row numbers: from 1 to n // as row
starting from 1

2. Write column numbers: from 0 to n // as


row starting from 1
3. Write probability qi diagonally
4. Mark position below
diagonal as Blank or -
5. Proceed by adding elements diagonally-

0.25+0.05
=0.30
0.15+0.10
=0.25
5. Proceed by adding elements diagonally-

0.25+0.05
=0.30
0.15+0.10
=0.25
0.1+0.05
=0.15
0.15+0.05
=0.20
0.3+0.05
=0.35
0.25+0.0
5 =0.30
0.15+0.1
0 =0.25
0.1+0.05
=0.15
0.15+0.0
5 =0.20
0.3+0.05
=0.35
6. Now for second diagonal start adding from
second probability in qi and previous calculated Diagonal

0.25+0.0 0.15+0.30
5 =0.30 =0.45
0.15+0.1 0.1+0.25
0 =0.25 =0.35
0.1+0.05 0.15+0.15
=0.15 =0.30
0.15+0.0 0.3+0.20
5 =0.20 =0.50
0.3+0.05
=0.35
6. Now for second diagonal start adding from
second probability in qi and previous calculated Diagonal

0.25+0.0 0.15+0.30
5 =0.30 =0.45
0.15+0.1 0.1+0.25
0 =0.25 =0.35
0.1+0.05 0.15+0.15
=0.15 =0.30
0.15+0.0 0.3+0.20
5 =0.20 =0.50
0.3+0.05
=0.35
7. Now for third diagonal start adding from third
probability in qi and previous calculated Diagonal

0.25+0.0 0.15+0.30 0.1+0.45


=0.45 =0.55
5 =0.30
0.15+0.1 0.1+0.25 0.15+0.35
0 =0.25 =0.35 =0.5

0.1+0.05 0.15+0.15 0.3+0.30


=0.30 =0.6
=0.15
0.15+0.0 0.3+0.20
5 =0.20 =0.50
0.3+0.05
=0.35
7. Now for third diagonal start adding from third
probability in qi and previous calculated Diagonal

0.25+0.0 0.15+0.30 0.1+0.45


=0.45 =0.55
5 =0.30
0.15+0.1 0.1+0.25 0.15+0.35
0 =0.25 =0.35 =0.5

0.1+0.05 0.15+0.15 0.3+0.30


=0.30 =0.6
=0.15
0.15+0.0 0.3+0.20
5 =0.20 =0.50
0.3+0.05
=0.35
7. Now for fourth diagonal start adding from fourth
probability in qi and previous calculated Diagonal

0.1+0.45 0.15+0.55
0.25+0.05 0.15+0.30 =0.7
=0.55
=0.30 =0.45

0.3+0.5 =0.8
0.15+0.10 0.1+0.25 0.15+0.35
=0.25 =0.35 =0.5

0.1+0.05 0.15+0.15
=0.15 0.3+0.30 =0.6
=0.30

0.15+0.0 0.3+0.20
5 =0.20 =0.50

0.3+0.05
=0.35
8. Now for fifth diagonal start adding from fifth
probability in qi and previous calculated Diagonal

0.1+0.45 0.15+0.55 0.3+0.7 =1


0.25+0.05 0.15+0.30 =0.7
=0.55
=0.30 =0.45

0.15+0.10 0.1+0.25 0.15+0.35 0.3+0.5 =0.8


=0.25 =0.35 =0.5

0.1+0.05 0.15+0.15
=0.15 0.3+0.30 =0.6
=0.30

0.15+0.0 0.3+0.20
5 =0.20 =0.50

0.3+0.05
=0.35

Highest value of calculation will be always 1


Part B : Creating matrix e :
Expectation Matrix
In root matrix R mark row and column subscript
from 1 to n-1
Fill the diagonal values
Mark – below the diagonal
1. For calculating e we make use of corresponding
row and column value and add them with the value
of matix w
To calculate [1,1]
Add row ( 0.05)and
column(0.10)=0.25
Add above and to [1,1]
value of w
1. For calculating e we make use of corresponding
row and column value and add them with the value
of matix w
To calculate [1,1]
Add row ( 0.05)and
column(0.10)=0.25
Add above ans to [1,1]
value of w
0.25+0.30 = 0.45
1. For calculating e we make use of corresponding
row and column value and add them with the value
of matix w 0.45
To calculate [2,2]
Add row ( 0.10)and
column(0.05)=0.15
Add above ans to [2,2]
value of w
0.15+0.25 = 0.4
value of row and column addition and then add the
minimum value to corresponding value of matrix w.
For [1,2] row values are 0.05. 0.45
Row / column addition min
For [1,2] column values are 0.4. 0.05
value
0.05+0.4 = 0.45

0.45+0.05 = 0.5

minimum is 0.45

now add min with [1,2] value of w

[1,2] value of w is
value of row and column addition and then add the
minimum value to corresponding value of matrix w.
Row / column addition min value
For [1,2] row values are 0.05. 0.45
0.05+0.4 = 0.45
For [1,2] column values are 0.4. 0.05
0.9
0.45+0.05 = 0.5

minimum is 0.45

now add min with [1,2] value of w

[1,2] value of w is 0.45

0.45+0.45 = 0.9
At this point make entry in root
matrix also
in matrix r
value
0.05+0.4 = 0.45
For [1,2] row values are 0.05. 0.45
0.45+0.05 = 0.5
For [1,2] column values are 0.4. 0.05 0.9

minimum is 0.45

Row value are 0.05, 0.45

0.05 – value 1

0.45- value 2

0.45 you got from row value present at index position 1

so enter 1 in [1,2] of r matrix


value of row and column addition and then add the
minimum value to corresponding value of matrix w.
For [2,3] row values are 0.10. 0.4
Row / column addition min value
For [2,3] column values are 0.25. 0.05
0.9
0.10+0.25 = 0.35

0.4+0.05 = 0.45 minimum is 0.35

now add min with [2,3] value of w

[2,3] value of w is 0.35

0.35+0.35 = 0.7

So e [2,3] = 0.7
value of row and column addition and then add the
minimum value to corresponding value of matrix w.
For [2,3] row values are 0.10. 0.4
Row / column addition min value
For [2,3] column values are 0.25. 0.05
0.9
0.10+0.25 = 0.35
0.7
0.4+0.05 = 0.45 minimum is 0.35

now add min with [2,3] value of w

[2,3] value of w is 0.35

0.35+0.35 = 0.7

So e [2,3] = 0.7
At this point make entry in root
matrix also
value of row and column addition and then add the
minimum value to corresponding value of matrix w.
For [2,3] row values are 0.10. 0.4

Row
For [2,3] /column
column addition
values are 0.25. 0.05
0.9
min value 0.7

0.10+0.25 = 0.35

0.4+0.05 = 0.45

minimum is 0.35

0.35 you got from row value 1, but value

present at index position 2 hence we write

2 in row matrix
value of row and column addition and then add the
minimum value to corresponding value of matrix w.
Row
For [1,3] /row
column addition min value
values are 0.05,0.45,0.9

For [1,3] =0.75


0.05+0.7 column values are 0.7,0.25,0.05
0.7
0.45+0.25 = 0.7

0.9+0.05 = 0.95 minimum is 0.7

now add min with [1,3] value of w

[1,3] value of w is 0.55

0.7+0.55 = 1.25
At this point make entry in root
matrix also
value of row and column addition and then add the
minimum value to corresponding value of matrix w.
For [1,3] row values are 0.05,0.45,0.9

For [1,3] column values are 0.7,0.25,0.05

Row / column addition min value


0.05+0.7 =0.75

0.45+0.25 = 0.7

0.9+0.05 = 0.95 minimum is 0.7

0.7 you got from row value present at index position 2,

hence we write 2 in row matrix


Now to construct tree we have key values from 1 to 5
Decide root : first row last column of r matrix . Here we
have value 2. So key 2 is the root node.
So as it is a binary tree , it will 2 child left and right.

Now calculate the index position of left and right child at


insert the node value as the corresponding value of matrix
r
So as it is a binary tree , it will 2 child left and right.

Now calculate the index position of left and right child at


insert the node value as the corresponding value of matrix
r . As 5 is the highest key value it will not have any right
child

You might also like