Javatpoint Logo
Javatpoint Logo

Floor and Ceiling in Binary Search Tree

In a binary search tree, the concept of floor and ceiling play a vital role in looking for the elements that may not exist in the tree. The floor of the given value in the binary search tree points out the biggest value that is smaller than or equal to the surfacing value. On the other hand, the ceiling represents the smallest element that is considered bigger than or equal to the allotted value.

In technical terms, if all the values are organized in a sorted manner, then the value of the floor will be the value that is immediately preceding it. On the other hand, the ceiling is the value that is immediately followed.

Some advantages of using floor and ceiling are: -

  1. The floor and ceiling help us in finding out the closest value when it comes to finding out a target in the binary search tree.
  2. Another advantage of the floor and ceiling is that it helps us determine the closest value in a range of datasets.
  3. They prove to be most significant when it comes to neighbor searching; by finding out the closest value to the target, we can narrow down to the search space and find the value.

Implementation

Output

Floor and Ceiling in Binary Search Tree

A step-by-step explanation of the code

  1. The code begins by importing the necessary header files of the C++ libraries.
  2. The code then emphasizes defining a class named 'Node', which contains the data value and the pointers to the left and right child.
  3. It defines a new helper function to help us allocate a new node with the given value and memory.
  4. The 'Ceil' function is defined to find the ceiling of the given binary tree. It has two parameters: root and input. It returns the -1 value or the ceiling value if the BST appears to be empty.
  5. Now, we have to check the base case. If the tree is empty, return -1.
  6. Now, verify whether the crucial value is equal to the input value. The input value is present if both these values are equal, and the root key is returned as the ceiling value.
  7. If the root key is smaller than the input value, then the ceiling value is present in the right subtree, and if it is larger, it is in the left subtree.
  8. In the program's main function, the binary tree is created with a root node having the value 8.
  9. A loop is executed from 0 to 15 to determine each number's ceiling value in the range.
  10. Finally, the program returns 0, indicating successful execution of the program.

Example 2)

Output

Floor and Ceiling in Binary Search Tree

A step-by-step explanation of the code

  1. The code begins by importing the necessary header files, such as "stdio. h" and "stdlib.h".
  2. The code then emphasizes defining a structure for a binary tree node named 'struct', which contains the data value and the pointers to the left and right child.
  3. It defines a new helper function to help us allocate a new node with the given value and memory.
  4. The 'Ceil' function is defined to find the ceiling of the given binary tree. It has two parameters: root and input. It returns the -1 value or the ceiling value if the BST appears to be empty.
  5. Now, we have to check the base case. If the tree is empty, return -1.
  6. Now, verify whether the crucial value is equal to the input value. The input value is present if both these values are equal, and the root key is returned as the ceiling value.
  7. If the root key is smaller than the input value, then the ceiling value is present in the right subtree, and if it is larger, it is in the left subtree.
  8. The binary tree is created in the program's primary function with a root node having the value 8.
  9. A loop is executed from 0 to 15 to determine each number's ceiling value in the range.
  10. Finally, the program returns 0, indicating successful execution of the program.

Example 3)

Output

Floor and Ceiling in Binary Search Tree

A step-by-step explanation of the code

  1. The code begins by defining the 'node' and 'Binary tree' classes. The binary tree contains BST itself and a reference root node.
  2. The code then emphasizes defining a class named 'Node', which contains the data value and the pointers to the left and right child.
  3. It defines a new helper function to help us allocate a new node with the given value and memory.
  4. The 'Ceil' function is defined to find the ceiling of the given binary tree. It has two parameters: root and input. It returns the -1 value or the ceiling value if the BST appears to be empty.
  5. Now, we have to check the base case; if the tree is empty, return -1.
  6. Now, verify whether the critical value is equal to the input value. The input value is present if both these values are equal, and the root key is returned as the ceiling value.
  7. If the root key is smaller than the input value, then the ceiling value is present in the right subtree, and if it is larger, it is in the left subtree.
  8. In the program's primary function, the binary tree is created with a root node having the value 8.
  9. A loop is executed from 0 to 15 to determine each number's ceiling value in the range.
  10. Finally, the program returns 0, indicating successful execution of the program.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA