forked from aliya-rahmani/Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL.cpp
195 lines (167 loc) · 3.88 KB
/
AVL.cpp
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
#include<bits/stdc++.h>
using namespace std;
// implementing avl tree (self balancing binary search tree)
struct node {
int data;
node* right;
node* left;
int height;
};
// function finding the height of the tree
int height(node* n) {
if (n == NULL)
return 0;
else
return n->height;
}
// returning the balance factor of the tree
int getBalance(node* n) {
if (n == NULL)
return 0;
else
return (height(n->left) - height(n->right));
}
//utility function to implement the right-rotate case
node* right_rotate(node* y) {
node* x = y->left;
node* t2 = x->right;
x->right = y;
y->left = t2;
x->height = 1 + max(height(x->left), height(x->right));
y->height = 1 + max(height(y->left), height(y->right));
return x;
}
//utility function to implement the left rotate case
node* left_rotate(node* x) {
node* y = x->right;
node* t1 = y->left;
y->left = x;
x->right = t1;
x->height = 1 + max(height(x->left), height(x->right));
y->height = 1 + max(height(y->left), height(y->right));
return y;
}
//utility function to implement insertion
node* insert(node* root, int data) {
node* ptr = new node;
ptr->data = data;
ptr->right = ptr->left = NULL;
ptr->height = 1;
// implementing simple bst operations
if (root == NULL) {
root = ptr;
return root;
}
if (data < root->data) {
root->left = insert(root->left, data);
}
else if (data > root->data) {
root->right = insert(root->right, data);
}
else
return root;
root->height = 1 + max(height(root->left), height(root->right));
int balance = getBalance(root);
// left left case
if (balance > 1 && data < root->left->data)
return right_rotate(root);
if (balance < -1 && data > root->right->data)
return left_rotate(root);
if (balance > 1 && data > root->left->data)
{
root->left = left_rotate(root->left);
return right_rotate(root);
}
if (balance < -1 && data < root->right->data) {
root->right = right_rotate(root->right);
return left_rotate(root);
}
return root;
}
node* minVal(node* n) {
node* curr = n;
while (curr->left != NULL)
curr = curr->left;
return curr;
}
// utility funcion for node deletion
node* delete_avl(node* root, int data) {
if (root == NULL)
return NULL;
if (data < root->data) {
root->left = delete_avl(root->left, data);
}
else if (data > root->data) {
root->right = delete_avl(root->right, data);
}
// if the data == root->data
else {
// if the node has no child or 1 child
if ((root->left == NULL) || (root->right == NULL))
{
node* temp = root->left ? root->left : root->right;
if (temp == NULL)
{
temp = root;
root = NULL;
delete(temp);
}
else {
*root = *temp;
delete(temp);
}
}
// node has both the children
else {
node* inord_succ = minVal(root->right);
root->data = inord_succ->data;
root->right = delete_avl(root->right, inord_succ->data);
}
}
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 && getBalance(root->left) >= 0)
return right_rotate(root);
if (balance < -1 && getBalance(root->right) <= 0)
return left_rotate(root);
if (balance > 1 && getBalance(root->left) < 0)
{
root->left = left_rotate(root->left);
return right_rotate(root);
}
if (balance < -1 && getBalance(root->right) > 0) {
root->right = right_rotate(root->right);
return left_rotate(root);
}
return root;
}
void preorder(node* root) {
if (root == NULL)
return;
else {
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}
}
signed main() {
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
node* root = new node;
root = NULL;
root = insert(root, 6);
root = insert(root, 7);
root = insert(root, 8);
preorder(root);
cout << "\n";
root = delete_avl(root, 6);
preorder(root);
return 0;
}