0
0
DSA C++programming~10 mins

Floor and Ceil in BST in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Floor and Ceil in BST
Start at root
Compare key with current node
Floor = Ceil = node value
Done
Go to left subtree
Record node value as potential floor
Go to right subtree
Repeat until no more nodes
Return recorded floor or ceil value
Start at the root and compare the key with current node value. Move left if key is smaller, move right if key is larger, updating floor or ceil candidates until traversal ends.
Execution Sample
DSA C++
struct Node {
  int val;
  Node* left;
  Node* right;
};

int floorBST(Node* root, int key) {
  int floor = -1;
  while (root) {
    if (root->val == key) return root->val;
    if (root->val > key) root = root->left;
    else {
      floor = root->val;
      root = root->right;
    }
  }
  return floor;
}
Finds the floor value (largest value <= key) in a BST by traversing from root and updating floor candidate.
Execution Table
StepOperationCurrent Node ValueKeyFloor CandidateCeil CandidatePointer MoveVisual State
1Start at root2018-1nullCompare 18 with 2020 / \ 10 30
2Key < node value2018-120Move to left child20 / \ 10 30
3At node 101018-120Compare 18 with 1020 / \ 10 30
4Key > node value10181020Move to right child20 / \ 10 30
5At node 1515181020Compare 18 with 1520 / \ 10 30 \ 15
6Key > node value15181520Move to right child20 / \ 10 30 \ 15
7At node nullnull181520No node, stop20 / \ 10 30 \ 15
8Return floornull181520Floor found: 15Final floor = 15
9Return ceilnull181520Ceil found: 20Final ceil = 20
💡 Traversal ends when pointer reaches null; floor and ceil candidates recorded during traversal.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
Current Node201015nullnull
Floor Candidate-1-1101515
Ceil Candidatenull20202020
Key Moments - 3 Insights
Why do we update the floor candidate only when current node value is less than the key?
Because floor is the largest value less than or equal to the key. When current node value is less than key, it can be a floor candidate. This is shown in execution_table rows 4 and 6 where floor updates to 10 and then 15.
Why do we move left when current node value is greater than the key?
Because all values in the right subtree will be even larger, so to find a floor or ceil closer to key, we move left. This is shown in execution_table row 2 where pointer moves left from 20 to 10.
How do we find the ceil value during traversal?
We update the ceil candidate when current node value is greater than or equal to the key. In this example, ceil is updated to 20 at step 2 and remains until traversal ends, as shown in variable_tracker.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the floor candidate value?
A10
B-1
C15
D20
💡 Hint
Check the 'Floor Candidate' column at step 4 in the execution_table.
At which step does the pointer move to a null node, ending traversal?
AStep 5
BStep 6
CStep 7
DStep 8
💡 Hint
Look for 'At node null' in the 'Current Node Value' column in execution_table.
If the key was 25 instead of 18, how would the floor candidate change at step 6?
AIt would remain 15
BIt would update to 20
CIt would update to 30
DIt would be -1
💡 Hint
Consider how floor updates when current node value is less than key, check variable_tracker for floor candidate updates.
Concept Snapshot
Floor and Ceil in BST:
- Floor: largest value <= key
- Ceil: smallest value >= key
- Start at root, compare key with node
- Move left if key < node, update ceil
- Move right if key > node, update floor
- Stop when no more nodes
- Return recorded floor and ceil
Full Transcript
To find floor and ceil in a BST, start at the root node. Compare the key with the current node's value. If equal, floor and ceil are the node's value. If key is less, move to the left child and update ceil candidate. If key is greater, move to the right child and update floor candidate. Continue until no nodes remain. The last recorded floor and ceil candidates are the results. This process efficiently finds the closest values less than or equal to (floor) and greater than or equal to (ceil) the key.