Skip to content

Commit 691009a

Browse files
Create CeilOfBST.java
1 parent 14cf7e6 commit 691009a

File tree

1 file changed

+130
-0
lines changed

1 file changed

+130
-0
lines changed

Binary Search Tree/CeilOfBST.java

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//{ Driver Code Starts
2+
// Initial Template for Java
3+
4+
import java.util.LinkedList;
5+
import java.util.Queue;
6+
import java.io.*;
7+
import java.util.*;
8+
9+
class Node {
10+
int data;
11+
Node left;
12+
Node right;
13+
Node(int data) {
14+
this.data = data;
15+
left = null;
16+
right = null;
17+
}
18+
}
19+
20+
class GfG {
21+
22+
static Node buildTree(String str) {
23+
24+
if (str.length() == 0 || str.charAt(0) == 'N') {
25+
return null;
26+
}
27+
28+
String ip[] = str.split(" ");
29+
// Create the root of the tree
30+
Node root = new Node(Integer.parseInt(ip[0]));
31+
// Push the root to the queue
32+
33+
Queue<Node> queue = new LinkedList<>();
34+
35+
queue.add(root);
36+
// Starting from the second element
37+
38+
int i = 1;
39+
while (queue.size() > 0 && i < ip.length) {
40+
41+
// Get and remove the front of the queue
42+
Node currNode = queue.peek();
43+
queue.remove();
44+
45+
// Get the current node's value from the string
46+
String currVal = ip[i];
47+
48+
// If the left child is not null
49+
if (!currVal.equals("N")) {
50+
51+
// Create the left child for the current node
52+
currNode.left = new Node(Integer.parseInt(currVal));
53+
// Push it to the queue
54+
queue.add(currNode.left);
55+
}
56+
57+
// For the right child
58+
i++;
59+
if (i >= ip.length) break;
60+
61+
currVal = ip[i];
62+
63+
// If the right child is not null
64+
if (!currVal.equals("N")) {
65+
66+
// Create the right child for the current node
67+
currNode.right = new Node(Integer.parseInt(currVal));
68+
69+
// Push it to the queue
70+
queue.add(currNode.right);
71+
}
72+
i++;
73+
}
74+
75+
return root;
76+
}
77+
static void printInorder(Node root) {
78+
if (root == null) return;
79+
80+
printInorder(root.left);
81+
System.out.print(root.data + " ");
82+
83+
printInorder(root.right);
84+
}
85+
86+
public static void main(String[] args) throws IOException {
87+
BufferedReader br =
88+
new BufferedReader(new InputStreamReader(System.in));
89+
90+
int t = Integer.parseInt(br.readLine());
91+
92+
while (t > 0) {
93+
String s = br.readLine();
94+
int n = Integer.parseInt(br.readLine());
95+
Node root = buildTree(s);
96+
Tree g = new Tree();
97+
System.out.println(g.findCeil(root, n));
98+
t--;
99+
100+
System.out.println("~");
101+
}
102+
}
103+
}
104+
105+
// } Driver Code Ends
106+
107+
108+
// User function Template for Java
109+
110+
class CeilOfBST {
111+
// Function to return the ceil of given number in BST.
112+
int findCeil(Node root, int key) {
113+
if (root == null) return -1;
114+
int ceil = -1;
115+
while(root!=null){
116+
if(root.data == key){
117+
return key;
118+
}
119+
if(root.data > key){
120+
ceil = root.data;
121+
root = root.left;
122+
}
123+
else{
124+
root = root.right;
125+
}
126+
}
127+
128+
return ceil;
129+
}
130+
}

0 commit comments

Comments
 (0)