Bird
Raised Fist0
DSA C++programming~30 mins

Floor and Ceil in BST in DSA C++ - Build from Scratch

Choose your learning style10 modes available
Floor and Ceil in BST
📖 Scenario: You are working with a Binary Search Tree (BST) that stores integer values. You want to find two special values for a given number:Floor: The greatest value in the BST less than or equal to the given number.Ceil: The smallest value in the BST greater than or equal to the given number.This is useful in many real-world cases like searching for closest available prices or dates.
🎯 Goal: Build a program that creates a BST with given values, sets a target number, and finds the floor and ceil values in the BST for that target.
📋 What You'll Learn
Create a BST by inserting nodes with exact values: 8, 4, 12, 2, 6, 10, 14
Create an integer variable called target and set it to 5
Write two functions findFloor and findCeil that find floor and ceil values in the BST for target
Print the floor and ceil values exactly as: Floor: X and Ceil: Y
💡 Why This Matters
🌍 Real World
Finding floor and ceil values in sorted data is common in price matching, scheduling, and recommendation systems.
💼 Career
Understanding BST operations and search algorithms is essential for software engineering roles involving data structures and algorithm optimization.
Progress0 / 4 steps
1
Create the BST structure and insert nodes
Create a struct called Node with int data, Node* left, and Node* right. Then write a function insertNode that takes Node* root and int val and inserts a new node with val into the BST. Finally, create a Node* root and insert these values in order: 8, 4, 12, 2, 6, 10, 14.
DSA C++
Hint

Start by defining the Node structure with data and pointers to left and right children. Then write a recursive insert function that places values correctly in the BST. Finally, create the root pointer and insert the given values one by one.

2
Set the target value to find floor and ceil
Create an integer variable called target and set it to 5 inside main() after the BST is created.
DSA C++
Hint

Just add a line inside main() to create an integer variable named target and assign it the value 5.

3
Write functions to find floor and ceil in BST
Write two functions: int findFloor(Node* root, int target) and int findCeil(Node* root, int target). Each function should return the floor or ceil value for target in the BST. Use the BST property to traverse efficiently. Return -1 if no floor or ceil exists.
DSA C++
Hint

Use a loop to traverse the BST. For floor, move left if current node is greater than target, else update floor and move right. For ceil, move right if current node is less than target, else update ceil and move left. Return -1 if no suitable value found.

4
Print the floor and ceil values
In main(), after calling findFloor and findCeil, print the results exactly as: Floor: X and Ceil: Y where X and Y are the floor and ceil values found.
DSA C++
Hint

Use cout to print the floor and ceil values with the exact format: Floor: X and Ceil: Y.

Practice

(1/5)
1. What does the floor of a value in a Binary Search Tree (BST) represent?
easy
A. The greatest value in the BST less than or equal to the given value
B. The smallest value in the BST greater than or equal to the given value
C. The root value of the BST
D. The average of all values in the BST

Solution

  1. Step 1: Understand the definition of floor in BST

    The floor of a value is the greatest value in the BST that is less than or equal to the given value.
  2. Step 2: Compare with other options

    The smallest value in the BST greater than or equal to the given value describes ceil, while the remaining options are unrelated to floor concept.
  3. Final Answer:

    The greatest value in the BST less than or equal to the given value -> Option A
  4. Quick Check:

    Floor = greatest ≤ value [OK]
Hint: Floor is largest value ≤ target in BST [OK]
Common Mistakes:
  • Confusing floor with ceil
  • Thinking floor is the smallest value
  • Assuming floor is always the root
2. Which of the following is the correct C++ function signature to find the ceil of a value in a BST node structure named Node?
easy
A. int ceilBST(int val);
B. void ceilBST(Node root, int val);
C. Node* ceilBST(int val, Node* root);
D. int ceilBST(Node* root, int val);

Solution

  1. Step 1: Analyze function parameters and return type

    The function should take a pointer to the root node and the value to find ceil for, returning an integer ceil value.
  2. Step 2: Check each option

    int ceilBST(Node* root, int val); matches correct signature: returns int, takes Node* and int. Others have wrong parameter order, missing pointers, or wrong return type.
  3. Final Answer:

    int ceilBST(Node* root, int val); -> Option D
  4. Quick Check:

    Correct signature returns int and takes Node* root [OK]
Hint: Function needs root pointer and value, returns int [OK]
Common Mistakes:
  • Passing Node by value instead of pointer
  • Wrong parameter order
  • Incorrect return type
3. Given the BST below and the value 14, what is the ceil value?
        10
       /  \
      5    15
          /  \
         12   20
medium
A. 12
B. 15
C. 20
D. 10

Solution

  1. Step 1: Locate value 14 in BST context

    14 is not in BST. We look for smallest value ≥ 14.
  2. Step 2: Check nodes greater than or equal to 14

    Nodes ≥14 are 15 and 20. The smallest among these is 15.
  3. Final Answer:

    15 -> Option B
  4. Quick Check:

    Ceil(14) = 15 [OK]
Hint: Ceil is smallest node ≥ target [OK]
Common Mistakes:
  • Choosing 12 which is less than 14
  • Choosing 20 which is larger but not smallest ≥14
  • Confusing floor and ceil
4. Consider this buggy C++ code snippet to find floor in BST:
int floorBST(Node* root, int val) {
  if (!root) return -1;
  if (root->data == val) return root->data;
  if (root->data > val) return floorBST(root->left, val);
  int floorRight = floorBST(root->right, val);
  return (floorRight != -1 && floorRight <= val) ? floorRight : root->data;
}
What is the bug?
medium
A. It always returns root->data without recursion
B. It does not check for null root
C. It returns root->data when floorRight is greater than val, which is incorrect
D. It returns -1 for all values

Solution

  1. Step 1: Understand the floor logic in code

    If root->data > val, search left subtree. Else search right subtree and compare.
  2. Step 2: Identify incorrect return condition

    The code returns root->data if floorRight > val. However, since floorRight is never > val (returns -1 or ≤ val), this never triggers, causing it to return -1 (when right subtree has no floor ≤ val) instead of root->data.
  3. Final Answer:

    It returns root->data when floorRight is greater than val, which is incorrect -> Option C
  4. Quick Check:

    Floor must be ≤ val, code returns invalid value [OK]
Hint: Floor must never exceed val; check return conditions [OK]
Common Mistakes:
  • Returning values greater than val as floor
  • Missing null checks
  • Ignoring recursion on left subtree
5. You have a BST with nodes: 8, 4, 12, 2, 6, 10, 14. You want to find both floor and ceil for value 7. Which pair is correct?
hard
A. Floor = 6, Ceil = 8
B. Floor = 8, Ceil = 6
C. Floor = 4, Ceil = 10
D. Floor = 7, Ceil = 7

Solution

  1. Step 1: Identify floor of 7 in BST

    Floor is greatest value ≤7. Values ≤7 are 2,4,6. Greatest is 6.
  2. Step 2: Identify ceil of 7 in BST

    Ceil is smallest value ≥7. Values ≥7 are 8,10,12,14. Smallest is 8.
  3. Final Answer:

    Floor = 6, Ceil = 8 -> Option A
  4. Quick Check:

    Floor ≤7 = 6, Ceil ≥7 = 8 [OK]
Hint: Floor ≤ val, Ceil ≥ val; pick closest values [OK]
Common Mistakes:
  • Swapping floor and ceil values
  • Assuming floor or ceil equals the value if not present
  • Choosing values not in BST
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard