Skip to content

Commit e118abd

Browse files
Add ceil value in a Binary Search Tree (TheAlgorithms#2399)
Co-authored-by: Amit Kumar <[email protected]>
1 parent 776c3c6 commit e118abd

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package DataStructures.Trees;
2+
3+
import DataStructures.Trees.BinaryTree.Node;
4+
5+
/**
6+
* Problem Statement
7+
* Ceil value for any number x in a collection is a number y which is either equal to x or the least greater number than x.
8+
*
9+
* Problem: Given a binary search tree containing positive integer values.
10+
* Find ceil value for a given key in O(lg(n)) time. In case if it is not present return -1.
11+
*
12+
* Ex.1. [30,20,40,10,25,35,50] represents level order traversal of a binary search tree. Find ceil for 10.
13+
* Answer: 20
14+
*
15+
* Ex.2. [30,20,40,10,25,35,50] represents level order traversal of a binary search tree. Find ceil for 22
16+
* Answer: 25
17+
*
18+
* Ex.2. [30,20,40,10,25,35,50] represents level order traversal of a binary search tree. Find ceil for 52
19+
* Answer: -1
20+
*/
21+
22+
/**
23+
*
24+
* Solution 1:
25+
* Brute Force Solution:
26+
* Do an inorder traversal and save result into an array. Iterate over the array to get an element equal to or greater
27+
* than current key.
28+
* Time Complexity: O(n)
29+
* Space Complexity: O(n) for auxillary array to save inorder representation of tree.
30+
* <p>
31+
* <p>
32+
* Solution 2:
33+
* Brute Force Solution:
34+
* Do an inorder traversal and save result into an array.Since array is sorted do a binary search over the array to get an
35+
* element equal to or greater than current key.
36+
* Time Complexity: O(n) for traversal of tree and O(lg(n)) for binary search in array. Total = O(n)
37+
* Space Complexity: O(n) for auxillary array to save inorder representation of tree.
38+
* <p>
39+
* <p>
40+
* Solution 3: Optimal
41+
* We can do a DFS search on given tree in following fashion.
42+
* i) if root is null then return null because then ceil doesn't exist
43+
* ii) If key is lesser than root value than ceil will be in right subtree so call recursively on right subtree
44+
* iii) if key is greater than current root, then either
45+
* a) the root is ceil
46+
* b) ceil is in left subtree: call for left subtree. If left subtree returns a non null value then that will be ceil
47+
* otherwise the root is ceil
48+
*/
49+
public class CeilInBinarySearchTree {
50+
51+
public static Node getCeil(Node root, int key) {
52+
if (root == null) {
53+
return null;
54+
}
55+
56+
// if root value is same as key than root is the ceiling
57+
if (root.data == key) {
58+
return root;
59+
}
60+
61+
// if root value is lesser than key then ceil must be in right subtree
62+
if (root.data < key) {
63+
return getCeil(root.right, key);
64+
}
65+
66+
// if root value is greater than key then ceil can be in left subtree or if
67+
// it is not in left subtree then current node will be ceil
68+
Node result = getCeil(root.left, key);
69+
70+
// if result is null it means that there is no ceil in children subtrees
71+
// and the root is the ceil otherwise the returned node is the ceil.
72+
return result == null ? root : result;
73+
}
74+
}
75+

0 commit comments

Comments
 (0)