BST Search Operation in DSA C++ - 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.
// Search for a value in a BST
bool searchBST(Node* root, int target) {
if (root == nullptr) return false;
if (root->val == target) return true;
if (target < root->val) return searchBST(root->left, target);
else return searchBST(root->right, target);
}
This code looks for a target value by moving left or right depending on comparisons.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls moving down one node at a time.
- How many times: At most once per tree level until the value is found or tree ends.
Each step moves down one level in the tree, so the number of steps depends on tree height.
| Input Size (n) | Approx. Operations (steps) |
|---|---|
| 10 | About 4 steps (height ~ log2(10)) |
| 100 | About 7 steps (height ~ log2(100)) |
| 1000 | About 10 steps (height ~ log2(1000)) |
Pattern observation: Steps grow slowly as tree size grows, roughly doubling size adds one more step.
Time Complexity: O(h) where h is tree height
This means 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: BST search moves down one path only, not all nodes. So it depends on height, not total nodes.
Understanding BST search time helps you explain efficient data lookup and compare with other structures like arrays or hash tables.
"What if the BST is unbalanced and looks like a linked list? How would the time complexity change?"
Practice
search operation in a Binary Search Tree (BST)?Solution
Step 1: Understand the purpose of search in BST
The search operation is used to check if a specific value is present in the BST.Step 2: Differentiate from other operations
Insertion adds nodes, deletion removes nodes, traversal visits nodes. Search only checks presence.Final Answer:
To find whether a given value exists in the BST -> Option AQuick Check:
Search = Find value [OK]
- Confusing search with insertion
- Thinking search modifies the tree
- Mixing search with traversal
root and integer key?Solution
Step 1: Identify return type for search
Search returns true/false if key found, so return type should be bool.Step 2: Check parameter types
Root is a pointer to Node, key is int. So signature is bool search(Node* root, int key).Final Answer:
bool search(Node* root, int key) { ... } -> Option BQuick Check:
Return bool and pointer param [OK]
- Using void return type for search
- Passing Node by value instead of pointer
- Returning Node instead of bool
search(root, 15). What will be the output?struct Node {
int data;
Node* left;
Node* right;
};
bool search(Node* root, int key) {
if (!root) 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 20
// / \
// 15 30
// Call:
// bool found = search(root, 15);
// std::cout << (found ? "Found" : "Not Found");
Solution
Step 1: Trace search path for key 15
Start at root 10, 15 > 10, go right to 20. Then 15 < 20, go left to 15.Step 2: Check node data at 15
Node data matches key 15, so function returns true and prints "Found".Final Answer:
Found -> Option AQuick Check:
Key found in BST = Found [OK]
- Stopping search too early
- Confusing left and right child directions
- Assuming key not found when present
bool search(Node* root, int key) {
if (root == nullptr) 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 logic
For BST, if key < root->data, search left; if key > root->data, search right.Step 2: Identify incorrect direction
Code searches left when key > root->data, which is wrong. Left subtree has smaller values.Step 3: Correct the recursive calls
Swap left and right calls: if (key < root->data) search left; else search right.Final Answer:
Swap left and right in recursive calls; should be key < root->data for left -> Option CQuick Check:
BST property fixes search direction [OK]
- Mixing left and right subtree directions
- Ignoring base case
- Changing return type unnecessarily
Solution
Step 1: Build BST structure from insertion order
Insert 25 (root), then 15 (left of 25), 50 (right of 25), 10 (left of 15), 22 (right of 15), 35 (left of 50), 70 (right of 50).Step 2: Trace search path for 22
Start at 25 (visit 1), 22 < 25 go left to 15 (visit 2), 22 > 15 go right to 22 (visit 3), found key.Final Answer:
3 -> Option DQuick Check:
Nodes visited = 3 [OK]
- Counting nodes not visited
- Misplacing nodes in BST
- Stopping count before finding key
Ready to prove it?
Pass the test and this pattern moves unknown
