0% found this document useful (0 votes)
7 views

Optimal Binary Search Tree

The document discusses optimal binary search trees (Optimal BST), which are designed to minimize search time by ensuring that nodes in the left subtree have lesser values than the root and those in the right subtree have greater values. It explains the calculation of search costs based on tree structure and node frequencies, illustrating the dynamic programming approach to determine the minimum search cost for various configurations of keys. Examples are provided to demonstrate how different arrangements of keys affect the overall search efficiency.

Uploaded by

labbie2024
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Optimal Binary Search Tree

The document discusses optimal binary search trees (Optimal BST), which are designed to minimize search time by ensuring that nodes in the left subtree have lesser values than the root and those in the right subtree have greater values. It explains the calculation of search costs based on tree structure and node frequencies, illustrating the dynamic programming approach to determine the minimum search cost for various configurations of keys. Examples are provided to demonstrate how different arrangements of keys affect the overall search efficiency.

Uploaded by

labbie2024
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Optimal Binary Search

Tree
Binary search tree, the nodes in the left subtree have lesser value than
the root node and
the nodes in the right subtree have greater value than the root node.
The optimal binary search tree (Optimal BST) is also known as a weight-
balanced search tree.
It is a binary search tree that provides the shortest possible search time
or expected search time.
An optimal binary search tree is helpful in dictionary search.
Search Cost and Tree Structure
The cost of searching in a binary search tree depends on the structure of
the tree and the number of comparisons required to reach the desired
identifier.
The overall cost of searching a node should be less.
Let's understand through an example.

If the keys are 10, 20, 30, 40, 50, 60, 70

The maximum time required to search a node is


equal to the minimum height of the tree, equal to
log(n)
If we have keys: 15, 25, 35, then see how many BST can be formed. We
will use the formula given below:
= 2n
Cn​/ (n+1)

= 6C3/(3+1)
=20/4
=5​
So we can see that 5 trees will be formed as shown in the figure given
below.
In this case, the total number of comparisons is less as compared to others
because the height of the tree is less. So we can say that it’s a balanced binary
Let's assume that frequencies associated with the keys 15,
25, 35 are 3, 2, 5.

=1 x3 +2 x 2+ 3 x 5=22 =1 x3 +2 x 5+ 3 x =1 x5 +2 x 3+ 3 x
2=19 2=17

=1 x2 +2 x 3+ 2x =1 x5 +2 x 2+ 3 x
5=18 3=18
Dynamic Approach
Consider the below table, which contains the keys and
frequencies.

First, we will calculate the values where j-i is equal


to zero.
When i=0, j=0, then j-i = 0
When i = 1, j=1, then j-i = 0
When i = 2, j=2, then j-i = 0
When i = 3, j=3, then j-i = 0
When i = 4, j=4, then j-i = 0
Therefore, c[0, 0] = 0, c[1 , 1] = 0, c[2,2] = 0, c[3,3] = 0,
c[4,4] = 0
Now we will calculate the values where j-i equal
to 1.
When j=1, i=0 then j-i = 1
When j=2, i=1 then j-i = 1
When j=3, i=2 then j-i = 1
When j=4, i=3 then j-i = 1
Now to calculate the cost, we will consider only the jth
value.
The cost of c[0,1] is 4 (The key is 10, and the cost
corresponding to key 10 is 4).
The cost of c[1,2] is 2 (The key is 20, and the cost
corresponding to key 20 is 2).
The cost of c[2,3] is 6 (The key is 30, and the cost
corresponding to key 30 is 6)
The cost of c[3,4] is 3 (The key is 40, and the cost
corresponding to key 40 is 3)
Now we will calculate the values where j-i equal
to 1.
When j=1, i=0 then j-i = 1
When j=2, i=1 then j-i = 1
When j=3, i=2 then j-i = 1
When j=4, i=3 then j-i = 1
Now to calculate the cost, we will consider only the jth
value.
The cost of c[0,1] is 4 (The key is 10, and the cost
corresponding to key 10 is 4).
The cost of c[1,2] is 2 (The key is 20, and the cost
corresponding to key 20 is 2).
The cost of c[2,3] is 6 (The key is 30, and the cost
corresponding to key 30 is 6)
The cost of c[3,4] is 3 (The key is 40, and the cost
corresponding to key 40 is 3)
Now we will calculate the values where j-i =
2
When j=2, i=0 then j-i = 2
When j=3, i=1 then j-i = 2
When j=4, i=2 then j-i = 2
In this case, we will consider two keys.
When i=0 and j=2, then keys 10 and 20.
There are two possible trees that can be made out
from these two keys shown below:
• In the first binary tree, cost would be: 1 *4+ 2*2 = 8
• In the second binary tree, cost would be: 2*4 + 1*2 =
10
• The minimum cost is 8; therefore, c[0,2] = 8
Now we will calculate the values where j-i =
2
When j=2, i=0 then j-i = 2
When j=3, i=1 then j-i = 2
When j=4, i=2 then j-i = 2
In this case, we will consider two keys.
When i=0 and j=2, then keys 10 and 20.
There are two possible trees that can be made out
from these two keys shown below:
• In the first binary tree, cost would be: 1 *4+ 2*2 = 8
• In the second binary tree, cost would be: 2*4 + 1*2 =
10
• The minimum cost is 8; therefore, c[0,2] = 8
When i=1 and j=3, then keys 20 and 30.
There are two possible trees that can be made out from
these two keys shown below:
In the first binary tree, cost would be: 1*2 + 2*6 = 14
In the second binary tree, cost would be: 1*6 + 2*2 =
10
The minimum cost is 10; therefore, c[1,3] = 10
When i=2 and j=4, we will consider the keys at 3 and 4,
i.e., 30 and 40.
There are two possible trees that can be made out from
these two keys shown as below:
In the first binary tree, cost would be: 1*6 + 2*3 = 12
In the second binary tree, cost would be: 1*3 + 2*6 =
15
The minimum cost is 12, therefore, c[2,4] = 12
When i=1 and j=3, then keys 20 and 30.
There are two possible trees that can be made out from
these two keys shown below:
In the first binary tree, cost would be: 1*2 + 2*6 = 14
In the second binary tree, cost would be: 1*6 + 2*2 =
10
The minimum cost is 10; therefore, c[1,3] = 10
When i=2 and j=4, we will consider the keys at 3 and 4,
i.e., 30 and 40.
There are two possible trees that can be made out from
these two keys shown as below:
In the first binary tree, cost would be: 1*6 + 2*3 = 12
In the second binary tree, cost would be: 1*3 + 2*6 =
15
The minimum cost is 12, therefore, c[2,4] = 12
Now we will calculate the values when j-i = 3
When j=3, i=0 then j-i = 3
When j=4, i=1 then j-i = 3
When i=0, j=3 then we will consider three keys, i.e., 10, 20,
and 30.
The following are the trees that can be made if 10 is
considered as a root node.
Cost would be: 1*2 + 4*2 + 6*2 = 2
Cost would be: 1*4 + 2*2 + 3*6 = Cost would be: 1*4 + 2*6 + 3*2 = 22
26

