What if you could find the closest smaller or bigger number instantly without checking every single one?
Why Floor and Ceil in BST in DSA C?
Imagine you have a sorted list of numbers on paper, and you want to find the closest number less than or equal to a target (floor), or the closest number greater than or equal to the target (ceil). Doing this by scanning the list every time is slow and tiring.
Manually scanning a list for floor or ceil means checking each number one by one. This is slow for large lists and easy to make mistakes, especially if the list is very long or changes often.
A Binary Search Tree (BST) organizes numbers so you can quickly find floor and ceil by moving left or right based on comparisons. This saves time and reduces errors.
int floor = -1; for (int i = 0; i < n; i++) { if (arr[i] <= target) floor = arr[i]; }
int floor = findFloor(root, target);
This lets you quickly find closest smaller or larger values in sorted data, enabling fast decisions and queries.
When booking a flight, you want the closest cheaper or slightly more expensive ticket than your budget. Floor and ceil in BST help find these prices fast.
Manual search is slow and error-prone for floor and ceil.
BST structure speeds up finding floor and ceil efficiently.
Useful for quick range queries and decision making.
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
