Floor and Ceil in BST in DSA C - 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.
int floorBST(Node* root, int key) {
int floor = -1;
while (root) {
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 traversing nodes.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing down the BST nodes one by one.
- How many times: At most once per tree level, moving left or right each step.
Each step moves down one level of the tree, so the number of steps depends on the tree height.
| Input Size (n) | Approx. Operations (steps) |
|---|---|
| 10 | Up to 4 (if balanced) |
| 100 | Up to 7 (if balanced) |
| 1000 | Up to 10 (if balanced) |
Pattern observation: Steps grow slowly with input size if tree is balanced, but can be as large as n if unbalanced.
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.
It shows you can use tree structure to find answers faster by skipping unnecessary parts.
"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 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 CQuick Check:
Floor = greatest ≤ value [OK]
- Confusing floor with ceil
- Thinking floor is the smallest value
- Assuming floor is always the root
Node?Solution
Step 1: Analyze function parameters and return type
The function should take a pointer to the root node and the key to find ceil for, returning an integer value.Step 2: Match with options
int ceilBST(Node* root, int key); matches this: it takesNode* rootandint key, returnsint. Others have wrong parameter order, types, or return type.Final Answer:
int ceilBST(Node* root, int key); -> Option AQuick Check:
Correct signature: pointer root, key, returns int [OK]
- Passing root by value instead of pointer
- Wrong parameter order
- Returning Node* instead of int
10
/ \
5 15
/ \
12 20
Solution
Step 1: Understand ceil definition and traverse BST
Ceil is smallest value ≥ key. Starting at root 10, since 14 > 10, move right to 15. 15 ≥ 14, so candidate ceil is 15. Check left subtree of 15 (12) which is < 14, so ignore.Step 2: Confirm smallest candidate
15 is the smallest node ≥ 14. 20 is larger but not smaller than 15, so 15 is ceil.Final Answer:
15 -> Option BQuick Check:
Ceil(14) = 15 [OK]
- Choosing 20 instead of 15
- Choosing 12 which is < 14
- Returning -1 when ceil exists
int floorBST(Node* root, int key) {
if (!root) return -1;
if (root->data == key) return root->data;
if (root->data > key) return floorBST(root->left, key);
int floorValue = floorBST(root->right, key);
if (floorValue <= key && floorValue != -1) return floorValue;
else return root->data;
}
Solution
Step 1: Analyze return conditions
If floorValue is -1 (meaning no floor found in right subtree), the code returns root->data without checking if root->data ≤ key, which can be incorrect.Step 2: Identify the error
The code should check if root->data ≤ key before returning it; otherwise, it may return a value greater than key, violating floor definition.Final Answer:
It returns root->data even if floorValue is -1 -> Option AQuick Check:
Missing check before returning root->data causes wrong floor [OK]
- Ignoring floorValue == -1 case
- Returning wrong subtree
- Not handling NULL root
Solution
Step 1: Identify floor of 5 in BST
Floor is greatest value ≤ 5. Nodes ≤ 5 are 2 and 4. Greatest is 4.Step 2: Identify ceil of 5 in BST
Ceil is smallest value ≥ 5. Nodes ≥ 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 wrong closest values
Ready to prove it?
Pass the test and this pattern moves unknown
