BST property and invariant in Data Structures Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the rules of a Binary Search Tree affect how fast we can find or add items.
Specifically, how does the tree's structure influence the work needed as it grows?
Analyze the time complexity of searching a value in a Binary Search Tree (BST).
function searchBST(node, target) {
if (node == null) return false;
if (node.value === target) return true;
if (target < node.value) {
return searchBST(node.left, target);
} else {
return searchBST(node.right, target);
}
}
This code looks for a target value by following the BST rule: left child is smaller, right child is bigger.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls moving down one tree level at a time.
- How many times: At most once per level, until the target is found or a leaf is reached.
Each step moves down one level, so the work depends on the tree's height.
| Input Size (n) | Approx. Operations (levels visited) |
|---|---|
| 10 | About 3 to 10 steps depending on tree shape |
| 100 | About 7 to 100 steps depending on tree shape |
| 1000 | About 10 to 1000 steps depending on tree shape |
Pattern observation: If the tree is balanced, steps grow slowly with input size; if unbalanced, steps can grow as large as the number of nodes.
Time Complexity: O(h) where h is the tree height
This means the time to search 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 (O(n))."
[OK] Correct: Because the BST property lets us skip half the tree at each step if balanced, so time depends on height, not total items.
Understanding how the BST property controls search speed shows you can reason about data structure efficiency, a key skill in coding interviews.
"What if the BST becomes a linked list (completely unbalanced)? How would the time complexity change?"
Practice
Solution
Step 1: Understand BST node arrangement
A BST arranges nodes so that left children have smaller values and right children have larger values than their parent node.Step 2: Compare options with BST definition
Left child nodes are smaller, right child nodes are larger than the parent node. correctly states this property. Other options describe different or incorrect structures.Final Answer:
Left child nodes are smaller, right child nodes are larger than the parent node. -> Option CQuick Check:
BST property = left < parent < right [OK]
- Thinking all nodes must have two children
- Confusing BST with other tree types
- Ignoring the order property in BST
Solution
Step 1: Recall BST left child rule
In a BST, the left child's value must be less than the parent's value.Step 2: Match this rule to options
left_child.value < node.value correctly states left_child.value < node.value. Other options violate this rule.Final Answer:
left_child.value < node.value -> Option BQuick Check:
Left child < parent [OK]
- Using greater than or equal instead of less than
- Allowing equal values on left child
- Confusing left and right child rules
10
/ \
5 15
/ \ \
3 7 20
Solution
Step 1: Understand in-order traversal
In-order traversal visits left subtree, then node, then right subtree, producing sorted order in BST.Step 2: Traverse the tree in-order
Left subtree of 10: nodes 3, 5, 7 in order; then 10; then right subtree 15, 20.Final Answer:
[3, 5, 7, 10, 15, 20] -> Option AQuick Check:
In-order traversal = sorted nodes [OK]
- Confusing pre-order or post-order with in-order
- Listing nodes in insertion order
- Reversing the traversal order
def insert(node, value):
if node is null:
return Node(value)
if value < node.value:
node.left = insert(node.left, value)
else:
node.right = insert(node.right, value)
return node
Solution
Step 1: Analyze duplicate handling in insertion
The code inserts duplicates into the right subtree without restriction, which may violate BST uniqueness if duplicates are not allowed.Step 2: Check other parts of the code
Base case and recursive updates are correct; function returns updated node properly.Final Answer:
The code does not handle duplicate values correctly. -> Option AQuick Check:
Duplicates need special handling in BST insert [OK]
- Assuming duplicates are automatically handled
- Ignoring return statements in recursion
- Confusing base case with recursive case
Solution
Step 1: Understand BST validation requirements
Each node must satisfy that all nodes in its left subtree are smaller and all in right subtree are larger, not just immediate children.Step 2: Evaluate approaches
Check if all left descendants are smaller and all right descendants are larger than the node, using min and max limits. uses min and max limits to ensure all descendants satisfy BST property, which is correct. Check if each node's left child is smaller and right child is larger, recursively. only checks immediate children, which is insufficient.Final Answer:
Check if all left descendants are smaller and all right descendants are larger than the node, using min and max limits. -> Option DQuick Check:
BST validation requires range checks, not just immediate children [OK]
- Checking only immediate children values
- Confusing BST property with tree balance
- Assuming unique values guarantee BST
