Java Program to Implement Binary Search Tree using Linked Lists

This is a Java Program to implement Binary Search Tree using Linked Lists. 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 BST is implemented using Linked List.

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

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

Linked List Binary Search Tree Test
 
Enter integer element to insert
45
 
Post order : 45
Pre order : 45
In order : 45
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
12
 
Post order : 12 45
Pre order : 45 12
In order : 12 45
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
67
 
Post order : 12 67 45
Pre order : 45 12 67
In order : 12 45 67
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
23
 
Post order : 23 12 67 45
Pre order : 45 12 23 67
In order : 12 23 45 67
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
96
 
Post order : 23 12 96 67 45
Pre order : 45 12 23 67 96
In order : 12 23 45 67 96
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
32
 
Post order : 32 23 12 96 67 45
Pre order : 45 12 23 32 67 96
In order : 12 23 32 45 67 96
Do you want to continue (Type y or n)
 
y
Enter integer element to insert
24
 
Post order : 24 32 23 12 96 67 45
Pre order : 45 12 23 32 24 67 96
In order : 12 23 24 32 45 67 96
Do you want to continue (Type y or n)
 
n

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.