BST Property and Why It Matters in DSA Typescript - Time & Space Complexity
We want to understand how the special order in a Binary Search Tree (BST) helps speed up searching and other operations.
How does this order affect the time it takes to find or add items?
Analyze the time complexity of searching a value in a BST.
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 looks for a value by moving left or right depending on comparisons, using the BST order.
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 (if balanced) |
| 100 | About 7 (if balanced) |
| 1000 | About 10 (if balanced) |
Pattern observation: In a balanced BST, steps grow slowly (logarithmically) as input grows.
Time Complexity: O(h) where h is tree height
This means the 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 sorted list."
[OK] Correct: A BST can find values faster by skipping half the tree at each step if balanced, unlike a list which checks items one by one.
Knowing how the BST property affects search time helps you explain why data structures matter and how to choose the right one for fast lookups.
"What if the BST becomes a linked list (all nodes only have one child)? How would the time complexity change?"
Practice
Solution
Step 1: Understand BST node arrangement
In a BST, every node's left child has a smaller value and the right child has a larger value.Step 2: Compare with other options
Other options describe different data structures or incorrect properties.Final Answer:
Left child nodes have smaller values, right child nodes have larger values -> Option AQuick Check:
BST property = Left smaller, right larger [OK]
- Thinking all nodes must have two children
- Confusing BST with binary tree
- Assuming nodes store sums
Solution
Step 1: Identify BST node structure
A BST node has a value and two pointers: left and right, which can be null.Step 2: Check options
interface Node { value: number; left: Node | null; right: Node | null; } matches this structure; others describe linked lists or generic trees.Final Answer:
interface Node { value: number; left: Node | null; right: Node | null; } -> Option BQuick Check:
BST node = value + left + right [OK]
- Using next/prev like linked lists
- Using children array for BST
- Missing null for leaf nodes
const root = {
value: 10,
left: { value: 5, left: null, right: null },
right: { value: 15, left: null, right: null }
};
function inorder(node) {
if (!node) return [];
return [...inorder(node.left), node.value, ...inorder(node.right)];
}
console.log(inorder(root));Solution
Step 1: Understand in-order traversal
In-order visits left subtree, then node, then right subtree.Step 2: Apply traversal to given tree
Left subtree: 5, root: 10, right subtree: 15, so output is [5, 10, 15].Final Answer:
[5, 10, 15] -> Option DQuick Check:
In-order = left, root, right = [5,10,15] [OK]
- Confusing pre-order or post-order with in-order
- Mixing left and right subtree order
- Forgetting to return empty array for null
function insert(node, value) {
if (!node) return { value, left: null, right: null };
if (value < node.value) insert(node.left, value);
else insert(node.right, value);
return node;
}Solution
Step 1: Analyze recursive calls
The recursive calls to insert do not assign their results back to node.left or node.right.Step 2: Fix missing assignment
We must assign: node.left = insert(node.left, value) or node.right = insert(node.right, value) to update the tree.Final Answer:
Missing return when recursively inserting left or right -> Option CQuick Check:
Recursive insert must update child pointers [OK]
- Not assigning recursive call result
- Wrong comparison operator
- Expecting parent pointer in BST node
Solution
Step 1: Understand BST property role
The BST property ensures left < node < right, which makes in-order traversal sorted.Step 2: Effect of deletion on BST property
If deletion breaks this property, in-order traversal won't be sorted, breaking BST usefulness.Final Answer:
To ensure in-order traversal still returns sorted values -> Option AQuick Check:
BST property keeps in-order sorted [OK]
- Thinking BST auto-balances on deletion
- Assuming duplicates are allowed by default
- Confusing BST with linked list
Ready to prove it?
Pass the test and this pattern moves unknown
