BST Search Operation in DSA Javascript - 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, target) {
if (root === null) return false;
if (root.value === target) return true;
if (target < root.value) {
return searchBST(root.left, target);
} else {
return searchBST(root.right, target);
}
}
This code searches for a target 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 each time.
- How many times: At most once per tree level, until the target 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 (tree height) |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: The steps grow slowly, roughly with the height of the tree, not the total nodes.
Time Complexity: O(h)
This means the search time depends on the tree height, which is the number of levels from root to leaf.
[X] Wrong: "Searching a BST always takes O(log n) time because it's a tree."
[OK] Correct: If the tree is not balanced, height can be as big as n, making search O(n) in the worst case.
Understanding BST search time helps you explain how data structure shape affects performance, a key skill in interviews.
"What if the BST is perfectly balanced? How would the time complexity change compared to a skewed tree?"
Practice
search operation in a Binary Search Tree (BST)?Solution
Step 1: Understand the search operation
The search operation in a BST is used to check if a specific value is present in the tree.Step 2: Differentiate from other operations
Insertion adds new values, deletion removes values, and balancing adjusts the tree structure. Search only checks for existence.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 changes the tree
- Assuming search balances the tree
Solution
Step 1: Identify parameters needed for BST search
The search needs the root node to start and the value to find, so parameters are (root, value).Step 2: Check function signature correctness
function searchBST(root, value) { } correctly uses (root, value). Options A, C, and D have wrong parameter order or missing parameters.Final Answer:
function searchBST(root, value) { } -> Option DQuick Check:
Correct parameters order = function searchBST(root, value) { } [OK]
- Swapping parameter order
- Missing one parameter
- Returning wrong variable immediately
class Node {
constructor(val) {
this.val = val;
this.left = null;
this.right = null;
}
}
const root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
function searchBST(root, val) {
if (!root) return false;
if (root.val === val) return true;
if (val < root.val) return searchBST(root.left, val);
else return searchBST(root.right, val);
}
console.log(searchBST(root, 15));What will be the output?Solution
Step 1: Trace the search for value 15
Start at root (10). Since 15 > 10, move to right child (15).Step 2: Check if current node value matches 15
Node value is 15, which matches the search value, so return true.Final Answer:
true -> Option AQuick Check:
Value found = true [OK]
- Returning false too early
- Not checking node value before recursion
- Confusing left and right child directions
function searchBST(root, val) {
if (root === null) return false;
if (root.val === val) return true;
if (val > root.val) return searchBST(root.left, val);
else return searchBST(root.right, val);
}Solution
Step 1: Analyze comparison logic for recursion
If val > root.val, search should go right, but code goes left. This is reversed.Step 2: Confirm correct child direction for BST search
For val > root.val, search right subtree; for val < root.val, search left subtree.Final Answer:
The comparison directions for left and right child are swapped -> Option CQuick Check:
Left/right child direction swapped = The comparison directions for left and right child are swapped [OK]
- Swapping left and right child in recursion
- Forgetting base case
- Returning wrong data type
Solution
Step 1: Start at root node 20 and compare with 12
Since 12 < 20, move to left child 10.Step 2: Compare 12 with node 10
12 > 10, so move to right child 15.Step 3: Compare 12 with node 15
12 < 15, so move to left child, which is null (not in tree), search ends.Final Answer:
20 -> 10 -> 15 -> Option AQuick Check:
Visited nodes = 20, 10, 15 [OK]
- Visiting wrong child nodes
- Assuming 12 is in the tree
- Skipping nodes in path
Ready to prove it?
Pass the test and this pattern moves unknown
