Skip to content

Commit 44e60c4

Browse files
committed
Read AVL Trees
1 parent f3141ea commit 44e60c4

File tree

3 files changed

+221
-1
lines changed

3 files changed

+221
-1
lines changed

geekforgeeks/trees/AVL-Tree/a.out

-24 Bytes
Binary file not shown.
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/* AVL tree is a self-balancing Binary Search Tree (BST)
2+
* where the difference between heights of left and right
3+
* subtrees cannot be more than one for all nodes.
4+
* We can guarantee an upper bound of O(Logn) for all
5+
* operations. The height of an AVL tree is always O(Logn)
6+
* where n is the number of nodes in the tree.
7+
*/
8+
9+
#include<bits/stdc++.h>
10+
using namespace std;
11+
12+
struct Node{
13+
int data;
14+
int height;
15+
Node *right,*left;
16+
};
17+
18+
int height(Node* N){
19+
if (N == NULL)
20+
return 0;
21+
return N->height;
22+
}
23+
24+
int getBalance(Node* N){
25+
if(N == NULL)
26+
return 0;
27+
return height(N->left) - height(N->right);
28+
}
29+
30+
Node* newNode(int data){
31+
Node* temp=new Node;
32+
temp->data=data;
33+
temp->height=1;
34+
temp->right=NULL;
35+
temp->left=NULL;
36+
return temp;
37+
}
38+
39+
Node* rightRotate(Node* y){
40+
Node* x=y->left;
41+
Node* temp=x->right;
42+
43+
x->right=y;
44+
y->left=temp;
45+
46+
47+
// Update the height of the root at the last
48+
y->height = max(height(y->left), height(y->right))+1;
49+
x->height = max(height(x->left), height(x->right))+1;
50+
51+
return x;//new root
52+
}
53+
54+
Node* leftRotate(Node* x){
55+
Node* y=x->right;
56+
Node* temp=y->left;
57+
58+
y->left=x;
59+
x->right=temp;
60+
61+
x->height = max(height(x->left), height(x->right))+1;
62+
y->height = max(height(y->left), height(y->right))+1;
63+
64+
return y;//new root
65+
66+
}
67+
68+
Node* insert(Node* temp,int data){
69+
if(temp == NULL)
70+
return newNode(data);
71+
if(data>temp->data)
72+
temp->right=insert(temp->right,data);
73+
else if(data<temp->data)
74+
temp->left=insert(temp->left,data);
75+
else
76+
return temp; //no duplicates
77+
78+
temp->height = 1 + max(height(temp->left), height(temp->right));
79+
int balance = getBalance(temp);
80+
81+
//Left Left Case---------Right Rotation
82+
if(balance > 1 && data < temp->left->data)
83+
return rightRotate(temp);
84+
//Right Right Case-------Left Rotation
85+
if(balance < -1 && data > temp->right->data)
86+
return leftRotate(temp);
87+
//Left Right Case--------Left Right Rotation
88+
if(balance > 1 && data > temp->left->data){
89+
temp->left = leftRotate(temp->left);
90+
return rightRotate(temp);
91+
}
92+
//Right Left Case--------Right Left Rotation
93+
if(balance < -1 && data < temp->right->data){
94+
temp->right = rightRotate(temp->right);
95+
return leftRotate(temp);
96+
}
97+
return temp;
98+
}
99+
100+
101+
Node* minValueNode(Node* temp){
102+
Node* current=temp;
103+
while(current->left != NULL){
104+
current= current->left;
105+
}
106+
return current;
107+
}
108+
109+
Node* deleteNode(Node* root,int data){
110+
if(root== NULL)
111+
return root;
112+
if(data > root->data)
113+
root->right = deleteNode(root->right,data);
114+
else if(data < root->data)
115+
root->left = deleteNode(root->left,data);
116+
else
117+
{
118+
if(root->left == NULL){
119+
Node* temp=root->right;
120+
free(root);
121+
return temp;
122+
}
123+
else if(root->right == NULL){
124+
Node* temp=root->left;
125+
free(root);
126+
return temp;
127+
}
128+
129+
Node *temp=minValueNode(root->right);
130+
root->data=temp->data;
131+
root->right=deleteNode(root->right,temp->data);
132+
}
133+
if(root==NULL)
134+
return root;
135+
136+
root->height=1+max(height(root->left),height(root->right));
137+
138+
int balance=getBalance(root);
139+
140+
// Left Left Case
141+
if (balance > 1 && getBalance(root->left) >= 0)
142+
return rightRotate(root);
143+
144+
// Left Right Case
145+
if (balance > 1 && getBalance(root->left) < 0)
146+
{
147+
root->left = leftRotate(root->left);
148+
return rightRotate(root);
149+
}
150+
151+
// Right Right Case
152+
if (balance < -1 && getBalance(root->right) <= 0)
153+
return leftRotate(root);
154+
155+
// Right Left Case
156+
if (balance < -1 && getBalance(root->right) > 0)
157+
{
158+
root->right = rightRotate(root->right);
159+
return leftRotate(root);
160+
}
161+
162+
return root;
163+
}
164+
165+
void preOrder(Node *root)
166+
{
167+
if(!root)
168+
return;
169+
cout<<root->data<<" ";
170+
preOrder(root->left);
171+
preOrder(root->right);
172+
}
173+
174+
int main()
175+
{
176+
Node *root = NULL;
177+
178+
root = insert(root, 9);
179+
root = insert(root, 5);
180+
root = insert(root, 10);
181+
root = insert(root, 0);
182+
root = insert(root, 6);
183+
root = insert(root, 11);
184+
root = insert(root, -1);
185+
root = insert(root, 1);
186+
root = insert(root, 2);
187+
188+
/* The constructed AVL Tree would be
189+
9
190+
/ \
191+
1 10
192+
/ \ \
193+
0 5 11
194+
/ / \
195+
-1 2 6
196+
*/
197+
198+
printf("Preorder traversal of the constructed AVL "
199+
"tree is \n");
200+
preOrder(root);
201+
202+
root = deleteNode(root, 10);
203+
204+
/* The AVL Tree after deletion of 10
205+
1
206+
/ \
207+
0 9
208+
/ / \
209+
-1 5 11
210+
/ \
211+
2 6
212+
*/
213+
214+
printf("\nPreorder traversal after deletion of 10 \n");
215+
preOrder(root);
216+
217+
return 0;
218+
}
219+

geekforgeeks/trees/AVL-Tree/insert.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ Node* rightRotate(Node* y){
4343
x->right=y;
4444
y->left=temp;
4545

46+
y->height = max(height(y->left), height(y->right))+1;
4647
x->height = max(height(x->left), height(x->right))+1;
47-
y->height = max(height(y->left), height(y->right))+1;
48+
4849

4950
return x;//new root
5051
}

0 commit comments

Comments
 (0)