1
+ /*
2
+ Given a Binary Search Tree ,Check if its a AVL Tree or not
3
+ A BST is said to be a AVL Tree
4
+
5
+ AVL Tree can be defined as height balanced binary search tree in which each node is associated with a balance factor
6
+ which is calculated by subtracting the height of its right sub-tree from that of its left sub-tree.
7
+ Tree is said to be balanced if balance factor of each node is in between -1 to 1, otherwise,
8
+ the tree will be unbalanced and need to be balanced.
9
+
10
+ Balance Factor (k) = height (left(k)) - height (right(k))
11
+ */
12
+
13
+ #include < bits/stdc++.h>
14
+ using namespace std ;
15
+
16
+ // Define Node as structure
17
+ struct Node
18
+ {
19
+ int key;
20
+ Node* left;
21
+ Node* right;
22
+ };
23
+
24
+ /* Function to create a node with 'value' as the data stored in it.
25
+ Both the children of this new Node are initially null. */
26
+ struct Node * newNode (int value)
27
+ {
28
+ Node* n = new Node;
29
+ n->key = value;
30
+ n->left = NULL ;
31
+ n->right = NULL ;
32
+ return n;
33
+ }
34
+
35
+ // Function to insert a node with given value to the root
36
+ struct Node * insert (struct Node * root,int element)
37
+ {
38
+ /* If the root is NULL , create a node with given element and assign it to root
39
+ else if the root itself is the node with given data , return
40
+ else recursively insert it in one of the subtrees accordingly */
41
+ if (root==NULL )
42
+ root = newNode (element);
43
+ else if (root->key < element)
44
+ root->right = insert (root->right ,element);
45
+ else if (root->key > element)
46
+ root->left = insert (root->left ,element);
47
+
48
+ return root;
49
+ }
50
+
51
+ // Function to calculate height of a given Tree recursively
52
+ int Height (struct Node * root)
53
+ {
54
+ /* If root is a NULL node return 0
55
+ else recursively calculate heights of subtrees and get height of the root */
56
+ if (root==NULL )
57
+ return 0 ;
58
+ else
59
+ return 1 + max ( Height (root->left ) , Height (root->right ) );
60
+ }
61
+
62
+ // Function to determine whether given Tree is AVL Tree or not
63
+ bool IsAVLTree (struct Node * root)
64
+ {
65
+ /* If heights of subtrees differ atmost by 1 , return True
66
+ else return False */
67
+ if (abs ( Height (root->left ) - Height (root->right ) ) <= 1 )
68
+ return true ;
69
+ else
70
+ return false ;
71
+ }
72
+
73
+ // Driver code
74
+ int main ()
75
+ {
76
+ int n;
77
+ cout<<" Enter total no.of nodes of the input Tree ( including NULL nodes ) : " ;
78
+ cin>>n;
79
+
80
+ // create a null node and initialise it as a NULL node
81
+ struct Node * root;
82
+ root=NULL ;
83
+
84
+ cout<<" Enter value of each node of the Tree , with head as the first value ( if a node is NULL , enter -1 ) with spaces" <<endl;
85
+ for (int i=0 ;i<n;i++)
86
+ {
87
+ int data;
88
+ cin>>data;
89
+ // take input of data of each node and insert it in the root
90
+ root = insert (root,data);
91
+ }
92
+
93
+ // Call the function and print the result
94
+ if (IsAVLTree (root))
95
+ cout<<" Hence given BST is a AVL Tree" ;
96
+ else
97
+ cout<<" Hence given BST is not a AVL Tree" ;
98
+
99
+ return 0 ;
100
+ }
101
+
102
+ /*
103
+ While entering node values of the tree ,
104
+ value of the head of the tree should be enetered first
105
+
106
+ Input Binary Tree:
107
+ 11
108
+ / \
109
+ 2 20
110
+ / \
111
+ 1 5
112
+ /
113
+ 4
114
+ Input:
115
+ Enter total no.of nodes of the input Tree ( including NULL nodes ) : 6
116
+ Enter value of each node of the Tree , with head as the first value ( if a node is NULL , enter -1 ) with spaces
117
+ 11 2 20 1 5 4
118
+ Output:
119
+ Hence given BST is not a AVL Tree
120
+
121
+ Time Complexity : O(n)
122
+ Where n is the no.of nodes ofthe tree
123
+ Space Complexity : O(h)
124
+ Where h is the height of the tree
125
+ */
0 commit comments