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
Node* bstSearch(Node* root, int target) {
if (root == NULL || root->data == target) {
return root;
}
if (target < root->data) {
return bstSearch(root->left, target);
} else {
return bstSearch(root->right, target);
}
}
This code searches 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 level of the tree.
- How many times: At most once per tree level until the target is found or a leaf is reached.
Each recursive call moves one level down the tree, so the number of calls depends on tree height.
| Input Size (n) | Approx. Operations (calls) |
|---|---|
| 10 | Up to 4 (if tree is balanced) |
| 100 | Up to 7 (if tree is balanced) |
| 1000 | Up to 10 (if tree is balanced) |
Pattern observation: In a balanced tree, operations grow slowly (logarithmically) as input size increases.
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 the same time as searching an array, O(n)."
[OK] Correct: A BST search usually takes less time because it skips half the tree at each step if balanced, unlike a simple array search.
Understanding BST search time helps you explain how data structures speed up finding information, a key skill in coding interviews.
"What if the BST becomes a linked list (all nodes only have one child)? 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 values starting from the root.Step 2: Differentiate from other operations
Insertion adds new nodes, deletion removes nodes, and traversal visits all nodes. Search only checks for presence.Final Answer:
To find whether a given value exists in the BST -> Option BQuick Check:
Search = Find value [OK]
- Confusing search with insert or delete
- Thinking search returns all nodes
- Assuming search modifies the tree
root and an integer key?Solution
Step 1: Understand function parameters for BST search
The search function typically takes the root node pointer first, then the key to find.Step 2: Check each option's parameter order and types
search(root, key); correctly passes root and key. search(key, root); reverses parameters. search(root->key, key); passes a value, not a node pointer. search(key); misses root.Final Answer:
search(root, key); -> Option AQuick Check:
Function(root, key) = Correct call [OK]
- Swapping parameter order
- Passing node value instead of pointer
- Omitting root parameter
search(root, 15);. What will be the output if the BST contains nodes with values 10, 5, 15, 12, 20?struct Node {
int data;
struct Node* left;
struct Node* right;
};
struct Node* search(struct Node* root, int key) {
if (root == NULL || root->data == key)
return root;
if (key < root->data)
return search(root->left, key);
else
return search(root->right, key);
}
// Assume root points to the BST described above
struct Node* result = search(root, 15);
if (result != NULL) printf("Found: %d", result->data);
else printf("Not Found");Solution
Step 1: Trace the search for key 15 in the BST
Start at root (10). Since 15 > 10, go right to node 15.Step 2: Check if node 15 matches key
Node 15's data equals key 15, so return this node.Final Answer:
Found: 15 -> Option DQuick Check:
Search found node with data 15 [OK]
- Stopping at root without checking children
- Returning wrong node data
- Assuming search always returns root
struct Node* search(struct Node* root, int key) {
if (root == NULL)
return NULL;
if (root->data == key)
return root;
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 right subtree; if less, search left subtree.Step 2: Check the code's recursive calls
Code calls search(root->left, key) when key > root->data, which is incorrect. It should call search(root->right, key).Final Answer:
The recursive calls for left and right subtrees are swapped -> Option CQuick Check:
Key > root means go right, not left [OK]
- Swapping left and right subtree calls
- Ignoring base case
- Confusing comparison operators
Solution
Step 1: Start at root and compare with key 13
Root is 8. Since 13 > 8, go right to 10.Step 2: Compare 13 with 10 and move accordingly
13 > 10, go right to 14. Then 13 < 14, go left to 13.Step 3: Sequence of visited nodes
Visited nodes are 8, 10, 14, then 13.Final Answer:
8 -> 10 -> 14 -> 13 -> Option AQuick Check:
Follow BST rules to trace path [OK]
- Skipping nodes in path
- Going left when should go right
- Assuming direct jump to node
Ready to prove it?
Pass the test and this pattern moves unknown
