BST Property and Why It Matters in DSA Javascript - Time & Space Complexity
We want to understand how the special rule of a Binary Search Tree (BST) affects how fast we can find or add items.
How does this rule help us work faster as the tree grows?
Analyze the time complexity of the following code snippet.
function searchBST(root, val) {
if (!root) return null;
if (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 based on the BST rule.
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 tree ends.
Each step moves down one level, so the number of steps grows with the tree's height.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps (if tree is balanced) |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Steps grow slowly as tree size grows, if the tree stays balanced.
Time Complexity: O(h) where h is tree height
This means the time depends on how tall the tree is, not just how many items it has.
[X] Wrong: "Searching a BST always takes the same time as the number of items (n)."
[OK] Correct: If the tree is unbalanced, it can act like a list, making search slower. But if balanced, search is faster than checking every item.
Knowing how the BST rule helps search quickly shows you understand why data structures matter for speed, a key skill in coding interviews.
"What if the BST becomes a linked list (all nodes only on one side)? How would the time complexity change?"
Practice
Solution
Step 1: Understand BST node value rules
In a BST, the left subtree contains values smaller than the node, and the right subtree contains values larger than the node.Step 2: Compare options with BST definition
For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. matches the BST property exactly, while others describe different structures.Final Answer:
For each node, all values in the left subtree are smaller, and all values in the right subtree are larger. -> Option BQuick Check:
BST property = left < node < right [OK]
- Confusing BST with binary tree (not ordered)
- Thinking all nodes must have two children
- Assuming all values are equal
Solution
Step 1: Recall BST insertion rule
Values smaller than the current node go to the left subtree.Step 2: Match condition to insertion side
Only the condition 'value < node.value' correctly directs insertion to the left.Final Answer:
if (value < node.value) { insertLeft(); } -> Option AQuick Check:
Smaller values go left [OK]
- Using > instead of < for left subtree
- Using == which is not for left or right
- Using >= which includes equal values incorrectly
10 / \ 5 15 / \ \ 3 7 20
Solution
Step 1: Understand in-order traversal
In-order traversal visits left subtree, then node, then right subtree recursively.Step 2: Apply in-order to given tree
Left subtree of 10: 3, 5, 7; then 10; then right subtree: 15, 20. Combined: [3, 5, 7, 10, 15, 20]Final Answer:
[3, 5, 7, 10, 15, 20] -> Option CQuick Check:
In-order = sorted BST values [OK]
- Confusing pre-order or post-order with in-order
- Listing nodes in insertion order
- Reversing the traversal order
function insert(node, value) {
if (!node) return { value, left: null, right: null };
if (value <= node.value) {
node.left = insert(node.left, value);
} else {
node.right = insert(node.right, value);
}
return node;
}What is the main issue with this code regarding BST property?
Solution
Step 1: Analyze duplicate handling in insertion
The condition 'value <= node.value' inserts duplicates to the left subtree.Step 2: Understand BST strictness
Strict BSTs require left subtree values strictly less than node value; duplicates on left can break this.Final Answer:
It allows duplicate values on the left subtree, which may break strict BST rules. -> Option DQuick Check:
Duplicates on left break strict BST [OK]
- Ignoring duplicate value placement
- Thinking null check is missing
- Confusing left and right subtree insertion
Solution
Step 1: Understand the violation type
Left child having greater value than node breaks BST property locally.Step 2: Choose minimal fix without full rebuild
Swapping values of violating nodes with their children can restore local BST property without full rebuild.Step 3: Evaluate other options
Perform an in-order traversal, collect values, then rebuild the BST from sorted values. rebuilds fully, Delete all nodes and insert them again in correct order. deletes all nodes (costly), Ignore the violations as they do not affect search operations. ignores problem (wrong).Final Answer:
Swap the values of the violating nodes with their children to restore BST property locally. -> Option AQuick Check:
Local swaps fix BST violations efficiently [OK]
- Rebuilding entire tree unnecessarily
- Ignoring violations thinking no impact
- Deleting all nodes instead of fixing
Ready to prove it?
Pass the test and this pattern moves unknown