Cost would be: 1*6 + 2*2 + 3*4 = 22 Cost would be: 1*6 + 2*4 + 3*2 = 20
Therefore, the minimum cost is 20 which is the 3rd root. So, c[0,3] is equal to 20.
It can also be done using the formula
C[0,3]=
w(0,3)=
w(0,3)=f(1)+f(2)+f(3)
w(0,3)=4+2+6=12
C[0,3]= Node 10 as key Node 20 as key Node 30 as key
since 1 is the key than c[0,1-1] + c[1,j] + w(0,3)
c[0,0] + c[1,3] + w(0,3)
since 2 is the key than c[0,2-1] + c[2,j] + w(0,3)
c[0,1] + c[2,3] + w(0,3)
since 3 is the key than c[0,3-1] + c[3,j] + w(0,3)
c[0,2] + c[3,3] + w(0,3)
c[0,3]=min{c[0,0] + c[1,3] + w(0,3),
c[0,1] + c[2,3] + w(0,3), C[i,r−1]+C[r,j]+w(i,j)
c[0,2] + c[3,3] + w(0,3) }
c[0,3]=min{0 + 10 + 12,
4+ 6 + 12,
8+ 0 +12 }
When i=1 and j=4 then we will consider the keys 20, 30, 40
c[1,4] = min{
c[1,2-1] + c[2,4],
c[1,3-1] + c[3,4] ,
c[1,4-1] + c[4,4]
} + 11
c[1,4] = min{
c[1,1] + c[2,4],
c[1,2] + c[3,4] ,
c[1,3] + c[4,4]
} + 11
= min{
0+12, 2+3, 10+0}+ 11
= min{12, 5, 10} + 11
The minimum value is 5; therefore, c[1,4] = 5+11 = 16
When i=0 and j=4 then we will consider the keys 10,20, 30, 40
W(0,4)=4+2+6+3=15
c[0,4] = min{
c[0,0] + c[1,4],
c[0,1] + c[2,4] ,
c[0,2] + c[3,4] ,
c[0,3] + c[4,4]
} + 15

c[0,4] = min{
0 + 16,
4 + 12,
C[i,r−1]+C[r,j]+w(i,j)
8+3,
20 +0
} + 15
r(0,4)
3

r(0,2) r(3,4)
since3 is the remainin after key
key
1 4

r(1,2)
r(0,0)
since1 is the
since1 is the
key
key 2
r(0,4)
30

r(0,2) r(3,4)
since3 is the remainin after key
key
10 40

r(1,2)
r(0,0)
since1 is the
since1 is the
key
key 20

You might also like