Site icon

Find floor value of input data in Binary search tree – DFS / Example

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

Fig 1: Floor 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

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

Fig 2: Floor(90) = 75

Example 2: find floor value of input 120 in a given BST (Fig 3).

Fig 3:Floor(120) = 120

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

Fig 4: Floor(126) = 125

We can analyse the Fig 4 like example 1 & example 2.
Let us summarize to find floor values of sample input data in a given BST as follows:

S. No.Input data Floor value
190 75
2120 120
3126 125
4170 160

Algorithm: find floor value in binary search tree

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

Program: find floor value of input data in Binary search tree.

1.) FloorInBST Class:

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

public class FloorInBST {	
	public static Node floor(Node root, int data) {
		if (root == null) 
			return root;
		if (root.data == data) 
			return root;
		if (root.data >  data) 
			return floor(root.left, data);
		Node right = floor(root.right, data); 
		if (right == null) 
			return root;
		else return right; 
	}	
	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 representing the nodes of a 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.FloorInBST;

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:");   
       FloorInBST.inorder(A);
       int data = 90;       
       Node floor = FloorInBST.floor(A, data);     
       if(null != floor) {
    	   System.out.printf("\nFloor value of %d is %d",data,floor.data);
       }       
       data = 120;       
       floor = FloorInBST.floor(A, data);     
       if(null != floor) {
    	   System.out.printf("\nFloor value of %d is %d",data,floor.data);
       }       
       data = 170;       
       floor = FloorInBST.floor(A, data);     
       if(null != floor) {
    	   System.out.printf("\nFloor value of %d is %d",data,floor.data);
       }             
    }
}

Output: find floor value of input data in Binary search tree:

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

Download code – find floor value in BST DFS recursive

 

Exit mobile version