Floor and Ceil in BST in DSA C# - Time & Space Complexity
We want to understand how fast we can find the floor or ceil value in a Binary Search Tree (BST).
How does the time taken grow 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.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 traversing from root down.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Moving down one level in the BST (either left or right child).
- How many times: At most once per level of the tree, until a leaf is reached or floor is found.
Each step moves down one level in the tree, so the number of steps depends on the tree height.
| Input Size (n) | Approx. Operations (tree height) |
|---|---|
| 10 | About 3-4 steps |
| 100 | About 6-7 steps |
| 1000 | About 9-10 steps |
Pattern observation: The steps grow slowly, roughly proportional to the height of the tree, which is much smaller than the total number of nodes.
Time Complexity: O(h)
This means the time grows with the height of the tree, not the total number of nodes.
[X] Wrong: "Finding floor or ceil always takes time proportional to the total number of nodes (O(n))."
[OK] Correct: Because BST allows skipping large parts of the tree, the search only follows one path down, so it depends on tree height, not total nodes.
Understanding how tree height affects search time helps you explain why balanced trees are important and shows you can analyze real code efficiently.
"What if the BST is not balanced and becomes 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 largest value 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 ceil or unrelated concepts.Final Answer:
The greatest value in the BST less than or equal to the given value -> Option CQuick Check:
Floor = greatest ≤ value [OK]
- Confusing floor with ceil
- Thinking floor is always smaller than the value
- Assuming floor is the root value
Solution
Step 1: Understand method signature requirements
The method should be public, return an int (the ceil value), and accept the BST root node and the key to find ceil for.Step 2: Match with options
public int FindCeil(Node root, int key) matches this: public int FindCeil(Node root, int key). int FindCeil(int key, Node root) has wrong parameter order, C returns void, D misses root parameter.Final Answer:
public int FindCeil(Node root, int key) -> Option DQuick Check:
Correct signature returns int and takes root, key [OK]
- Wrong parameter order
- Using void return type
- Missing root parameter
10
/ \
5 15
/ \
12 20
Solution
Step 1: Understand ceil definition and BST structure
Ceil is the smallest value greater than or equal to 14. The BST nodes are 10, 5, 15, 12, 20.Step 2: Find values ≥ 14 and pick smallest
Values ≥ 14 are 15 and 20. The smallest among these is 15.Final Answer:
15 -> Option BQuick Check:
Ceil(14) = 15 [OK]
- Choosing 12 which is less than 14
- Choosing 20 which is greater but not smallest
- Confusing ceil with floor
int FindFloor(Node root, int key) {
if (root == null) return -1;
if (root.val == key) return root.val;
if (root.val > key) return FindFloor(root.left, key);
int floorVal = FindFloor(root.right, key);
return (floorVal <= key && floorVal != -1) ? floorVal : root.val;
}
What is the main issue causing incorrect floor results?Solution
Step 1: Analyze the floorVal condition
The code checks if floorVal ≤ key and floorVal != -1 to decide return value. But floorVal could be less than root.val and still valid floor.Step 2: Identify the incorrect logic
The condition should check if floorVal is greater than root.val to pick the better floor, but it doesn't, causing wrong floor return.Final Answer:
The condition to check floorVal is incorrect and may return wrong value -> Option AQuick Check:
Incorrect floorVal check causes wrong floor [OK]
- Ignoring comparison with root.val
- Returning -1 as valid floor
- Not handling null root properly
Solution
Step 1: Consider efficiency of approaches
Two separate traversals or converting to array add extra time or space. BFS is not suitable for ordered search.Step 2: Single traversal updating floor and ceil
During one BST traversal, compare node values with key to update floor (largest ≤ key) and ceil (smallest ≥ key) efficiently.Final Answer:
Traverse the BST once, updating floor and ceil candidates during traversal -> Option AQuick Check:
Single traversal updates floor and ceil efficiently [OK]
- Doing two separate traversals wasting time
- Using BFS which ignores BST order
- Converting BST to array wastes space
Ready to prove it?
Pass the test and this pattern moves unknown
