Site icon

Find minimum/maximum value in binary search tree (BST) using java (DFS/example)

What is minimum element in BST ?

What is maximum element in BST?

Example 1: find min & max value in a BST (Fig 1).

Fig 1: Min and Max in BST

Example 2: find min & max value in a BST (Fig 2).

Fig 2: Min and Max in Binary search tree

Algorithm to find minimum element in a binary search tree

Program to find minimum element in a BST

public static int min(Node root) {
  if(null == root) {
   System.out.println("Tree is empty");
   return -1;
  }
  //we found the min value
  if(root.left == null) {
   return root.data;
  }
  return min(root.left);
 }

Algorithm to find maximum element in a binary search tree

Program to find maximum value in BST.

public static int max(Node root) {
  if(null == root) {
   System.out.println("Tree is empty");
   return -1;
  }
  //we found the max value
  if(root.right == null) {
   return root.data;
  }
  return max(root.right);
 }
}

Complete program to find minimum & maximum element in a BST

1.) MinAndMaxInBST class:

  

package org.learn.Question;

public class MinAndMaxInBST {
	public static int min(Node root) {
		if(null == root) {
			System.out.println("Tree is empty");
			return -1;
		}
		//we found the min value
		if(root.left == null) {
			return root.data;
		}
		return min(root.left);
	}
	
	public static int max(Node root) {
		if(null == root) {
			System.out.println("Tree is empty");
			return -1;
		}
		//we found the max value
		if(root.right == null) {
			return root.data;
		}
		return max(root.right);
	}
}

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

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(80);
		Node F = Node.createNode(125);
		Node G = Node.createNode(170);

		// Level 3
		Node H = Node.createNode(10);
		Node I = Node.createNode(30);
		Node J = Node.createNode(60);
		Node K = Node.createNode(90);
		Node L = Node.createNode(110);
		Node M = Node.createNode(140);

		// 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 1 and level 2
		D.left  = H;
		D.right = I;
		E.left  = J;
		E.right = K;
		F.left  = L;
		F.right = M;
		
		System.out.println("Min value in BST is : " + MinAndMaxInBST.min(A));
		System.out.println("Max value in BST is : " + MinAndMaxInBST.max(A));
	}
}

Output – min & max value in a BST

Min value in BST is : 10
Max value in BST is : 170

Download Code – Min & Max Element in BST (Recursive)

 

Exit mobile version