🌟 Searching (Optimum Search Trees)
Imagine you have a small library and you often look up books by their titles.
Some books are checked almost every day, while others are rarely touched.
Now here’s a question:
Shouldn’t the books you search for most often be easier and faster to find?
That’s exactly the idea behind an Optimum Search Tree (OST).
🌱 The Basic Idea
An Optimum Search Tree (also called an Optimal Binary Search Tree, or OBST) is a special kind of Binary Search Tree that is arranged in the best possible way so that searching becomes as fast as possible on average.
It’s not just about building any Binary Search Tree —
it’s about building one that gives minimum total search time based on how often you search for each item.
🌳 Why Do We Need an Optimum Tree?
Let’s say you have the following words in a dictionary app:
Apple, Banana, Cherry, Mango
If you searched for “Apple” 50 times a day,
“Banana” 30 times,
“Cherry” 15 times,
and “Mango” only 5 times,
then logically, “Apple” should be placed near the top of the tree so it can be found quickly —
because it’s the one you use most often.
If “Mango” (the rare one) was at the top instead, you’d waste time every day searching for “Apple”.
So, the goal of an Optimum Search Tree is to arrange nodes based on how frequently they’re accessed, not just by order.
🧠 Key Concept: Search Probability
Each key (or data value) has a probability of being searched —
basically, how likely it is that you’ll look for that key.
An Optimal Binary Search Tree tries to minimize the total search cost,
which depends on:
- How deep the node is in the tree, and
- How often it’s searched.
So the frequently searched nodes should be closer to the root,
and the rarely searched ones can be further down.
🪴 Let’s Visualize It
Suppose you have these keys and their search frequencies:
| Key | Frequency |
|---|---|
| 10 | 3 |
| 20 | 2 |
| 30 | 4 |
Now, if you simply build a normal Binary Search Tree (BST):
20
/ \
10 30
Let’s count the total cost (depth × frequency):
| Node | Depth | Frequency | Cost |
|---|---|---|---|
| 20 | 1 | 2 | 2 |
| 10 | 2 | 3 | 6 |
| 30 | 2 | 4 | 8 |
| Total Cost | 16 |
But what if we rearrange it like this?
30
/
10
\
20
Now the depths and costs become:
| Node | Depth | Frequency | Cost |
|---|---|---|---|
| 30 | 1 | 4 | 4 |
| 10 | 2 | 3 | 6 |
| 20 | 3 | 2 | 6 |
| Total Cost | 16 |
In this case, the total cost remains the same — but with larger trees and many keys,
there is usually one specific arrangement that gives the minimum total cost.
That arrangement is what we call the Optimum Search Tree.
🌿 Diagram: Example of an Optimum Search Tree
Here’s a simple visual idea:
[A]
/ \
[B] [C]
/ \ / \
[D] [E] [F] [G]
If nodes A and C are searched frequently,
placing them higher in the tree reduces the average search time for all operations.
📘 How It’s Built
Building an Optimum Search Tree isn’t done by simple guessing —
it’s usually done using Dynamic Programming, where the computer tests different ways of building the tree and calculates which one gives the lowest total search cost.
In simple terms:
- It tries all possible roots.
- Calculates the cost of left and right subtrees.
- Chooses the one with the least total cost.
That’s why it’s called “optimum” — it’s mathematically the best arrangement.
💡 Real-Life Analogy
Think of your mobile app icons.
You keep the ones you open daily (like WhatsApp or YouTube) on the front screen.
The less-used apps go inside folders or the last pages.
You’re not deleting anything —
you’re just placing frequently used things where they’re easiest to reach.
That’s exactly what an Optimum Search Tree does for data. 🌟
⚙️ Time Complexity
- Building the Tree: O(n³) using dynamic programming (can be improved to O(n²))
- Searching in the Tree: O(h), where h is the height of the tree (usually smaller than normal BSTs because the tree is well-balanced)
🌼 Advantages
✅ Gives the minimum average search time.
✅ Great when some data is searched much more often than others.
✅ Perfect for static data (where the data doesn’t change often).
🍂 Limitations
❌ Construction is complex and takes time.
❌ Not ideal for dynamic data (where insertions/deletions happen frequently).
❌ Needs prior knowledge of search frequencies.
🌈 Quick Recap
| Concept | Meaning |
|---|---|
| Goal | Arrange the tree to minimize average search cost |
| Key Factor | Frequency (probability) of searches |
| Best Used For | Static data with known access frequencies |
| Built Using | Dynamic Programming |
| Analogy | Keeping frequently used apps on the home screen |