Floor and Ceil in BST in DSA Java - Time & Space Complexity
We want to know how long it takes to find the floor or ceil 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.
public int floor(Node root, int key) {
int floor = -1;
while (root != null) {
if (root.data == key) return root.data;
if (root.data > key) root = root.left;
else {
floor = root.data;
root = root.right;
}
}
return floor;
}
This code finds the floor value (largest value <= 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 number of steps to find floor or ceil grows roughly with the tree height.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 4 steps (if tree height ~4) |
| 100 | Up to 7 steps (if tree height ~7) |
| 1000 | Up to 10 steps (if tree height ~10) |
Pattern observation: The steps grow with the height of the tree, which depends on how balanced the tree is.
Time Complexity: O(h)
This means the time depends on the height of the BST, moving down one level at a time.
[X] Wrong: "Finding floor or ceil always takes O(log n) time because BSTs are balanced."
[OK] Correct: Not all BSTs are balanced; if the tree is skewed, height can be as large as n, making the time O(n).
Understanding how tree height affects search time helps you explain and improve BST operations clearly in interviews.
What if the BST is perfectly balanced? 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 is the greatest value in the BST that is less than or equal to the given value.Step 2: Compare with options
The greatest value in the BST less than or equal to the given value matches this definition exactly, while others describe different concepts.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 smallest value
- Assuming floor is always the root
Solution
Step 1: Recall Java method signature syntax
In Java, a method signature includes access modifier, return type, method name, and parameters in parentheses.Step 2: Check each option
public int findCeil(Node root, int key) follows correct syntax: public int findCeil(Node root, int key). Others have incorrect order or missing return types.Final Answer:
public int findCeil(Node root, int key) -> Option AQuick Check:
Java method = access + return + name(params) [OK]
- Placing return type after method name
- Omitting access modifier
- Using void when return type is int
15
/ \
10 20
/ \ \
8 12 25
Solution
Step 1: Find floor of 14 in BST
Floor is greatest value ≤ 14. Values ≤14 are 8,10,12. Greatest is 12.Step 2: Find ceil of 14 in BST
Ceil is smallest value ≥ 14. Values ≥14 are 15,20,25. Smallest is 15.Final Answer:
Floor = 12, Ceil = 15 -> Option BQuick Check:
Floor ≤14 = 12, Ceil ≥14 = 15 [OK]
- Choosing floor or ceil outside BST values
- Mixing floor and ceil values
- Ignoring BST property when searching
int floor(Node root, int key) {
if (root == null) return -1;
if (root.data == key) return root.data;
if (root.data > key) return floor(root.left, key);
int floorValue = floor(root.right, key);
return (floorValue <= key && floorValue != -1) ? floorValue : root.data;
}
What is the bug in this code?Solution
Step 1: Analyze the return condition when root.data > key
If root.data > key, method searches left subtree correctly.Step 2: Analyze return when root.data < key
It gets floorValue from right subtree. If floorValue is -1 (no floor found), it returns root.data even if root.data > key, which is wrong.Step 3: Identify bug
Returning root.data without checking if root.data ≤ key causes incorrect floor when floorValue is -1.Final Answer:
It returns root.data even if floorValue is -1 and root.data > key -> Option DQuick Check:
Check root.data ≤ key before returning [OK]
- Ignoring invalid floorValue -1
- Returning root.data without condition
- Not handling null root properly
Solution
Step 1: Understand BST properties for floor and ceil
BST allows searching floor and ceil by comparing key with node values and moving left or right.Step 2: Evaluate approaches
Traversing once and updating floor and ceil during search is efficient (O(h)), better than traversing twice or converting to array.Step 3: Choose best approach
Traverse BST once, updating floor and ceil during search is best as it uses BST property and single traversal.Final Answer:
Traverse BST once, updating floor and ceil during search -> Option CQuick Check:
Single BST traversal updates floor & ceil [OK]
- Doing two separate traversals
- Converting BST to array unnecessarily
- Using BFS which ignores BST order
Ready to prove it?
Pass the test and this pattern moves unknown
