Given a binary tree and a key(node) value, find the floor and ceil value for that particular key value.
Floor Value Node: Node with greatest data lesser than or equal to key value.
Ceil Value Node: Node with smallest data larger than or equal to key value.
For example, Let’s consider the Binary Tree below –
8 / \ 4 12 / \ / \ 2 6 10 14 Key: 11 Floor: 10 Ceil: 12 Key: 1 Floor: -1 Ceil: 2 Key: 6 Floor: 6 Ceil: 6 Key: 15 Floor: 14 Ceil: -1
There are numerous applications where we need to find floor / ceil value of a key in a binary search tree or sorted array. For example, consider designing memory management system in which free nodes are arranged in BST. Find the best fit for the input request.
Imagine we are moving down the tree, and assume we are root node. The comparison yields three possibilities, A) Root data is equal to key. We are done, root data is ceil value. B) Root data < key value, certainly the ceil value can't be in left subtree. Proceed to search on right subtree as reduced problem instance. C) Root data > key value, the ceil value may be in left subtree. We may find a node with is larger data than key value in left subtree, if not the root itself will be ceil node.
Here is the code for ceil value:
0 2 1 2 2 2 3 4 4 4 5 6 6 6 7 8 8 8 9 10 10 10 11 12 12 12 13 14 14 14 15 -1
Iterative Approach –
1. If tree is empty, i.e. root is null, return back to calling function. 2. If current node address is not null, perform the following steps : (a) If current node data matches with the key value - We have found both our floor and ceil value. Hence, we return back to calling function. (b) If data in current node is lesser than the key value - We assign the current node data to the variable keeping track of current floor value and explore the right subtree, as it may contain nodes with values greater than key value. (c) If data in current node is greater than the key value - We assign the current node data to the variable keeping track of current ceil value and explore the left subtree, as it may contain nodes with values lesser than key value. 3. Once we reach null, we return back to the calling function, as we have got our required floor and ceil values for the particular key value.
Below is the implementation of the above approach:
0 -1 2 1 -1 2 2 2 2 3 2 4 4 4 4 5 4 6 6 6 6 7 6 8 8 8 8 9 8 10 10 10 10 11 10 12 12 12 12 13 12 14 14 14 14 15 14 -1
Time Complexity: O(N)
Space Complexity: O(1)
1. Modify above code to find floor value of input key in a binary search tree.
2. Write neat algorithm to find floor and ceil values in a sorted array. Ensure to handle all possible boundary conditions.
This article is contributed by Venki. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Floor in Binary Search Tree (BST)
- K'th Largest Element in BST when modification to BST is not allowed
- Convert a normal BST to Balanced BST
- Two nodes of a BST are swapped, correct the BST | Set-2
- Two nodes of a BST are swapped, correct the BST
- Find k-th smallest element in BST (Order Statistics in BST)
- Find median of BST in O(n) time and O(1) space
- Inorder predecessor and successor for a given key in BST | Iterative Approach
- Check if a Binary Tree is BST : Simple and Efficient Approach
- Inorder predecessor and successor for a given key in BST
- Overview of Data Structures | Set 2 (Binary Tree, BST, Heap and Hash)
- A program to check if a binary tree is BST or not
- Find the largest BST subtree in a given Binary Tree | Set 1
- Sorted order printing of a given array that represents a BST
- Print BST keys in the given range
- Sorted Linked List to Balanced BST
- Sorted Array to Balanced BST
- K'th smallest element in BST using O(1) Extra Space
- Check if each internal node of a BST has exactly one child
- Construct BST from given preorder traversal | Set 2