Java Program to Check if a Given Binary Tree is an AVL Tree or Not

This is a Java Program to implement a binary tree and check whether it is AVL Tree or not. An AVL tree is a self-balancing binary search tree. It was the first such data structure to be invented. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Lookup, insertion, and deletion all take O(log n) time in both the average and worst cases, where n is the number of nodes in the tree prior to the operation. Insertions and deletions may require the tree to be rebalanced by one or more tree rotations.
A tree is AVL if and only if it is Binary Search Tree and is Balanced.

Here is the source code of the Java Program to Check if a Given Binary Tree is an AVL Tree or Not. 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 check whether a tree is AVL tree or not
  2. class BSTAVLTreeNode
  3. {
  4.     int            value;
  5.     BSTAVLTreeNode Left;
  6.     BSTAVLTreeNode Right;
  7.  
  8.     BSTAVLTreeNode(int k)
  9.     {
  10.         value = k;
  11.     }
  12. }
  13.  
  14. class BST_AVL
  15. {
  16.     public static boolean isBST(BSTAVLTreeNode node)
  17.     {
  18.         return (isBSTUtil(node, 0, 100));
  19.     }
  20.  
  21.     public static boolean isBSTUtil(BSTAVLTreeNode node, int min, int max)
  22.     {
  23.  
  24.         if (node == null)
  25.             return true;
  26.  
  27.         if (node.value < min || node.value > max)
  28.             return false;
  29.  
  30.         return (isBSTUtil(node.Left, min, node.value - 1) && isBSTUtil(
  31.                 node.Right, node.value + 1, max));
  32.     }
  33.  
  34.     public static boolean isBalanced(BSTAVLTreeNode root)
  35.     {
  36.         int lh; /* for height of left subtree */
  37.         int rh; /* for height of right subtree */
  38.  
  39.         if (root == null)
  40.             return true;
  41.  
  42.         lh = height(root.Left);
  43.         rh = height(root.Right);
  44.  
  45.         if (Math.abs(lh - rh) <= 1 && isBalanced(root.Left)
  46.                 && isBalanced(root.Right))
  47.             return true;
  48.  
  49.         return false;
  50.     }
  51.  
  52.     public static int max(int a, int b)
  53.     {
  54.         return (a >= b) ? a : b;
  55.     }
  56.  
  57.     public static int height(BSTAVLTreeNode node)
  58.     {
  59.         if (node == null)
  60.             return 0;
  61.  
  62.         return 1 + max(height(node.Left), height(node.Right));
  63.     }
  64.  
  65.     public static void main(String args[])
  66.     {
  67.         BSTAVLTreeNode t1 = new BSTAVLTreeNode(1);
  68.         t1.Left = new BSTAVLTreeNode(2);
  69.         t1.Right = new BSTAVLTreeNode(3);
  70.         t1.Right.Left = new BSTAVLTreeNode(4);
  71.         t1.Right.Right = new BSTAVLTreeNode(5);
  72.  
  73.         BSTAVLTreeNode t2 = new BSTAVLTreeNode(15);
  74.         t2.Left = new BSTAVLTreeNode(5);
  75.         t2.Right = new BSTAVLTreeNode(20);
  76.         t2.Right.Left = new BSTAVLTreeNode(18);
  77.         t2.Right.Right = new BSTAVLTreeNode(23);
  78.         t2.Left.Left = new BSTAVLTreeNode(4);
  79.         t2.Left.Right = new BSTAVLTreeNode(6);
  80.  
  81.         if (isBST(t1) && isBalanced(t1))
  82.             System.out.println("Tree t1 is AVL tree");
  83.         else
  84.             System.out.println("Tree t1 is not AVL tree");
  85.  
  86.         if (isBST(t2) && isBalanced(t2))
  87.             System.out.println("Tree t1 is AVL tree");
  88.         else
  89.             System.out.println("Tree t1 is not AVL tree");
  90.     }
  91. }

Output:

$ javac BST_AVL.java
$ java BST_AVL
 
Tree t1 is not AVL tree
Tree t1 is AVL tree

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

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

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.