Java Program to Create a Balanced Binary Tree of the Incoming Data

«
»
This is a Java Program to implement Self Balancing Binary Tree. A self-balancing (or height-balanced) binary tree is any node-based binary tree that automatically keeps its height (maximal number of levels below the root) small in the face of arbitrary item insertions and deletions.
These structures provide efficient implementations for mutable ordered lists, and can be used for other abstract data structures such as associative arrays, priority queues and sets. The implementation of self balancing binary tree is similar to that of a AVL Tree data structure.

Here is the source code of the Java Program to Create a Balanced Binary Tree of the Incoming Data. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to make a self balancing binary tree for the input data
  2. import java.util.Scanner;
  3.  
  4. class SBBSTNodes
  5. {
  6.     SBBSTNodes left, right;
  7.     int        data;
  8.     int        height;
  9.  
  10.     public SBBSTNodes()
  11.     {
  12.         left = null;
  13.         right = null;
  14.         data = 0;
  15.         height = 0;
  16.     }
  17.  
  18.     public SBBSTNodes(int n)
  19.     {
  20.  
  21.         left = null;
  22.         right = null;
  23.         data = n;
  24.         height = 0;
  25.     }
  26. }
  27.  
  28. class SelfBalancingBinarySearchTrees
  29. {
  30.     private SBBSTNodes root;
  31.  
  32.     public SelfBalancingBinarySearchTrees()
  33.     {
  34.         root = null;
  35.     }
  36.  
  37.     public boolean isEmpty()
  38.     {
  39.         return root == null;
  40.     }
  41.  
  42.     public void clear()
  43.     {
  44.         root = null;
  45.     }
  46.  
  47.     public void insert(int data)
  48.     {
  49.         root = insert(data, root);
  50.     }
  51.  
  52.     private int height(SBBSTNodes t)
  53.     {
  54.  
  55.         return t == null ? -1 : t.height;
  56.     }
  57.  
  58.     private int max(int lhs, int rhs)
  59.     {
  60.         return lhs > rhs ? lhs : rhs;
  61.     }
  62.  
  63.     private SBBSTNodes insert(int x, SBBSTNodes t)
  64.     {
  65.         if (t == null)
  66.             t = new SBBSTNodes(x);
  67.         else if (x < t.data)
  68.         {
  69.             t.left = insert(x, t.left);
  70.             if (height(t.left) - height(t.right) == 2)
  71.                 if (x < t.left.data)
  72.                     t = rotateWithLeftChild(t);
  73.                 else
  74.                     t = doubleWithLeftChild(t);
  75.         } else if (x > t.data)
  76.         {
  77.             t.right = insert(x, t.right);
  78.             if (height(t.right) - height(t.left) == 2)
  79.                 if (x > t.right.data)
  80.                     t = rotateWithRightChild(t);
  81.                 else
  82.                     t = doubleWithRightChild(t);
  83.         } else
  84.             ;
  85.         t.height = max(height(t.left), height(t.right)) + 1;
  86.         return t;
  87.     }
  88.  
  89.     private SBBSTNodes rotateWithLeftChild(SBBSTNodes k2)
  90.     {
  91.         SBBSTNodes k1 = k2.left;
  92.         k2.left = k1.right;
  93.         k1.right = k2;
  94.         k2.height = max(height(k2.left), height(k2.right)) + 1;
  95.         k1.height = max(height(k1.left), k2.height) + 1;
  96.         return k1;
  97.     }
  98.  
  99.     private SBBSTNodes rotateWithRightChild(SBBSTNodes k1)
  100.     {
  101.         SBBSTNodes k2 = k1.right;
  102.         k1.right = k2.left;
  103.         k2.left = k1;
  104.         k1.height = max(height(k1.left), height(k1.right)) + 1;
  105.         k2.height = max(height(k2.right), k1.height) + 1;
  106.         return k2;
  107.     }
  108.  
  109.     private SBBSTNodes doubleWithLeftChild(SBBSTNodes k3)
  110.     {
  111.         k3.left = rotateWithRightChild(k3.left);
  112.         return rotateWithLeftChild(k3);
  113.     }
  114.  
  115.     private SBBSTNodes doubleWithRightChild(SBBSTNodes k1)
  116.     {
  117.         k1.right = rotateWithLeftChild(k1.right);
  118.         return rotateWithRightChild(k1);
  119.     }
  120.  
  121.     public int countNodes()
  122.     {
  123.         return countNodes(root);
  124.     }
  125.  
  126.     private int countNodes(SBBSTNodes r)
  127.     {
  128.         if (r == null)
  129.             return 0;
  130.         else
  131.         {
  132.             int l = 1;
  133.             l += countNodes(r.left);
  134.             l += countNodes(r.right);
  135.             return l;
  136.         }
  137.     }
  138.  
  139.     public boolean search(int val)
  140.     {
  141.         return search(root, val);
  142.     }
  143.  
  144.     private boolean search(SBBSTNodes r, int val)
  145.     {
  146.         boolean found = false;
  147.         while ((r != null) && !found)
  148.         {
  149.             int rval = r.data;
  150.             if (val < rval)
  151.                 r = r.left;
  152.             else if (val > rval)
  153.                 r = r.right;
  154.             else
  155.             {
  156.                 found = true;
  157.                 break;
  158.             }
  159.             found = search(r, val);
  160.         }
  161.         return found;
  162.     }
  163.  
  164.     public void inorder()
  165.     {
  166.         inorder(root);
  167.     }
  168.  
  169.     private void inorder(SBBSTNodes r)
  170.     {
  171.         if (r != null)
  172.         {
  173.             inorder(r.left);
  174.             System.out.print(r.data + " ");
  175.             inorder(r.right);
  176.         }
  177.     }
  178.  
  179.     public void preorder()
  180.     {
  181.  
  182.         preorder(root);
  183.     }
  184.  
  185.     private void preorder(SBBSTNodes r)
  186.     {
  187.         if (r != null)
  188.         {
  189.             System.out.print(r.data + " ");
  190.             preorder(r.left);
  191.             preorder(r.right);
  192.         }
  193.     }
  194.  
  195.     public void postorder()
  196.     {
  197.         postorder(root);
  198.     }
  199.  
  200.     private void postorder(SBBSTNodes r)
  201.     {
  202.         if (r != null)
  203.         {
  204.             postorder(r.left);
  205.             postorder(r.right);
  206.             System.out.print(r.data + " ");
  207.         }
  208.     }
  209. }
  210.  
  211. public class Balanced_B_Tree
  212. {
  213.     public static void main(String[] args)
  214.     {
  215.         Scanner scan = new Scanner(System.in);
  216.  
  217.         SelfBalancingBinarySearchTrees sbbst = new SelfBalancingBinarySearchTrees();
  218.         System.out.println("Self Balancing Tree\n");
  219.  
  220.         int N = 10;
  221.         for (int i = 0; i < N; i++)
  222.             sbbst.insert(scan.nextInt());
  223.  
  224.         System.out.println("\nPre-order  :");
  225.         sbbst.preorder();
  226.         System.out.println("\nIn-order   :");
  227.         sbbst.inorder();
  228.         System.out.println("\nPost-order :");
  229.         sbbst.postorder();
  230.         scan.close();
  231.     }
  232. }

Output:

advertisement
$ javac Balanced_B_Tree.java
$ java Balanced_B_Tree
 
Self Balancing Tree
 
45
46
48
98
23
34
65
59
21
10
 
Pre-order  :
46 34 21 10 23 45 65 48 59 98 
In-order   :
10 21 23 34 45 46 48 59 65 98 
Post-order :
10 23 21 45 34 59 48 98 65 46

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

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 & technical discussions at Telegram SanfoundryClasses.