What is the floor of a value in a Binary Search Tree (BST)?
The floor of a value in a BST is the greatest value in the BST that is less than or equal to the given value.
Click to reveal answer
beginner
What is the ceil of a value in a Binary Search Tree (BST)?
The ceil of a value in a BST is the smallest value in the BST that is greater than or equal to the given value.
Click to reveal answer
intermediate
How does the BST property help in finding floor and ceil efficiently?
Because BST nodes are ordered, we can decide to go left or right based on comparisons, skipping large parts of the tree and finding floor or ceil in O(h) time, where h is the tree height.
Click to reveal answer
intermediate
In finding the floor of a value in BST, what do you do when the current node's value is greater than the target?
You move to the left child because all values in the right subtree are greater, so they can't be floor candidates.
Click to reveal answer
intermediate
In finding the ceil of a value in BST, what do you do when the current node's value is less than the target?
You move to the right child because all values in the left subtree are smaller, so they can't be ceil candidates.
Click to reveal answer
What does the floor of a value in a BST represent?
AThe greatest value less than or equal to the target
BThe root value of the BST
CThe smallest value greater than or equal to the target
DThe smallest value in the BST
✗ Incorrect
Floor is the greatest value less than or equal to the target.
If the current node's value is equal to the target, what is the floor and ceil?
AFloor is left child, ceil is right child
BFloor and ceil do not exist
CFloor is right child, ceil is left child
DBoth floor and ceil are the current node's value
✗ Incorrect
If the node's value equals the target, both floor and ceil are that value.
When searching for ceil in BST, if current node's value is less than target, where do you go next?
ARight child
BReturn current node
CStop search
DLeft child
✗ Incorrect
Go right because ceil must be greater or equal to target.
What is the time complexity of finding floor or ceil in a balanced BST?
AO(n log n)
BO(n)
CO(log n)
DO(1)
✗ Incorrect
Balanced BST allows O(log n) search time.
If no value in BST is less than or equal to the target, what is the floor?
AThe largest value in BST
BNo floor exists (null)
CThe smallest value in BST
DThe root value
✗ Incorrect
Floor does not exist if no value ≤ target.
Explain how to find the floor of a given value in a BST step-by-step.
Think about moving left or right depending on comparison.
You got /6 concepts.
Describe the difference between floor and ceil in a BST with examples.
Use simple numbers to illustrate.
You got /5 concepts.
Practice
(1/5)
1. What does the floor of a value in a Binary Search Tree (BST) represent?
easy
A. The smallest value in the BST greater than or equal to the given value
B. The root value of the BST
C. The greatest value in the BST less than or equal to the given value
D. The smallest value in the BST less than the given value
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 C
Quick Check:
Floor = greatest ≤ value [OK]
Hint: Floor is largest value ≤ target in BST [OK]
Common Mistakes:
Confusing floor with ceil
Thinking floor is the smallest value
Assuming floor is always the root
2. Which of the following is the correct C function signature to find the ceil of a value in a BST node structure named Node?
easy
A. int ceilBST(Node* root, int key);
B. void ceilBST(Node root, int key);
C. Node* ceilBST(int key, Node* root);
D. int ceilBST(int key);
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 takes Node* root and int key, returns int. Others have wrong parameter order, types, or return type.
Final Answer:
int ceilBST(Node* root, int key); -> Option A
Quick Check:
Correct signature: pointer root, key, returns int [OK]
Hint: Function takes root pointer and key, returns int [OK]
Common Mistakes:
Passing root by value instead of pointer
Wrong parameter order
Returning Node* instead of int
3. Given the BST below and the key = 14, what is the output of the ceil function?
10
/ \
5 15
/ \
12 20
medium
A. -1
B. 15
C. 12
D. 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 B
Quick Check:
Ceil(14) = 15 [OK]
Hint: Ceil is smallest node ≥ key, check right subtree carefully [OK]
Common Mistakes:
Choosing 20 instead of 15
Choosing 12 which is < 14
Returning -1 when ceil exists
4. Consider the following buggy C code snippet to find floor in BST. What is the main error?
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;
}
medium
A. It returns root->data even if floorValue is -1
B. It does not check for NULL root
C. It returns -1 when key is found
D. It incorrectly searches left subtree when root->data < key
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 A
Quick Check:
Missing check before returning root->data causes wrong floor [OK]
Hint: Check root->data ≤ key before returning it [OK]
Common Mistakes:
Ignoring floorValue == -1 case
Returning wrong subtree
Not handling NULL root
5. You have a BST with nodes: 8, 4, 12, 2, 6, 10, 14. You want to find the floor and ceil of 5. Which pair is correct?
hard
A. Floor = 5, Ceil = 5
B. Floor = 6, Ceil = 4
C. Floor = 2, Ceil = 8
D. Floor = 4, Ceil = 6
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.