BST Search Operation in DSA Java - Time & Space Complexity
We want to understand how long it takes to find a value in a Binary Search Tree (BST).
How does the search time change as the tree grows bigger?
Analyze the time complexity of the following code snippet.
public boolean search(Node root, int key) {
if (root == null) return false;
if (root.val == key) return true;
if (key < root.val) return search(root.left, key);
else return search(root.right, key);
}
This code searches for a key in a BST by moving left or right depending on the value.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive call moving down one level in the tree.
- How many times: At most once per tree level until the key is found or a leaf is reached.
Each step moves down one level, so the number of steps depends on the tree's height.
| Input Size (n) | Approx. Operations (steps) |
|---|---|
| 10 | About 4 steps (if balanced) |
| 100 | About 7 steps (if balanced) |
| 1000 | About 10 steps (if balanced) |
Pattern observation: In a balanced tree, steps grow slowly as the tree grows bigger, roughly proportional to the height.
Time Complexity: O(h) where h is the tree height
This means the search time depends on how tall the tree is, not just how many nodes it has.
[X] Wrong: "Searching a BST always takes O(n) time because you might check every node."
[OK] Correct: In a balanced BST, you skip half the tree at each step, so you only check nodes along one path, not all nodes.
Understanding BST search time helps you explain how data structures speed up finding information, a key skill in many coding challenges.
"What if the BST becomes a linked list (completely unbalanced)? How would the time complexity change?"
Practice
search operation in a Binary Search Tree (BST)?Solution
Step 1: Understand the search operation
The search operation checks if a specific value is present in the BST by comparing it with nodes.Step 2: Differentiate from other operations
Insertion adds new nodes, deletion removes nodes, and traversal visits all nodes. Search only checks presence.Final Answer:
To find whether a given value exists in the BST -> Option BQuick Check:
Search = Find value presence [OK]
- Confusing search with insert or delete
- Thinking search returns all nodes
- Assuming search modifies the tree
Solution
Step 1: Identify return type for search
Search should return true or false, so return type must be boolean.Step 2: Check method signature correctness
Method takes an int value to search and returns boolean. public boolean search(int value) matches this.Final Answer:
public boolean search(int value) -> Option AQuick Check:
Return type boolean for search [OK]
- Using void return type for search
- Returning int or String instead of boolean
- Incorrect method parameter types
class Node {
int data;
Node left, right;
Node(int d) { data = d; left = right = null; }
}
public boolean search(Node root, int key) {
if (root == null) return false;
if (root.data == key) return true;
if (key < root.data) return search(root.left, key);
else return search(root.right, key);
}
// BST structure:
// 10
// / \
// 5 15
// / \ \
// 3 7 20
What is the output of search(root, 7)?Solution
Step 1: Trace search for key 7 starting at root 10
7 < 10, so search left subtree (node 5).Step 2: Trace search at node 5
7 > 5, so search right subtree (node 7).Step 3: Trace search at node 7
Node data equals key (7), so return true.Final Answer:
true -> Option CQuick Check:
7 found in BST = true [OK]
- Stopping search too early
- Confusing left and right subtree directions
- Assuming search returns node instead of boolean
public boolean search(Node root, int key) {
if (root == null) return false;
if (root.data == key) return true;
if (key > root.data) return search(root.left, key);
else return search(root.right, key);
}Solution
Step 1: Analyze recursive calls for BST search
For BST, if key < root.data, search left; if key > root.data, search right.Step 2: Identify swapped calls
Code calls search(root.left, key) when key > root.data, which is incorrect; left and right calls are swapped.Final Answer:
The recursive calls for left and right subtrees are swapped -> Option AQuick Check:
Left/right calls must match key comparison [OK]
- Swapping left and right recursive calls
- Missing base case for null root
- Returning wrong data type
15, 10, 20, 8, 12, 17, 25. Which sequence of nodes will the search operation visit when searching for 12?Solution
Step 1: Build BST structure from insertion order
Insert 15 as root; 10 left of 15; 20 right of 15; 8 left of 10; 12 right of 10; 17 left of 20; 25 right of 20.Step 2: Trace search path for key 12
Start at 15: 12 < 15, go left to 10; 12 > 10, go right to 12; found key.Final Answer:
15 -> 10 -> 12 -> Option DQuick Check:
Search path follows BST rules [OK]
- Visiting nodes not on search path
- Confusing insertion order with search path
- Assuming search visits all nodes
Ready to prove it?
Pass the test and this pattern moves unknown
