Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Ceil in BST.java #2518

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions Java/Java/Ceil in BST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
/*
Ceil in BST
Difficulty Level : Medium
Given a BST and a number X, find Ceil of X.
Note: Ceil(X) is a number that is either equal to X or is immediately greater than X.
If Ceil could not be found, return -1.
Example 1:
Input:
5
/ \
1 7
\
2
\
3
X = 3
Output: 3
Explanation: We find 3 in BST, so ceil
of 3 is 3.

Example 2:
Input:
10
/ \
5 11
/ \
4 7
\
8
X = 6
Output: 7
Explanation: We find 7 in BST, so ceil
of 6 is 7.
*/


import java.util.LinkedList;
import java.util.Queue;
import java.io.*;
import java.util.*;

class Node {
int data;
Node left;
Node right;
Node(int data) {
this.data = data;
left = null;
right = null;
}
}

class GfG {

static Node buildTree(String str) {

if (str.length() == 0 || str.charAt(0) == 'N') {
return null;
}

String ip[] = str.split(" ");
// Create the root of the tree
Node root = new Node(Integer.parseInt(ip[0]));
// Push the root to the queue

Queue<Node> queue = new LinkedList<>();

queue.add(root);
// Starting from the second element

int i = 1;
while (queue.size() > 0 && i < ip.length) {

// Get and remove the front of the queue
Node currNode = queue.peek();
queue.remove();

// Get the current node's value from the string
String currVal = ip[i];

// If the left child is not null
if (!currVal.equals("N")) {

// Create the left child for the current node
currNode.left = new Node(Integer.parseInt(currVal));
// Push it to the queue
queue.add(currNode.left);
}

// For the right child
i++;
if (i >= ip.length) break;

currVal = ip[i];

// If the right child is not null
if (!currVal.equals("N")) {

// Create the right child for the current node
currNode.right = new Node(Integer.parseInt(currVal));

// Push it to the queue
queue.add(currNode.right);
}
i++;
}

return root;
}
static void printInorder(Node root) {
if (root == null) return;

printInorder(root.left);
System.out.print(root.data + " ");

printInorder(root.right);
}

public static void main(String[] args) throws IOException {
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));

int t = Integer.parseInt(br.readLine());

while (t > 0) {
String s = br.readLine();
int n = Integer.parseInt(br.readLine());
Node root = buildTree(s);
Tree g = new Tree();
System.out.println(g.findCeil(root, n));
t--;
}
}
}

// } Driver Code Ends


// User function Template for Java

class Tree {
// Function to return the ceil of given number in BST.
int findCeil(Node root, int key) {
if (root == null) return -1;
// Code here
int res = -1;
while(root!=null)
{
if(root.data==key)
return key;
if(root.data<key)
root = root.right;
else
{
res = root.data;
root = root.left;
}
}
return res;
}
}