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.
public bool Search(Node root, int target) {
if (root == null) return false;
if (root.Value == target) return true;
if (target < root.Value) return Search(root.Left, target);
else return Search(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.
- 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 input size adds only one extra 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 an array, so it is O(n)."
[OK] Correct: BST search uses the tree structure to skip half the nodes each step, so it usually takes less time than checking every node.
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 unbalanced and looks like a linked list? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of BST search
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 nodes, deletion removes nodes, and traversal visits all nodes. Search only checks for presence.Final Answer:
Finds whether a given value exists in the BST -> Option DQuick Check:
Search = Find value [OK]
- Confusing search with insertion
- Thinking search deletes nodes
- Assuming search visits all nodes
root and target value key?Solution
Step 1: Understand typical BST search method signature
Usually, search is a static or instance method taking the root node and key as parameters, like Search(root, key).Step 2: Analyze options for correct syntax
bool found = Search(root, key); matches the common pattern. bool found = root.Search(key); assumes Search is an instance method on root, which may not exist. bool found = root.search(key); uses lowercase 'search' which is not standard C# naming. bool found = Search(key, root); has parameters reversed.Final Answer:
bool found = Search(root, key); -> Option BQuick Check:
Search(root, key) is standard call [OK]
- Using lowercase method names in C#
- Reversing parameter order
- Assuming search is a method on node instance
class Node {
public int val;
public Node left, right;
public Node(int v) { val = v; left = right = null; }
}
bool 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);
}
// BST structure:
// 10
// / \
// 5 15
// / / \
// 2 12 20
Node root = new Node(10);
root.left = new Node(5);
root.right = new Node(15);
root.left.left = new Node(2);
root.right.left = new Node(12);
root.right.right = new Node(20);
Console.WriteLine(Search(root, 12));
Console.WriteLine(Search(root, 7));
What is the output?Solution
Step 1: Trace Search(root, 12)
Start at root 10: 12 > 10, go right to 15. 12 < 15, go left to 12. Found 12, return true.Step 2: Trace Search(root, 7)
Start at root 10: 7 < 10, go left to 5. 7 > 5, go right which is null. Return false.Final Answer:
True False -> Option AQuick Check:
12 found = True, 7 not found = False [OK]
- Confusing left and right child directions
- Assuming 7 is in the tree
- Not returning false on null node
bool 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);
}
What is the bug in this code?Solution
Step 1: Analyze the direction of subtree search
When key > root.val, the search should go to the right subtree, but code incorrectly goes left.Step 2: Confirm other conditions are correct
Code correctly returns false if root is null and checks equality properly.Final Answer:
It searches left subtree when key is greater than root value -> Option AQuick Check:
Greater key goes right, not left [OK]
- Swapping left and right subtree in conditions
- Missing null check
- Ignoring equality check
Solution
Step 1: Build the BST structure mentally
Insert 8 as root. 3 left of 8. 10 right of 8. 1 left of 3. 6 right of 3. 14 right of 10. 4 left of 6. 7 right of 6. 13 left of 14.Step 2: Trace search for 7
Start at 8: 7 < 8 go left to 3. 7 > 3 go right to 6. 7 > 6 go right to 7. Found 7.Final Answer:
8 -> 3 -> 6 -> 7 -> Option CQuick Check:
Search path follows BST rules [OK]
- Skipping nodes in path
- Going left when should go right
- Confusing insertion order with search path
Ready to prove it?
Pass the test and this pattern moves unknown
