Floor and Ceil in BST in DSA Typescript - Time & Space Complexity
We want to know how long it takes to find the floor or ceil of a value in a Binary Search Tree (BST).
Specifically, how the time grows as the tree gets bigger.
Analyze the time complexity of the following code snippet.
function floorInBST(root: TreeNode | null, key: number): number | null {
let floor: number | null = null;
let current = root;
while (current) {
if (current.val === key) return current.val;
if (current.val > key) current = current.left;
else {
floor = current.val;
current = current.right;
}
}
return floor;
}
This code finds the floor value (largest value less than or equal to key) in a BST by moving left or right.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves down the tree nodes.
- How many times: At most once per tree level, moving down from root to leaf.
Each step moves one level down the tree, so the number of steps depends on the tree height.
| Input Size (n) | Approx. Operations (steps down tree) |
|---|---|
| 10 | Up to 4 (if balanced) |
| 100 | Up to 7 (if balanced) |
| 1000 | Up to 10 (if balanced) |
Pattern observation: The steps grow slowly, roughly with the tree height, which is about log of n if 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 nodes it has.
[X] Wrong: "Finding floor or ceil always takes O(n) time because we might check every node."
[OK] Correct: The BST property lets us skip large parts of the tree, so we only follow one path down, not all nodes.
Understanding this helps you explain how BSTs speed up searches compared to simple lists, showing your grasp of efficient data structures.
"What if the BST is not balanced and looks like a linked list? How would the time complexity change?"
Practice
floor of a value in a Binary Search Tree (BST) represent?Solution
Step 1: Understand the definition of floor in BST
The floor of a value in BST is the greatest value that is less than or equal to the given value.Step 2: Compare with other options
The smallest value in the BST greater than or equal to the given value describes ceil, not floor. Options C and D are unrelated to floor concept.Final Answer:
The greatest value in the BST less than or equal to the given value -> Option AQuick Check:
Floor = greatest ≤ value [OK]
- Confusing floor with ceil
- Thinking floor is the smallest value
- Assuming floor is always the root
Solution
Step 1: Check parameter order and types
Function should accept root node (possibly null) and a key number to find ceil for.Step 2: Validate return type
Return type should be number or null if no ceil found. function findCeil(root: TreeNode | null, key: number): number | null matches this.Final Answer:
function findCeil(root: TreeNode | null, key: number): number | null -> Option BQuick Check:
Correct params and return type [OK]
- Swapping parameter order
- Using wrong parameter types
- Missing null in root type
10
/ \
5 15
/ \
12 20
Solution
Step 1: Locate key 14 in BST or nearest nodes
14 is not in BST. Check nodes greater than or equal to 14: 15 and 20.Step 2: Find smallest node ≥ 14
Between 15 and 20, 15 is smaller and ≥ 14, so ceil is 15.Final Answer:
15 -> Option CQuick Check:
Ceil = smallest ≥ 14 is 15 [OK]
- Choosing 12 which is less than 14
- Choosing 20 which is larger but not smallest ≥ 14
- Returning null when ceil exists
function findFloor(root: TreeNode | null, key: number): number | null {
if (!root) return null;
if (root.val === key) return root.val;
const floorRight = findFloor(root.right, key);
return floorRight !== null ? floorRight : root.val;
}
What is the bug in this code?Solution
Step 1: Identify missing logic for root.val > key
The code does not recurse into the left subtree when root.val > key. Instead, it always computes floorRight from the right subtree.Step 2: Trace buggy return
When root.val > key and right subtree yields null for floorRight (common case), it returns root.val which exceeds key, violating floor definition.Final Answer:
It returns root.val even if floorRight is null and root.val > key -> Option AQuick Check:
Missing left recursion on root.val > key [OK]
- Ignoring null checks
- Confusing left and right subtree calls
- Assuming root.val always valid floor
Solution
Step 1: Identify floor of 5 in BST
Floor is greatest value ≤ 5. Values ≤ 5 are 2 and 4. Greatest is 4.Step 2: Identify ceil of 5 in BST
Ceil is smallest value ≥ 5. Values ≥ 5 are 6, 8, 10, 12, 14. Smallest is 6.Final Answer:
Floor = 4, Ceil = 6 -> Option DQuick Check:
Floor ≤ 5 = 4, Ceil ≥ 5 = 6 [OK]
- Swapping floor and ceil values
- Assuming floor or ceil equals key if not present
- Choosing values not in BST
Ready to prove it?
Pass the test and this pattern moves unknown
