0
0
DSA Javascriptprogramming~10 mins

Floor and Ceil in BST in DSA Javascript - Execution Trace

Choose your learning style9 modes available
Concept Flow - Floor and Ceil in BST
Start at root node
Compare target with current node value
Go left
Update floor
Repeat until leaf node
Return floor and ceil values
Start from the root, compare target with node value, move left or right updating floor or ceil until no more nodes to visit.
Execution Sample
DSA Javascript
function floorCeilBST(root, target) {
  let floor = null, ceil = null;
  let current = root;
  while (current) {
    if (current.val === target) return { floor: target, ceil: target };
    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 and updating floor or ceil accordingly.
Execution Table
StepOperationCurrent Node ValueTargetFloorCeilPointer MoveVisual State
1Start at root86nullnullCompare 6 with 88 / \ 3 10 / \ 1 6 14
2target=6 < current=8, update ceil=886null8Move left8 / \ 3 10 / \ 1 6 14
3At node 3, 3 < 63638Move right8 / \ 3 10 / \ 1 6 14
4At node 6, 6 == 66666Found exact match8 / \ 3 10 / \ 1 6 14
5Return floor and ceil-666Stop8 / \ 3 10 / \ 1 6 14
💡 Exact match found at node with value 6, floor and ceil both are 6.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
floornullnullnull366
ceilnullnull8866
currentroot(8)8366null
Key Moments - 3 Insights
Why do we update floor when current node value is less than target?
Because floor is the greatest value less than or equal to target. When current.val < target, current.val can be a candidate floor. See execution_table step 3 where floor updates to 3.
Why do we update ceil when current node value is greater than target?
Because ceil is the smallest value greater than or equal to target. When current.val > target, current.val can be a candidate ceil. See execution_table step 2 where ceil updates to 8.
What happens if target matches a node value exactly?
We return that value as both floor and ceil immediately. See execution_table step 4 where current.val == target, so floor and ceil both become 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the floor value after step 3?
Anull
B6
C3
D8
💡 Hint
Check the 'Floor' column in execution_table row for step 3.
At which step does the algorithm find an exact match for the target?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look for the step where current node value equals target in execution_table.
If the target was 5 instead of 6, what would be the ceil after step 3?
A3
B8
C6
Dnull
💡 Hint
Check the 'Ceil' column in execution_table and variable_tracker after step 3.
Concept Snapshot
Floor and Ceil in BST:
- Floor: greatest value ≤ target
- Ceil: smallest value ≥ target
- Start at root, compare target with node
- Move left if node ≥ target (update ceil)
- Move right if node < target (update floor)
- Stop when node is null or exact match found
Full Transcript
To find floor and ceil in a BST, start at the root node. Compare the target 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 right. If greater, update ceil and move left. Repeat until no more nodes to visit. The final floor and ceil are returned. This method efficiently uses BST properties to find closest values.