Floor and Ceil from a BST
- Difficulty Level : Medium
- Last Updated : 01 Jul, 2021
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 the greatest data lesser than or equal to the key value.
Ceil Value Node: Node with the smallest data larger than or equal to the 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 the floor/ceil value of a key in a binary search tree or sorted array. For example, consider designing a 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 the above code to find the floor value of the input key in a binary search tree.
2. Write a 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 write.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.