Floor and Ceil in BST in DSA Python - 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.
def floor_in_bst(root, key):
floor_val = None
while root:
if root.val == key:
return root.val
elif root.val > key:
root = root.left
else:
floor_val = root.val
root = root.right
return floor_val
This code finds the floor value (largest value <= key) in a BST by traversing from root down.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Traversing down the BST nodes from root to leaf.
- How many times: At most once per level of the tree, until a leaf or exact match 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 (steps down tree) |
|---|---|
| 10 | Up to 4 (if balanced) |
| 100 | Up to 7 (if balanced) |
| 1000 | Up to 10 (if balanced) |
Pattern observation: In a balanced BST, steps grow slowly (logarithmically) as tree size grows.
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: In a BST, we skip large parts of the tree by moving left or right, so we only visit nodes along one path, not all 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 but skewed 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 floor concept in BST
The floor of a value is the largest value in the BST that is less than or equal to the given value.Step 2: Compare with other options
The ceil is the smallest value greater than or equal to the given value, so The smallest value in the BST greater than or equal to the given value is incorrect. Options C and D do not relate to floor.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
Solution
Step 1: Check common parameter order for BST functions
Functions usually take the root node first, then the value to search for.Step 2: Validate each option
def find_ceil(root, value): matches this order and naming convention. Options B, C, and D either have wrong parameter order or missing parameters.Final Answer:
def find_ceil(root, value): -> Option BQuick Check:
Function(root, value) is standard [OK]
- Swapping parameter order
- Missing one parameter
- Using unclear function names
15
/ \
10 20
/ \ \
8 12 25
Solution
Step 1: Locate values less than or equal to 14 in BST
Values ≤ 14 are 10, 8, 12. 15 and above are greater.Step 2: Find the greatest among these
The greatest value ≤ 14 is 12.Final Answer:
12 -> Option CQuick Check:
Floor(14) = 12 [OK]
- Choosing 15 which is greater than 14
- Assuming floor equals the value if not present
- Picking smallest value instead of largest ≤ value
def find_ceil(root, value):
if root is None:
return None
if root.val == value:
return root.val
if root.val < value:
return find_ceil(root.right, value)
else:
ceil = find_ceil(root.left, value)
return ceil if ceil is not None else root.val
What is the main error?Solution
Step 1: Analyze recursion direction for ceil
If root.val < value, ceil must be in right subtree, so recurse on root.right.Step 2: Identify incorrect recursion
Code incorrectly recurses on root.left when root.val < value, which is wrong.Final Answer:
It should recurse on root.right when root.val < value -> Option DQuick Check:
Ceil search goes right if root.val < value [OK]
- Recursing left instead of right
- Confusing floor and ceil logic
- Not handling None cases properly
Solution
Step 1: Identify floor of 17 in BST
Floor is greatest value ≤ 17. Values ≤ 17 are 5, 10, 15. Greatest is 15.Step 2: Identify ceil of 17 in BST
Ceil is smallest value ≥ 17. Values ≥ 17 are 20, 25. Smallest is 20.Final Answer:
Floor = 15, Ceil = 20 -> Option AQuick Check:
Floor ≤ 17 = 15, Ceil ≥ 17 = 20 [OK]
- Choosing floor or ceil outside BST values
- Assuming floor or ceil equals the value if not present
- Mixing floor and ceil values
Ready to prove it?
Pass the test and this pattern moves unknown
