1
+ #include < iostream>
2
+
3
+ using namespace std ;
4
+
5
+ class Node
6
+ {
7
+ public:
8
+ int val;
9
+ Node *left, *right;
10
+
11
+ Node (){
12
+ this ->val = 0 ;
13
+ this ->left = NULL ;
14
+ this ->right = NULL ;
15
+ }
16
+
17
+ Node (int val){
18
+ this ->val = val;
19
+ this ->left = NULL ;
20
+ this ->right = NULL ;
21
+ }
22
+ };
23
+
24
+ class AVL
25
+ {
26
+
27
+ private:
28
+ Node* leftRotate (Node* t);
29
+ Node* rightRotate (Node* t);
30
+
31
+ public:
32
+ Node *root;
33
+
34
+ AVL ();
35
+ AVL (int val);
36
+
37
+ void insert (int val);
38
+ Node* insert (Node* t, Node* newnode);
39
+ void remove (int val);
40
+ Node* remove (Node* t, int val);
41
+
42
+ int getHeight (Node* t);
43
+ int getBalanceFactor (Node* t);
44
+ Node* minNode (Node* t);
45
+
46
+
47
+ void inOrder (Node* t);
48
+
49
+ };
50
+
51
+ AVL::AVL ()
52
+ {
53
+ this ->root = NULL ;
54
+ }
55
+
56
+ AVL::AVL (int val)
57
+ {
58
+ this ->root = new Node (val);
59
+ }
60
+
61
+ Node* AVL::rightRotate (Node* t)
62
+ {
63
+ Node *y = t->left ;
64
+ Node *z = y->right ;
65
+
66
+ y->right = t;
67
+ t->left = z;
68
+
69
+ return y;
70
+ }
71
+
72
+ Node* AVL::leftRotate (Node* t)
73
+ {
74
+ Node *y = t->right ;
75
+ Node *z = y->left ;
76
+
77
+ y->left = t;
78
+ t->right = z;
79
+
80
+ return y;
81
+ }
82
+
83
+ void AVL::insert (int val)
84
+ {
85
+ Node *newnode = new Node (val);
86
+
87
+ this ->root = insert (this ->root , newnode);
88
+ }
89
+
90
+ Node* AVL::insert (Node* t, Node* newnode)
91
+ {
92
+ if (t == NULL ){
93
+ t = newnode;
94
+ return t;
95
+ }
96
+ else if (newnode->val < t->val ){
97
+ t->left = insert (t->left , newnode);
98
+ }
99
+ else if (newnode->val > t->val ){
100
+ t->right = insert (t->right , newnode);
101
+ }
102
+ else {
103
+ cout << " \n\t\t NO DUPLICATES ALLOWED\n " ;
104
+ cout << " Cannot insert " << newnode->val << endl;
105
+ }
106
+
107
+
108
+ int bf = this ->getBalanceFactor (t);
109
+
110
+ if (bf > 1 && newnode->val < t->left ->val ){
111
+ return rightRotate (t);
112
+ }
113
+ else if (bf >1 && newnode->val > t->left ->val ){
114
+ t->left = leftRotate (t->left );
115
+ return rightRotate (t);
116
+ }
117
+ else if (bf < -1 && newnode->val > t->right ->val ){
118
+ return leftRotate (t);
119
+ }
120
+ else if (bf < -1 && newnode->val < t->right ->val ){
121
+ t->right = rightRotate (t->right );
122
+ return leftRotate (t);
123
+ }
124
+
125
+ return t;
126
+ }
127
+
128
+ Node* AVL::remove (Node *t, int val)
129
+ {
130
+ if (t == NULL ){
131
+ cout << " ELement not present " << val << endl;
132
+ return t;
133
+
134
+ }
135
+ else if (val < t->val ){
136
+ t->left = this ->remove (t->left , val);
137
+ }
138
+ else if (val > t->val ){
139
+ t->right = this ->remove (t->right , val);
140
+ }
141
+ else {
142
+ if (t->left == NULL ){
143
+ Node* d = t->right ;
144
+ delete t;
145
+ return d;
146
+ }
147
+ else if (t->right == NULL ){
148
+ Node* d = t->left ;
149
+ delete t;
150
+ return d;
151
+ }
152
+ else {
153
+ Node* min = minNode (t->right );
154
+ t->val = min->val ;
155
+ t->right = this ->remove (t->right , min->val );
156
+ }
157
+ }
158
+
159
+ int bf = getBalanceFactor (t);
160
+
161
+ if (bf > 1 && getBalanceFactor (t->left )>=0 ){
162
+ return rightRotate (t);
163
+ }
164
+ else if (bf> 1 && getBalanceFactor (t->left )<0 ){
165
+ t->left = leftRotate (t->left );
166
+ return rightRotate (t);
167
+ }
168
+ else if (bf < -1 && getBalanceFactor (t->right ) <=0 ){
169
+ return leftRotate (t);
170
+ }
171
+ else if (bf < -1 && getBalanceFactor (t->right ) > 0 ){
172
+ t->right = rightRotate (t->right );
173
+ return leftRotate (t);
174
+ }
175
+
176
+ return t;
177
+
178
+ }
179
+
180
+ void AVL::remove (int val)
181
+ {
182
+ this ->root = this ->remove (this ->root , val);
183
+ // cout << endl<< "In void remove" << this->root->val << " " << endl;;
184
+
185
+ }
186
+
187
+ Node* AVL::minNode (Node* t)
188
+ {
189
+ while (t->left != NULL ){
190
+ t=t->left ;
191
+ }
192
+
193
+ return t;
194
+ }
195
+
196
+ int AVL::getHeight (Node* t)
197
+ {
198
+ if (t == NULL )
199
+ return -1 ;
200
+ int leftHt = getHeight (t->left );
201
+ int rightHt = getHeight (t->right );
202
+ return ((leftHt>rightHt)? leftHt: rightHt) + 1 ;
203
+ }
204
+
205
+ int AVL::getBalanceFactor (Node *t)
206
+ {
207
+ if (t == NULL )
208
+ return -1 ;
209
+ return (getHeight (t->left ) - getHeight (t->right ));
210
+ }
211
+
212
+ void AVL::inOrder (Node* t)
213
+ {
214
+ if (t == NULL )
215
+ return ;
216
+ inOrder (t->left );
217
+ cout << t->val << " " ;
218
+ inOrder (t->right );
219
+ }
220
+
221
+ int main ()
222
+ {
223
+ AVL myAvl;
224
+ myAvl.insert (50 );
225
+ myAvl.insert (30 );
226
+ myAvl.insert (10 );
227
+ myAvl.insert (40 );
228
+ myAvl.insert (70 );
229
+ myAvl.insert (1 );
230
+ myAvl.insert (10 );
231
+ myAvl.insert (100 );
232
+ myAvl.insert (120 );
233
+
234
+ myAvl.inOrder (myAvl.root );
235
+ cout << endl;
236
+
237
+ cout << " Root: " << myAvl.root ->val <<endl;
238
+ myAvl.remove (30 );
239
+
240
+ myAvl.inOrder (myAvl.root );
241
+ cout << endl;
242
+ cout << " New Root: " << myAvl.root ->val <<endl;
243
+
244
+ return 0 ;
245
+
246
+ }
0 commit comments