0
0
DSA Typescriptprogramming~10 mins

Floor and Ceil in BST in DSA Typescript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Floor and Ceil in BST
Start at root
Compare target with current node
Go right
Update floor
Repeat until leaf
Return floor and ceil
Start at the root, compare target with current node value, move left or right updating floor or ceil until reaching a leaf, then return the found floor and ceil.
Execution Sample
DSA Typescript
function floorCeilBST(root, target) {
  let floor = null, ceil = null;
  let current = root;
  while (current) {
    if (current.val === target) return { floor: current.val, ceil: current.val };
    else if (current.val < target) {
      floor = current.val;
      current = current.right;
    } else {
      ceil = current.val;
      current = current.left;
    }
  }
  return { floor, ceil };
}
Find floor and ceil of target in BST by traversing from root, updating floor or ceil based on comparison.
Execution Table
StepOperationCurrent NodeComparisonFloorCeilPointer MoveVisual State
1Start at root1010 vs 14nullnullMove right10
2Compare1515 vs 1410nullMove left10 -> 15
3Compare1212 vs 1412nullMove right10 -> 15 -> 12
4Compare1313 vs 1413nullMove right10 -> 15 -> 12 -> 13
5ComparenullReached leaf13nullStop10 -> 15 -> 12 -> 13
6ReturnN/AN/A13nullN/AFloor=13, Ceil=null
💡 Reached leaf node with no further children to explore.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5Final
floornullnull1012131313
ceilnullnullnullnullnullnullnull
currentroot(10)151213nullnullnull
Key Moments - 3 Insights
Why do we update floor only when current node value is less than target?
Because floor is the greatest value less than or equal to target. When current node is less than target, it can be a candidate floor. See execution_table steps 2-4 where floor updates.
Why do we move left when current node value is greater than target?
Because all values in left subtree are smaller, so potential ceil (smallest value >= target) might be there. See execution_table step 2 where we move left after finding 15 > 14.
What if target equals a node value?
We return that node value as both floor and ceil immediately, since exact match means floor and ceil are the same. This is shown in the code but not in this trace.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the floor value?
A10
B12
Cnull
D13
💡 Hint
Check the 'Floor' column at step 3 in execution_table.
At which step does the pointer move to a null node, stopping the traversal?
AStep 4
BStep 5
CStep 3
DStep 2
💡 Hint
Look at the 'Current Node' and 'Pointer Move' columns in execution_table.
If the target was 15 instead of 14, what would be the returned floor and ceil?
AFloor=15, Ceil=15
BFloor=13, Ceil=15
CFloor=14, Ceil=15
DFloor=10, Ceil=12
💡 Hint
Recall the code returns exact match as floor and ceil immediately.
Concept Snapshot
Floor and Ceil in BST:
- Floor: greatest value <= target
- Ceil: smallest value >= target
- Start at root, compare target with node
- Move right if node < target (update floor)
- Move left if node > target (update ceil)
- Stop at leaf, return floor and ceil
Full Transcript
To find floor and ceil in a Binary Search Tree (BST), start at the root node. Compare the target value with the current node's value. If the current node's value equals the target, return it as both floor and ceil. If the current node's value is less than the target, update floor to current node's value and move to the right child. If greater, update ceil to current node's value and move to the left child. Repeat until reaching a leaf node (null). Then return the last updated floor and ceil values. This process efficiently finds the closest values less than or equal to (floor) and greater than or equal to (ceil) the target in the BST.