Site icon

Find ceil value of input data in binary search tree in java (DFS/Example)

Let us find the ceil value in a binary search tree (BST), shown in Fig 1.

Fig 1: Ceil value in BST

The inorder traversal of binary search tree as shown in Fig 1  is
25 50 75 100 120 125 140 150 160 175 190

Examples – find ceil value of input data in a binary search tree using java

Example 1: find ceil value of input 90 in a given BST (Fig 2).

Fig 2: Ceil (90) = 100

Example 2: calculate ceil value of input 120 in BST (Fig 3).

Fig 3: Ceil (120) = 120

Example 3: find ceil value of input 126 in a BST (Fig 3).

Fig 4: Ceil (126) = 140

We can analyse the Fig 4 like example 1 & example 2.

Let us summarize to find ceil values of sample input data in a given BST as follows:

S. No.Input data Ceil value
190  100
2120 120
3126 140
4170 175

Algorithm: find ceil value in a BST (recursive/DFS) using java

Time complexity of algorithm: O(log(n))

Calculate ceil value of input data in a binary search tree (DFS) using java

1.) CeilInBST Class:

package org.learn.Question;
import org.learn.PrepareTree.Node;

public class CeilInBST {
	
	public static Node ceilInBST(Node root, int data) {
		if (root == null) 
			return root;
		if (root.data == data) 
			return root;
		if (root.data >  data) {
			Node left = ceilInBST(root.left, data); 
			if (left == null) 
				return root;
			else 
				return left; 
		}
		return ceilInBST(root.right, data);		
	}
	
	public static void inorder(Node root) {		
		if(root == null)
			return;
		
		inorder(root.left);
		System.out.printf("%d ",root.data);
		inorder(root.right);		
	}	
}

2.) Node Class:

Node class is representing the nodes of binary tree. Node class has following attributes

package org.learn.PrepareTree;

public class Node {
	public int data;
	public Node left;
	public Node right;

	public Node(int num) {
		this.data = num;
		this.left = null;
		this.right = null;
	}
	public Node() {
		this.left = null;
		this.right = null;
	}
	public static Node createNode(int number) {
		return new Node(number);
	}
}

3.) App Class :

package org.learn.Client;

import org.learn.PrepareTree.Node;
import org.learn.Question.CeilInBST;

public class App 
{
    public static void main( String[] args )
    {  
       //root level 0
       Node A = Node.createNode(100);
       //Level 1
       Node B = Node.createNode(50);
       Node C = Node.createNode(150);
       //Level 2
       Node D = Node.createNode(25);
       Node E = Node.createNode(75);
       Node F = Node.createNode(125);
       Node G = Node.createNode(175);
       //Level 3
       Node H = Node.createNode(120);
       Node I = Node.createNode(140);
       Node J = Node.createNode(160);
       Node K = Node.createNode(190);
             
       //connect Level 0 and 1
       A.left = B;
       A.right = C;
       //connect level 1 and level 2
       B.left = D;
       B.right = E;
       C.left = F;
       C.right = G;
       //Connect level 2 and level 3
       F.left = H;
       F.right = I;
       G.left = J;
       G.right = K;
       
       System.out.println("Inorder Traversal of BST");   
       CeilInBST.inorder(A);
       int data = 90;       
       Node floor = CeilInBST.ceilInBST(A, data);     
       if(null != floor) {
    	   System.out.printf("\nCeil value of %d is %d",data,floor.data);
       }
       
       data = 120;       
       floor = CeilInBST.ceilInBST(A, data);     
       if(null != floor) {
    	   System.out.printf("\nCeil value of %d is %d",data,floor.data);
       }
       
       data = 126;       
       floor = CeilInBST.ceilInBST(A, data);     
       if(null != floor) {
    	   System.out.printf("\nCeil value of %d is %d",data,floor.data);
       }
       
       data = 170;       
       floor = CeilInBST.ceilInBST(A, data);     
       if(null != floor) {
    	   System.out.printf("\nCeil value of %d is %d",data,floor.data);
       }             
    }
}

Output – ceil value of input key in a binary search tree (DFS)

Inorder Traversal of BST
25 50 75 100 120 125 140 150 160 175 190
Ceil value of 90 is 100
Ceil value of 120 is 120
Ceil value of 126 is 140
Ceil value of 170 is 175

Download  Code – Ceil value in BST (DFS / recursive algorithm)

 

Exit mobile version