Java Program to Find the Minimum value of Binary Search Tree

This is a Java Program to find minimum value of a Binary Search Tree. A binary search tree (BST), sometimes also called an ordered or sorted binary tree, is a node-based binary tree data structure which has the following properties:
i) The left subtree of a node contains only nodes with keys less than the node’s key.
ii) The right subtree of a node contains only nodes with keys greater than the node’s key.
iii) The left and right subtree must each also be a binary search tree.
iv) There must be no duplicate nodes.

Here is the source code of the Java program to minimum value of a Binary Search Tree. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  *  Java Program to Find the Minimum value of Binary Search Tree
  3.  */
  4.  
  5.  import java.util.Scanner;
  6.  
  7.  /* Class BSTNode */
  8.  class BSTNode    
  9.  {
  10.      BSTNode left, right;
  11.      int data;
  12.  
  13.      /* Constructor */
  14.      public BSTNode()
  15.      {
  16.          left = null;
  17.          right = null;
  18.          data = 0;
  19.      }
  20.      /* Constructor */
  21.      public BSTNode(int n)
  22.      {
  23.          left = null;
  24.          right = null;
  25.          data = n;
  26.      }         
  27.  }
  28.  
  29.  /* Class BST */
  30.  class BST
  31.  {
  32.      private BSTNode root;
  33.  
  34.      /* Constructor */
  35.      public BST()
  36.      {
  37.          root = null;
  38.      }
  39.      /* Functions to insert data */
  40.      public void insert(int data)
  41.      {
  42.          root = insert(root, data);
  43.      }
  44.      /* Function to insert data recursively */
  45.      private BSTNode insert(BSTNode node, int data)
  46.      {
  47.          if (node == null)
  48.              node = new BSTNode(data);
  49.          else
  50.          {
  51.              if (data <= node.data)
  52.                  node.left = insert(node.left, data);
  53.              else
  54.                  node.right = insert(node.right, data);
  55.          }
  56.          return node;
  57.      }
  58.      /* Function to return least value */
  59.      public int minValue()
  60.      {
  61.          return minValue(root);          
  62.      }
  63.      /* Function to return least value recursively */
  64.      private int minValue(BSTNode r)
  65.      {
  66.          if (r.left == null)
  67.              return r.data;
  68.          return minValue(r.left);        
  69.      }
  70.  
  71.      public void inorder()
  72.      {
  73.          inorder(root);
  74.      }
  75.      private void inorder(BSTNode r)
  76.      {
  77.          if (r != null)
  78.          {
  79.              inorder(r.left);
  80.              System.out.print(r.data +" ");
  81.              inorder(r.right);
  82.          }
  83.      }
  84.      /* Function for preorder traversal */
  85.      public void preorder()
  86.      {
  87.          preorder(root);
  88.      }
  89.      private void preorder(BSTNode r)
  90.      {
  91.          if (r != null)
  92.          {
  93.              System.out.print(r.data +" ");
  94.              preorder(r.left);             
  95.              preorder(r.right);
  96.          }
  97.      }
  98.      /* Function for postorder traversal */
  99.      public void postorder()
  100.      {
  101.          postorder(root);
  102.      }
  103.      private void postorder(BSTNode r)
  104.      {
  105.          if (r != null)
  106.          {
  107.              postorder(r.left);             
  108.              postorder(r.right);
  109.              System.out.print(r.data +" ");
  110.          }
  111.      }     
  112.  }
  113.  
  114.  /* Class MinValueBST */
  115.  public class MinValueBST
  116.  {
  117.      public static void main(String[] args)
  118.      {                 
  119.          Scanner scan = new Scanner(System.in);
  120.          /* Creating object of BST */
  121.          BST bst = new BST(); 
  122.          System.out.println("Minimum Value of Binary Search Tree Test\n");          
  123.          char ch;
  124.          /*  Accept input  */
  125.          do    
  126.          {
  127.              System.out.println("Enter integer element to insert");
  128.              bst.insert( scan.nextInt() );                     
  129.  
  130.              /*  Display tree  */ 
  131.              System.out.print("\nPost order : ");
  132.              bst.postorder();
  133.              System.out.print("\nPre order : "); 
  134.              bst.preorder();
  135.              System.out.print("\nIn order : ");
  136.              bst.inorder(); 
  137.  
  138.              System.out.println("\nDo you want to continue (Type y or n) \n");
  139.              ch = scan.next().charAt(0);                        
  140.          } while (ch == 'Y'|| ch == 'y'); 
  141.  
  142.          System.out.println("\nMnimum value of the Binary Search Tree is : "+ bst.minValue());              
  143.      }
  144.  }

Minimum Value of Binary Search Tree Test
 
Enter integer element to insert
56
 
Post order : 56
Pre order : 56
In order : 56
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
23
 
Post order : 23 56
Pre order : 56 23
In order : 23 56
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
80
 
Post order : 23 80 56
Pre order : 56 23 80
In order : 23 56 80
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
12
 
Post order : 12 23 80 56
Pre order : 56 23 12 80
In order : 12 23 56 80
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
234
 
Post order : 12 23 234 80 56
Pre order : 56 23 12 80 234
In order : 12 23 56 80 234
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
546
 
Post order : 12 23 546 234 80 56
Pre order : 56 23 12 80 234 546
In order : 12 23 56 80 234 546
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
6
 
Post order : 6 12 23 546 234 80 56
Pre order : 56 23 12 6 80 234 546
In order : 6 12 23 56 80 234 546
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
32
 
Post order : 6 12 32 23 546 234 80 56
Pre order : 56 23 12 6 32 80 234 546
In order : 6 12 23 32 56 80 234 546
Do you want to continue (Type y or n)
 
n
 
Mnimum value of the Binary Search Tree is : 6

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.