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 floorInBST(Node* root, int key) {
int floorVal = -1;
while (root) {
if (root->data == key) return root->data;
if (root->data > key) root = root->left;
else {
floorVal = root->data;
root = root->right;
}
}
return floorVal;
}
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 using a while loop.
- How many times: At most once per tree level, moving left or right child each step.
As the BST grows, the number of steps depends on the tree height.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Up to 4 steps (if balanced) |
| 100 | Up to 7 steps (if balanced) |
| 1000 | Up to 10 steps (if balanced) |
Pattern observation: The steps grow slowly, roughly proportional to the tree height, not total nodes.
Time Complexity: O(h)
This means the time depends on the height of the BST, which is the number of levels from root to leaf.
[X] Wrong: "Finding floor or ceil always takes O(n) time because we might check every node."
[OK] Correct: The BST structure lets us skip large parts of the tree, so we only follow one path down, not all nodes.
Understanding how BST properties help reduce search time is a key skill. It shows you can use data structure features to write efficient code.
"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 is the greatest 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 ceil, while the remaining options 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
Node?Solution
Step 1: Analyze function parameters and return type
The function should take a pointer to the root node and the value to find ceil for, returning an integer ceil value.Step 2: Check each option
int ceilBST(Node* root, int val); matches correct signature: returns int, takes Node* and int. Others have wrong parameter order, missing pointers, or wrong return type.Final Answer:
int ceilBST(Node* root, int val); -> Option DQuick Check:
Correct signature returns int and takes Node* root [OK]
- Passing Node by value instead of pointer
- Wrong parameter order
- Incorrect return type
10
/ \
5 15
/ \
12 20
Solution
Step 1: Locate value 14 in BST context
14 is not in BST. We look for smallest value ≥ 14.Step 2: Check nodes greater than or equal to 14
Nodes ≥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 larger but not smallest ≥14
- Confusing floor and ceil
int floorBST(Node* root, int val) {
if (!root) return -1;
if (root->data == val) return root->data;
if (root->data > val) return floorBST(root->left, val);
int floorRight = floorBST(root->right, val);
return (floorRight != -1 && floorRight <= val) ? floorRight : root->data;
}
What is the bug?Solution
Step 1: Understand the floor logic in code
If root->data > val, search left subtree. Else search right subtree and compare.Step 2: Identify incorrect return condition
The code returns root->data if floorRight > val. However, since floorRight is never > val (returns -1 or ≤ val), this never triggers, causing it to return -1 (when right subtree has no floor ≤ val) instead of root->data.Final Answer:
It returns root->data when floorRight is greater than val, which is incorrect -> Option CQuick Check:
Floor must be ≤ val, code returns invalid value [OK]
- Returning values greater than val as floor
- Missing null checks
- Ignoring recursion on left subtree
Solution
Step 1: Identify floor of 7 in BST
Floor is greatest value ≤7. Values ≤7 are 2,4,6. Greatest is 6.Step 2: Identify ceil of 7 in BST
Ceil is smallest value ≥7. Values ≥7 are 8,10,12,14. Smallest is 8.Final Answer:
Floor = 6, Ceil = 8 -> Option AQuick Check:
Floor ≤7 = 6, Ceil ≥7 = 8 [OK]
- Swapping floor and ceil values
- Assuming floor or ceil equals the value if not present
- Choosing values not in BST
Ready to prove it?
Pass the test and this pattern moves unknown
