0
0
DSA Typescriptprogramming

Floor and Ceil in BST in DSA Typescript

Choose your learning style9 modes available
Mental Model
Floor is the biggest value smaller or equal to target; ceil is the smallest value bigger or equal to target in a BST.
Analogy: Imagine a bookshelf sorted by book height. Floor is the tallest book not taller than your target height; ceil is the shortest book not shorter than your target height.
       8
      / \
     4   12
    / \   / \
   2   6 10  14
Dry Run Walkthrough
Input: BST: 8->4->12->2->6->10->14, find floor and ceil for 5
Goal: Find floor (largest ≤5) and ceil (smallest ≥5) in the BST
Step 1: Start at root 8, compare 5 with 8
Current node: 8
Why: We start from root to decide which subtree to explore
Step 2: 5 < 8, move to left child 4; update ceil to 8
Current node: 4, ceil=8
Why: Since 5 < 8, 8 is a candidate ceil; floor unknown yet
Step 3: 5 > 4, move to right child 6; update floor to 4
Current node: 6, floor=4, ceil=8
Why: Since 5 > 4, 4 is a candidate floor; check right subtree for closer values
Step 4: 5 < 6, move to left child null; update ceil to 6
Current node: null, floor=4, ceil=6
Why: Since 5 < 6, 6 is a better ceil candidate; no left child means stop
Result:
Floor: 4
Ceil: 6
Annotated Code
DSA Typescript
class TreeNode {
  val: number;
  left: TreeNode | null;
  right: TreeNode | null;
  constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
    this.val = val === undefined ? 0 : val;
    this.left = left === undefined ? null : left;
    this.right = right === undefined ? null : right;
  }
}

function floorInBST(root: TreeNode | null, target: number): number | null {
  let floor: number | null = null;
  let curr = root;
  while (curr !== null) {
    if (curr.val === target) {
      return curr.val; // exact match is floor
    } else if (curr.val < target) {
      floor = curr.val; // update floor
      curr = curr.right; // try to find closer floor
    } else {
      curr = curr.left; // go left to find smaller values
    }
  }
  return floor;
}

function ceilInBST(root: TreeNode | null, target: number): number | null {
  let ceil: number | null = null;
  let curr = root;
  while (curr !== null) {
    if (curr.val === target) {
      return curr.val; // exact match is ceil
    } else if (curr.val > target) {
      ceil = curr.val; // update ceil
      curr = curr.left; // try to find closer ceil
    } else {
      curr = curr.right; // go right to find bigger values
    }
  }
  return ceil;
}

// Build BST from example
const root = new TreeNode(8,
  new TreeNode(4,
    new TreeNode(2),
    new TreeNode(6)
  ),
  new TreeNode(12,
    new TreeNode(10),
    new TreeNode(14)
  )
);

const target = 5;
const floorValue = floorInBST(root, target);
const ceilValue = ceilInBST(root, target);
console.log(`Floor: ${floorValue}`);
console.log(`Ceil: ${ceilValue}`);
while (curr !== null) {
Traverse nodes until no more to check
if (curr.val === target) { return curr.val; }
Exact match found, return immediately
else if (curr.val < target) { floor = curr.val; curr = curr.right; }
Update floor and move right to find closer floor
else { curr = curr.left; }
Move left to find smaller values
else if (curr.val > target) { ceil = curr.val; curr = curr.left; }
Update ceil and move left to find closer ceil
else { curr = curr.right; }
Move right to find bigger values
OutputSuccess
Floor: 4 Ceil: 6
Complexity Analysis
Time: O(h) because we traverse from root down to leaf at most once, where h is tree height
Space: O(1) because we use only a few variables, no extra data structures
vs Alternative: Compared to scanning all nodes O(n), this uses BST property to reduce search to O(h)
Edge Cases
target smaller than all nodes
floor returns null, ceil returns smallest node
DSA Typescript
return floor;
target larger than all nodes
floor returns largest node, ceil returns null
DSA Typescript
return ceil;
target equals a node value
floor and ceil both return that node value immediately
DSA Typescript
if (curr.val === target) { return curr.val; }
When to Use This Pattern
When asked to find closest smaller or larger values in a BST, use floor and ceil search by traversing left or right based on comparisons.
Common Mistakes
Mistake: Not updating floor or ceil before moving to child nodes
Fix: Always update floor or ceil when current node is a valid candidate before moving
Mistake: Searching entire tree instead of using BST property
Fix: Use BST ordering to decide left or right subtree to explore
Summary
Find the closest smaller or equal (floor) and closest larger or equal (ceil) values to a target in a BST.
Use when you need nearest bounds for a value in a sorted tree structure.
The key is to update floor or ceil candidates while moving down the tree using BST ordering.