Optimal Binary Search trees

An optimal binary search tree (OBST), also known as a weight-balanced binary search tree, is a binary search tree that minimizes the average search time for a given set of keys. The concept of OBSTs is widely used in data structures and algorithms to optimize search operations.

To construct an optimal binary search tree, you can use the dynamic programming approach known as the "Optimal Binary Search Tree" algorithm. The algorithm builds the tree bottom-up by considering all possible subtrees and choosing the one with the minimum cost.


Here is a step-by-step explanation of the Optimal Binary Search Tree algorithm:

1. Define the problem:

- Given a set of n keys with associated probabilities or frequencies.
- Determine the structure of the optimal binary search tree that minimizes the weighted average search time.


2. Calculate the cumulative probabilities:

- Calculate the cumulative probabilities for the keys and their associated frequencies.
- This step helps in optimizing the search time by considering the frequencies of keys.


3. Create a cost matrix:

- Create a 2D matrix `cost` of size (n+2) x (n+1), where cost[i][j] represents the minimum cost of constructing an optimal binary search tree from keys i to j.
- Initialize all cost[i][i] values with their respective key probabilities.
- Initialize all cost[i][j] values with infinity.


4. Calculate the minimum cost:

- For each chain length l (from 1 to n), iterate through the keys and calculate the cost of constructing subtrees.
- For each possible subtree of length l, calculate the cost using the formula: cost[i][j] = sum(frequencies[i..j]) + min(cost[i][k-1] + cost[k+1][j]) for all k in [i, j].
- Here, frequencies[i..j] represents the sum of probabilities/frequencies from key i to j.


5. Construct the optimal binary search tree:

- Using the calculated cost matrix, construct the optimal binary search tree by recursively dividing the problem into smaller subproblems.
- Determine the root of the subtree with the minimum cost for each interval [i, j].
- Assign the key with the minimum cost as the root and recursively construct the left and right subtrees.


6. Return the root:

- Once the entire tree is constructed, return the root node of the optimal binary search tree.



The Optimal Binary Search Tree algorithm has a time complexity of O(n^3) and a space complexity of O(n^2), where n is the number of keys.

This algorithm allows you to construct an optimal binary search tree that minimizes the average search time based on the probabilities or frequencies associated with the keys.



About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





 PreviousNext