Optimal Binary Search Tree (1)
Optimal Binary Search Tree (1)
In 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.
An Optimal Binary Search Tree (OBST) is a binary search tree (BST) that minimizes the expected search
cost for a given set of keys. The objective is to arrange the keys in the tree structure such that the
average search cost is as low as possible, based on the frequency of access (or search) for each key.
The construction of an OBST is crucial in situations where we expect certain keys to be accessed more
frequently than others, and the goal is to minimize the total search time or expected search cost. This
problem is commonly encountered in database indexing, data retrieval, and other applications where
search operations on a tree structure are frequent.
for example: Consider keys 10, 20, 30, 40, 50, 60, 70
To find Optimal BST, we should know how many binary search trees can be made from the given
number of keys.
For example: 10, 20, 30 are the keys, and the following are the binary search trees that can be made
out from these keys.
When we use the above formula, then it is found that total 5 number of trees can be created.
The cost required for searching an element depends on the comparisons to be made to search an
element. Now, we will calculate the average cost of time of the above binary search trees.
In the third case, the number of comparisons is less because the height of the tree is less, so it's a
balanced binary search tree.
Note: To find the optimal binary search tree, we should know the frequency of searching a key.
Dynamic Approach
Consider the below table, which contains the keys and frequencies.
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, consider only the jth value.
When i=0 and j=2, then keys 10 and 20. There are
two possible trees
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:
o 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.
In the above tree, 10 is the root node, 20 is the right child of node 10, and 30 is the right child of
node 20.
In the above tree, 20 is the root node, 30 is the right child of node 20, and 10 is the left child of node
20.
The following are the trees that can be created if 30 is considered as the root node.
In the above tree, 30 is the root node, 20 is the left child of node 30, and 10 is the left child of node
20.
Therefore, the minimum cost is 20 which is the 3rd root. So, c[0,3] is equal to 20.
o When i=1 and j=4 then we will consider the keys 20, 30, 40
= min{12, 5, 10} + 11
In this case, we will consider four keys, i.e., 10, 20, 30 and 40. The frequencies of 10, 20, 30 and 40
are 4, 2, 6 and 3 respectively.
w[0, 4] = 4 + 2 + 6 + 3 = 15
= min{4 + 12} + 15 = 31
If we consider 30 as the root node then,
= min {8 + 3} + 15
= 26
= min{20 + 0} + 15
= 35
In the above cases, we have observed that 26 is the minimum cost; therefore, c[0,4] is equal to 26.
An optimal binary search tree (OBST) is a specialized binary search tree that's used to minimize the
cost of searching when the frequency of access to different keys varies. It's also known as a weight-
balanced binary tree.
Applications of OBST
• Database Indexing: In databases, some records are queried more frequently than others. An
OBST can be used to index the data such that frequently accessed records are found faster.
• Data Compression: OBSTs are related to Huffman coding trees, where frequently used
characters are assigned shorter codes for optimal encoding.
• Search Engines: OBSTs can be used in search engines where certain queries are more
common than others.