BST Search Operation in DSA Typescript - 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.
function searchBST(root: TreeNode | null, val: number): TreeNode | null {
if (root === null || root.val === val) return root;
if (val < root.val) return searchBST(root.left, val);
else return searchBST(root.right, val);
}
This code searches for a value in a BST 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 value 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 (height ~ log2(10)) |
| 100 | About 7 steps (height ~ log2(100)) |
| 1000 | About 10 steps (height ~ log2(1000)) |
Pattern observation: The steps grow slowly, roughly doubling the input size adds only one more step.
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 a balanced tree."
[OK] Correct: If the tree is not balanced, it can become like a linked list, making search take much longer.
Understanding BST search time helps you explain how data structure shape affects performance, a key skill in coding interviews.
What if the BST is perfectly balanced? How does that affect the search time complexity?
Practice
search operation in a Binary Search Tree (BST)?Solution
Step 1: Understand the purpose of search in BST
The search operation is designed to check if a specific value is present in the BST.Step 2: Differentiate from other operations
Insertion adds new values, deletion removes values, and traversal visits all nodes. Search only checks existence.Final Answer:
To find whether a given value exists in the BST -> Option CQuick 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: Analyze parameter types
The search needs the root node (which can be null) and the value to find (a number).Step 2: Analyze return type
Search returns true or false, so return type should be boolean.Final Answer:
function search(root: TreeNode | null, value: number): boolean -> Option AQuick Check:
Correct params and boolean return [OK]
- Using wrong parameter types
- Returning void instead of boolean
- Using string instead of number for value
search(root, 7), what will be the output?BST structure:
10
/ \
5 15
/ \ \
3 7 20
Code snippet:
function search(root: TreeNode | null, value: number): boolean {
if (!root) return false;
if (root.val === value) return true;
if (value < root.val) return search(root.left, value);
return search(root.right, value);
}
console.log(search(root, 7));Solution
Step 1: Trace search for value 7
Start at root 10: 7 < 10, go left to 5. Then 7 > 5, go right to 7.Step 2: Check node value
Node value is 7, matches search value, so return true.Final Answer:
true -> Option DQuick Check:
Found 7 in BST = true [OK]
- Stopping search too early
- Confusing left and right child directions
- Returning false when node matches
function search(root: TreeNode | null, value: number): boolean {
if (root === null) return false;
if (root.val === value) return true;
if (value > root.val) return search(root.left, value);
else return search(root.right, value);
}Solution
Step 1: Check recursive calls logic
For BST, if value > root.val, search should go right, but code goes left.Step 2: Confirm swapped children calls
Similarly, else goes right instead of left. So left and right calls are swapped.Final Answer:
The recursive calls to left and right children are swapped -> Option BQuick Check:
Left/right calls swapped = bug [OK]
- Mixing left and right child in recursion
- Ignoring base case (though present here)
- Wrong comparison operators
Choose the correct approach:
Solution
Step 1: Understand duplicate placement in BST
Duplicates are stored in right subtree, so first occurrence is the leftmost matching node.Step 2: Modify search to find first occurrence
When value equals current node, continue searching left subtree to find earlier occurrence.Final Answer:
Always search left subtree when value equals current node's value -> Option AQuick Check:
Search left on equal to find first duplicate [OK]
- Stopping search immediately on equal
- Searching right subtree on equal
- Doing full inorder traversal unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
