Floor and Ceil in BST in DSA Javascript - Time & Space Complexity
We want to know how long it takes to find the floor or ceil value in a Binary Search Tree (BST).
How does the search time grow as the tree gets bigger?
Analyze the time complexity of the following code snippet.
function floorInBST(root, key) {
let floor = null;
while (root) {
if (root.val === key) return root.val;
if (root.val > key) root = root.left;
else {
floor = root.val;
root = root.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: Traversing down the tree nodes one by one.
- How many times: At most once per tree level, moving left or right until a leaf or match is found.
As the tree grows taller, the search path can get longer, but we only follow one path down.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 4 steps (tree height) |
| 100 | Up to 7 steps |
| 1000 | Up to 10 steps |
Pattern observation: The number of steps grows slowly, roughly with the tree height, not the total nodes.
Time Complexity: O(h)
This means the time depends on the tree height, which is the longest path from root to leaf.
[X] Wrong: "Finding floor or ceil always takes time proportional to the total number of nodes."
[OK] Correct: We only follow one path down the tree, not all nodes, so time depends on height, not total nodes.
Understanding how tree height affects search time helps you explain and optimize BST operations clearly in interviews.
"What if the BST is balanced versus completely unbalanced? How would the time complexity change?"
Practice
floor of a value in a Binary Search Tree (BST) represent?Solution
Step 1: Understand the floor concept in BST
The floor of a value is the largest value in the BST 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 the ceil, not the floor. Options C and D are unrelated to floor.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: Identify correct parameter order and naming
The function to find ceil should take the BST root and the target value as parameters, typically as (root, value).Step 2: Check options for correct signature
function findCeil(root, value) {} matches this pattern. function ceilFind(value, root) {} reverses parameters, C is for floor and missing root, D misses value parameter.Final Answer:
function findCeil(root, value) {} -> Option CQuick Check:
Correct params: root, value [OK]
- Swapping parameter order
- Missing root or value parameter
- Using wrong function name
15
/ \
10 20
/ \ \
8 12 25
Solution
Step 1: Locate floor value in BST for 14
Floor is greatest value ≤ 14. Values ≤ 14 are 8, 10, 12. 12 is the greatest among these.Step 2: Confirm no value closer to 14 exists
15 is greater than 14, so not floor. 14 is not in BST.Final Answer:
12 -> Option DQuick Check:
Floor(14) = 12 [OK]
- Choosing 15 which is > 14
- Assuming floor must be exact value
- Picking smaller values like 10 instead of 12
function findCeil(root, value) {
if (!root) return -1;
if (root.val === value) return root.val;
if (root.val < value) return findCeil(root.right, value);
let ceil = findCeil(root.left, value);
return (ceil >= value) ? ceil : root.val;
}
What is the main error?Solution
Step 1: Analyze subtree traversal logic
If root.val < value, ceil must be in right subtree, but code wrongly searches left subtree.Step 2: Check other parts
Return -1 is acceptable as sentinel. Comparison and equality check are correct.Final Answer:
It searches left subtree when root.val < value instead of right -> Option AQuick Check:
Wrong subtree chosen for root.val < value [OK]
- Searching wrong subtree
- Ignoring base case
- Incorrect comparison logic
16
/ \
10 20
/ \ \
8 12 25
Which pair is correct?Solution
Step 1: Find floor of 17
Floor is greatest value ≤ 17. Values ≤ 17 are 8, 10, 12, 16. Largest is 16.Step 2: Find ceil of 17
Ceil is smallest value ≥ 17. Values ≥ 17 are 20, 25. Smallest is 20.Final Answer:
[16, 20] -> Option BQuick Check:
Floor=16, Ceil=20 [OK]
- Assuming floor or ceil equals value if not present
- Choosing wrong subtree values
- Mixing floor and ceil values
Ready to prove it?
Pass the test and this pattern moves unknown
