-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAVL.cpp
More file actions
246 lines (204 loc) · 5.96 KB
/
AVL.cpp
File metadata and controls
246 lines (204 loc) · 5.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include<bits/stdc++.h>
using namespace std;
//Implementation of AVL tree
struct Node{
int key;
struct Node *left;
struct Node *right;
int height;
};
struct Node *newNode(int data){
struct Node *N = (struct Node *)malloc(sizeof(struct Node));
N->key = data;
N->left = NULL;
N->right = NULL;
N->height = 1;
return N;
}
int height(struct Node *N)
{
if (N == NULL)
return 0;
return N->height;
}
int getBalance(struct Node *N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
struct Node *rightrotate(struct Node *y){
struct Node *x = y->left;
struct Node *t2 = x->right;
x->right = y;
y->left = t2;
x->height = height(x->left) > height(x->right) ? height(x->left) : height(x->right);
y->height = height(y->left) > height(y->right) ? height(y->left) : height(y->right);
return x;
}
struct Node *leftrotate(struct Node *x){
struct Node *y = x->right;
struct Node *t2 = y->left;
x->right = t2;
y->left = x;
x->height = height(x->left) > height(x->right) ? height(x->left) : height(x->right);
y->height = height(y->left) > height(y->right) ? height(y->left) : height(y->right);
return y;
}
struct Node*Insert(struct Node* node, int key)
{
if (node == NULL)
return(newNode(key));
if (key < node->key)
node->left = Insert(node->left, key);
else if (key > node->key)
node->right = Insert(node->right, key);
else //equal keys
return node;
node->height = 1 + max(height(node->left), height(node->right));
int balance = getBalance(node);
// left left case
if (balance > 1 && key < node->left->key) return rightrotate(node);
// right right case
if (balance < -1 && key > node->right->key) return leftrotate(node);
// left right case
if (balance > 1 && key > node->left->key){
node->left = leftrotate(node->left);
return rightrotate(node);
}
// right left case
if (balance < -1 && key < node->right->key){
node->right = rightrotate(node->right);
return leftrotate(node);
}
return node;
}
struct Node * Min(struct Node* node) {
struct Node* temp = node;
while (temp->left != NULL)
temp = temp->left;
return temp;
}
struct Node* Delete(struct Node* root, int key){
if (root == NULL)
return root;
if ( key < root->key )
root->left = Delete(root->left, key);
else if( key > root->key )
root->right = Delete(root->right, key);
else{
if( (root->left == NULL) || (root->right == NULL) ) {
struct Node *temp = root->left ? root->left : root->right;
// No child
if (temp == NULL){
temp = root;
root = NULL;
}
else //one child case
*root = *temp;
free(temp);
}
else{
//node with two children
struct Node* temp = Min(root->right);
root->key = temp->key;
root->right = Delete(root->right, temp->key);
}
}
if (root == NULL)
return root;
root->height = 1 + max(height(root->left),
height(root->right));
int balance = getBalance(root);
// left left case
if (balance > 1 && key < root->left->key) return rightrotate(root);
// right right case
if (balance < -1 && key > root->right->key) return leftrotate(root);
// left right case
if (balance > 1 && key > root->left->key){
root->left = leftrotate(root->left);
return rightrotate(root);
}
// right left case
if (balance < -1 && key < root->right->key){
root->right = rightrotate(root->right);
return leftrotate(root);
}
return root;
}
struct Node *Search(int k, struct Node *root){
if(root == NULL || root->key == k) return root;
if(root->key > k) return Search(k, root->left);
return Search(k, root->right);
}
int Max(struct Node *root){
while(root->right!=NULL) root = root->right;
return root->key;
}
struct Node *join(struct Node *t1, struct Node *t2){
if(t1 == NULL) return t2;
if(t2 == NULL) return t1;
int m = Max(t1);
t1 = Delete(t1, m);
struct Node *root = newNode(m);
root->left = t1;
root->right = t2;
root->height = 1 + max(height(root->left),
height(root->right));
int balance = getBalance(root);
// left left case
if (balance > 1 ) return rightrotate(root);
// right right case
if (balance < -1 ) return leftrotate(root);
return root;
}
void preOrder(struct Node *root)
{
if(root != NULL)
{
printf("%d ", root->key);
preOrder(root->left);
preOrder(root->right);
}
}
int main(){
struct Node *root = NULL;
/* Constructing tree given in the above figure */
root = Insert(root, 9);
root = Insert(root, 5);
root = Insert(root, 10);
root = Insert(root, 0);
root = Insert(root, 6);
root = Insert(root, 11);
root = Insert(root, -1);
root = Insert(root, 1);
root = Insert(root, 2);
/* The constructed AVL Tree would be
9
/ \
1 10
/ \ \
0 5 11
/ / \
-1 2 6
*/
struct Node *root2 = NULL;
/* Constructing tree given in the above figure */
root2 = Insert(root2, 29);
root2 = Insert(root2, 52);
root2 = Insert(root2, 102);
root2 = Insert(root2, 20);
root2 = Insert(root2, 62);
root2 = Insert(root2, 112);
root2 = Insert(root2, -12);
root2 = Insert(root2, 12);
root2 = Insert(root2, 22);
preOrder(root2);
cout<<endl;
struct Node *t = join(root, root2);
preOrder(t);
cout<<endl;
struct Node *a = Search(29, root2);
cout<<a->key<<endl;
return 0;
}