
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to Check if a Given Binary Tree is an AVL Tree or Not
AVL tree is a self-balancing Binary Search Tree where the difference between heights of left and right subtrees cannot be more than one for all nodes.
This is a C++ program to check if a given Binary Tree is an AVL Tree or not.
Algorithm
Begin function AVL() returns true if the given tree is AVL otherwise false. if(root == NULL) return 1 leftheight = height(root->left) rightheight = height(root->right) if(abs(leftheight-rightheight) <= 1 && AVL(root->left) && AVL(root->right)) return 1 return 0 End
Example
#include <bits/stdc++.h> using namespace std; class nod { //node declaration public: int data; nod* l; nod* r; }; nod* newNod(int d) { //creation of new node nod* Nod = new nod(); Nod->data = d; Nod->l = NULL; Nod->r = NULL; return(Nod); } int max(int x, int y) { //return maximum between two values return (x >= y)? x: y; } int height(nod* node) { //get the height means the number of nodes along the longest path from the root node down to the farthest leaf node of the tree. if(node == NULL) return 0; return 1 + max(height(node->l), height(node->r)); } bool AVL(nod *root) { int lh; int rh; if(root == NULL) return 1; lh = height(root->l); // left height rh = height(root->r); // right height if(abs(lh-rh) <= 1 && AVL(root->l) && AVL(root->r)) return 1; return 0; } int main() { //take the nodes of the tree as input nod *root = newNod(7); root->l = newNod(6); root->r = newNod(12); root->l->l = newNod(4); root->l->r = newNod(5); root->r->r = newNod(13); if(AVL(root)) cout << "The Tree is AVL Tree"<<endl; else cout << "The Tree is not AVL Tree "<<endl; nod *root1 = newNod(7); root1->l = newNod(6); root1->r = newNod(12); root1->l->l = newNod(4); root1->l->r = newNod(5); root1->r->r = newNod(13); root1->r->r->r = newNod(26); if(AVL(root1)) cout << "The Tree is AVL Tree"<<endl; else cout << "The Tree is not AVL Tree "<<endl; return 0; }
Output
The Tree is AVL Tree The Tree is not AVL Tree
- Related Articles
- C++ Program to Check Whether a given Binary Tree is a Full Binary Tree or not
- A program to check if a binary tree is BST or not in C ?
- C++ Program to Check Whether a Given Tree is Binary Search Tree
- Check if a binary tree is sorted levelwise or not in C++
- Check if a given graph is tree or not
- Program to check whether given tree is symmetric tree or not in Python
- Check if a binary tree is sorted level-wise or not in C++
- C++ Program to Check Whether an Input Binary Tree is the Sub Tree of the Binary Tree
- Check if a given tree graph is linear or not in C++
- Check if a given Binary Tree is SumTree in C++
- Check if a given Binary Tree is Heap in C++
- C++ Program to Check if an UnDirected Graph is a Tree or Not Using DFS
- C++ Program to Check if a Binary Tree is a BST
- C++ Program to Implement AVL Tree
- Program to check whether a binary tree is complete or not in Python

Advertisements