-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL.CPP
421 lines (409 loc) · 8.04 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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
struct avlNode
{
int data;
int height;
avlNode *LeftChild,*RightChild;
};
struct stacknode
{
avlNode *item;
stacknode *next;
};
class Stack
{
stacknode *top;
public:
Stack() { top=NULL; };
avlNode* ShowTop()
{
if (!IsEmpty())
return top->item;
return 0;
};
int IsEmpty()
{
if (top==NULL)
return 1;
return 0;
};
avlNode* Pop(avlNode*);
void Push(avlNode *ch);
};
void Stack::Push(avlNode *ch)
{
stacknode *p;
p=new stacknode();
p->item=ch;
p->next=top;
top=p;
}
avlNode* Stack::Pop(avlNode *x)
{
stacknode *p;
if (IsEmpty())
{
cout<<"Stack is Empty";
getch();
return 0;
}
x=top->item;
p=top;
top=top->next;
delete p;
return x;
}
class avl
{
public:
avlNode *root;
avl() { root =NULL; };
void insert(avlNode *);
avlNode* insert(avlNode *,avlNode*);
int height(avlNode *);
int parentCalculate(avlNode *, avlNode *);
void Delete(avlNode *);
avlNode* avl::LR(avlNode *);
avlNode* avl::RL(avlNode *);
avlNode* avl::RR(avlNode *);
avlNode* avl::LL(avlNode *);
void InOrder(avlNode*);
}a;
int max (int a,int b)
{
if (a>b)
return a;
else
return b;
}
void avl::insert(avlNode *newNode)
{
root = insert(newNode,root);
}
int avl::height(avlNode *x)
{
if(!x)
return -1;
else
return max(height(x->LeftChild),height(x->RightChild))+1;
}//end height
void avl::InOrder(avlNode *cur)
{
if (cur==NULL)
return;
else
{
InOrder(cur->LeftChild);
cout<<cur->data<<"\t";
InOrder(cur->RightChild);
}
}
avlNode* avl::insert(avlNode *newNode, avlNode *current)
{
int x = newNode->data;
if( !current )
{
current = newNode;
}//end if
else if( x < current->data )
{
current->LeftChild = insert(newNode,current->LeftChild);
if(height(current->LeftChild) - height(current->RightChild) == 2)
{
if( x < current->LeftChild->data )
current = LL(current);
else current = LR(current);
}//end if
}//end else if
else if( x > current->data )
{
current->RightChild = insert(newNode,current->RightChild);
if(height(current->LeftChild)- height(current->RightChild) == -2)
{
if(x > current->RightChild->data )
current = RR(current);
else current = RL(current);
}//end if
}//end else if
current->height = max ( height(current->LeftChild), height(current->RightChild)) + 1;
return current;
}//end insert
//=========== LR rotate in avl ================
avlNode *avl::LR(avlNode *a)
{
avlNode *b = a->LeftChild;
avlNode *c = b->RightChild;
b->RightChild = c->LeftChild;
a->LeftChild = c->RightChild;
c->RightChild = a;
c->LeftChild = b;
a->height = max ( height(a->LeftChild) , height(a->RightChild) ) + 1 ; b->height = max ( height(b->LeftChild) , height(b->RightChild) ) + 1 ; c->height = max ( height(a) , height(b) ) + 1 ; return c;
}//end LR
//=========== RL rotate in avl ================
avlNode *avl::RL(avlNode *a)
{
avlNode *b = a->RightChild;
avlNode *c = b->LeftChild;
a->RightChild = c->LeftChild;
b->LeftChild = c->RightChild;
c->RightChild = b;
c->LeftChild = a;
a->height = max ( height(a->LeftChild) , height(a->RightChild) ) + 1 ;
b->height = max ( height(b->LeftChild) , height(b->RightChild) ) + 1 ;
c->height = max ( height(a) , height(b) ) + 1 ;
return c;
}//end RL
//=========== LL rotate in avl ================
avlNode *avl::LL(avlNode *a)
{
avlNode *b = a->LeftChild;
a->LeftChild = b->RightChild;
b->RightChild = a;
a->height = max ( height (a->LeftChild) , height (a->RightChild)) + 1 ;
b->height = max ( height (a) , height(b->LeftChild) ) + 1 ;
return b;
}//end LL
//=========== RR rotate in avl ================
avlNode *avl::RR(avlNode *a)
{
avlNode *b = a->RightChild;
a->RightChild = b->LeftChild;
b->LeftChild = a;
a->height = max ( height(a->RightChild) , height (a->LeftChild) ) + 1 ;
b->height = max (height(a) , height(b->RightChild) ) + 1 ;
return b ;
}//end RR
void avl::Delete(avlNode *deletedNode)
{
Stack s;
avlNode *check;
int x = deletedNode->data;
avlNode *current = root ;
avlNode *parent = 0 ;
while ( current )
{
if( x < current->data )
{
s.Push(current);
parent = current;
current = current->LeftChild ;
}//end if
else if ( x > current->data )
{
s.Push(current);
parent = current;
current = current->RightChild;
}//end else if
else break;
}//end while
if ( !current )
{
cout<<"not found";
return;
}//end if(!current)
cout<<"\nDeleted...";
if( current->LeftChild && current->RightChild )//current has 2 children
{
s.Push(current);
avlNode *l = current->LeftChild,*p=current;
while(l->RightChild)
{
s.Push(l);
p=l;
l = l->RightChild;
}//end while
if(p!=current)
p->RightChild=l->LeftChild;
else
p->LeftChild = l->LeftChild;
current->data=l->data;
delete l;
}//end if
else if ( current->RightChild )//current has 1 child in right
{
if(!parent)
root = current->RightChild;
else
{
if( parent->RightChild == current )
{
parent->RightChild = current->RightChild;
s.Push(parent->RightChild);
}//end if
else
{
parent->LeftChild = current->RightChild ;
s.Push(parent->LeftChild);
}//end else
}//end else
delete current ;
}//end else if
else if( current->LeftChild )//current has 1 child in left
{
if(!parent)
root = current->LeftChild;
else
{
if(parent->RightChild == current )
{
parent->RightChild = current->LeftChild;
s.Push(parent->RightChild);
}//end if
else
{
parent->LeftChild = current->LeftChild;
s.Push(parent->LeftChild);
}//end else
}//end else
delete current;
}//end else if
else// current is a leaf
{
if(!parent)
root = 0;
else
{
if ( x > parent->data )
parent->RightChild = 0 ;
else
parent->LeftChild = 0 ;
}//end else
delete current;
}//end else ( leaf )
while(!s.IsEmpty())
{
avlNode *par;
s.Pop(check);
int v = parentCalculate(check,par);
if(height(check->LeftChild)-height(check->RightChild) == 2 )
{
if(height(check->LeftChild->RightChild ) > height(check->LeftChild->LeftChild))
{
if(par)
{
if( v == 0 )
par->LeftChild = LR(check);
else
par->RightChild = LR(check);
}//end if(par)
else
root = LR(check);
}//end if
else
{
if(par)
{
if(v == 0 )
par->LeftChild = LL(check);
else
par->RightChild = LL(check);
}//end if(par)
else
root = LL(check);
}//end else
}//end if else
if(height(check->LeftChild)-height(check->RightChild)== -2 )
{
if(height(check->RightChild->RightChild) < height(check->RightChild->LeftChild))
{
if(par)
{
if( v == 0 )
par->LeftChild = RL(check);
else
par->RightChild = RL(check);
}//end if(par)
else
root = RL(check);
}//end if
else
{
if(par)
{
if( v == 0 )
par->LeftChild = RR(check);
else
par->RightChild = RR(check);
}//end if(par)
else root = RR(check);
}//end else
}//end else if
}//end while
}//end delete function
int avl::parentCalculate(avlNode *x, avlNode *parent)
{
int right_or_left = 0;//0 :left; 1 :right parent = 0;
avlNode *p = root;
while(p)
{
if ( x->data > p->data )
{
parent = p ;
p = p->RightChild;
right_or_left = 1;
}//end if
else if( x->data < p->data )
{
parent = p ;
p = p->LeftChild;
right_or_left = 0;
}//end else if
else
break;
}//end while
return right_or_left;
}//end parent function
void Menu()
{
int n;
clrscr();
cout<<"\n1.Insert Number";
cout<<"\n2.Delete Number";
cout<<"\n3.Order";
cout<<"\n4.Exit";
cout<<"\n\nEnter n (1-4): ";
do {
gotoxy(1,8);
cout<<" ";
gotoxy(1,8);
cin>>n;
} while (n<1 || n>4);
switch (n)
{
case 1:
int num;
avlNode *cur=new avlNode;
clrscr();
cout<<"Enter Number: ";
cin>>num;
cur->data=num;
cur->LeftChild=cur->RightChild=NULL;
a.insert(cur);
Menu();
break;
case 2:
cur=new avlNode;
clrscr();
cout<<"Enter Number: ";
cin>>num;
cur->data=num;
a.Delete(cur);
getch();
Menu();
break;
case 3:
clrscr();
a.InOrder(a.root);
getch();
Menu();
break;
case 4:
exit(0);
}
}
void main()
{
Menu();
}