Site icon

Find or search node in a binary search tree (Java/ recursive /example)

Fig 1: Find element in BST

Example – Search node having value = 45 in binary search tree using java

Fig 2: Search element in BST
  1. Traverse binary search tree using DFS algorithm
  2. Compare input value (45) with every node of BST.
  3. At every node, we will make decision about the traversal of BST
    • Input number is equal node data (We found the element).
    • Input number is less than node data
      • Find the element in left subtree.
    • Input number is greater than node data.
      • Find the element in right subtree.

The condition to find element in binary search tree is as follows.

  //Condition 1. we found the element
  if(node.data == value) {
   return node;
  } 
  //Condition 2. 
  //Value is less than node value. so go left sub tree
  else if(value < node.data)
     return findNodeInBST(node.left,value);
  //Condition 3. 
  //Value is greater than node value. so go right sub tree
  else 
     return findNodeInBST(node.right,value);

Algorithm: find element in a binary search tree using recursive method

Program: find element or node in a binary search tree (java / recursive)

1.) FindNodeInBST Class:

package org.learn.Question;

public class FindNodeInBST {
	public static Node findNodeInBST(Node node, int value) {
		if(null == node) {
			return null;
		}
		//Condition 1. we found the value
		if(node.data == value) {
			return node;
		} 
		//Condition 2. 
		//Value is less than node value. so go left sub tree
		else if(value < node.data)
			return findNodeInBST(node.left,value);
		//Condition 3. 
		//Value is greater than node value. so go right sub tree
		else 
			return findNodeInBST(node.right,value);
	}
}

2.) Node Class:

package org.learn.Question;

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.Question.FindNodeInBST;
import org.learn.Question.Node;

public class App {
	public static void main(String[] args) {
		// root level 0
		Node A = Node.createNode(50);
		// Level 1
		Node B = Node.createNode(40);
		Node C = Node.createNode(60);
		// Level 2
		Node D = Node.createNode(30);
		Node E = Node.createNode(45);
		Node F = Node.createNode(55);
		Node G = Node.createNode(70);

		// 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;
				
		Node node = FindNodeInBST.findNodeInBST(A, 180);
		if (null == node) {
			System.out.printf("Node=%d does not exists in BST\n",180);
		} else {
			System.out.printf("Found node=%d in BST\n",180);
		}
		
		node = FindNodeInBST.findNodeInBST(A, 45);
		if (null == node) {
			System.out.printf("Node=%d does not exists in BST\n",45);
		} else {
			System.out.printf("Found node=%d in BST\n",45);
		}
	}
}

Output – find or search node in a BST (Java /recursive algorithm)

Node=180 does not exists in BST
Found node=45 in BST

Download code – find or search a Node in BST Java recursive algorithm

Exit mobile version