m2skills

isBalanced java

Jun 22nd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. // http://code2begin.blogspot.com
  2. // program to check if a given binary tree is a balanced binary tree or not
  3.  
  4. /**
  5.  * Created by MOHIT on 25-05-2018.
  6.  */
  7.  
  8. import java.io.*;
  9. import java.lang.reflect.Array;
  10. import java.util.*;
  11.  
  12. import static java.lang.Integer.max;
  13.  
  14.  
  15. // node class
  16. class node{
  17.     int data;
  18.     node left;
  19.     node right;
  20.  
  21.     // function that returns a pointer to new node
  22.     public node(int element){
  23.         this.data = element;
  24.         this.left = null;
  25.         this.right = null;
  26.        
  27.     }
  28. };
  29.  
  30.  
  31. public class BinaryTree {
  32.    
  33.     // function to find the height of a binary tree
  34.     static int height(node root){
  35.         if (root == null){
  36.             return 0;
  37.         }
  38.         return (1 + max(height(root.left), height(root.right)));
  39.     }
  40.    
  41.     // function to check if the tree is a height balanced tree or not
  42.     static boolean isBalanced(node root){
  43.         if(root == null){
  44.             return true;
  45.         }
  46.         // get the heights of the left and the right subtrees
  47.         int left_height = height(root.left);
  48.         int right_height = height(root.right);
  49.         // if the difference between heights of the left and right subtrees is less than 2
  50.         // and left and right subtrees are also balanced then return true
  51.         return Math.abs(left_height - right_height) <= 1 && isBalanced(root.left) && isBalanced(root.right);
  52.    
  53.     }
  54.     public static void main(String arg[]) {
  55.    
  56.         // Binary Tree #1
  57.         node head = new node(1);
  58.         head.left = new node(2);
  59.         head.right = new node(3);
  60.         head.left.left = new node(4);
  61.         head.left.right = new node(5);
  62.         head.right.right = new node(6);
  63.         head.left.left.right = new node(7);
  64.         head.right.right.left = new node(8);
  65.         head.left.left.right.left = new node(9);
  66.         head.left.left.right.left.left = new node(10);
  67.         head.right.right.left.right = new node(11);
  68.  
  69.         // Binary Tree #2
  70.         node head2 = new node(5);
  71.         head2.left = new node(2);
  72.         head2.right = new node(12);
  73.         head2.left.left = new node(-4);
  74.         head2.left.right = new node(3);
  75.         head2.right.left = new node(9);
  76.         head2.right.right = new node(21);
  77.         head2.right.right.left = new node(19);
  78.         head2.right.right.right = new node(25);
  79.  
  80.         System.out.println("The Binary tree #1 is Balanced : " + isBalanced(head));
  81.         System.out.println("The Binary tree #2 is Balanced : " + isBalanced(head2));
  82.  
  83.     }
  84. }
Add Comment
Please, Sign In to add comment