-
Notifications
You must be signed in to change notification settings - Fork 0
/
AVL_tree.cpp
255 lines (230 loc) · 4.88 KB
/
AVL_tree.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
#include <iostream>
using namespace std;
class BST
{
public:
BST *left;
int data;
BST *right;
BST()
{
data = 0;
left = right = NULL;
}
};
int Height(BST *root)
{
if (!root)
return 0;
int L = Height(root->left);
int R = Height(root->right);
return ((L >= R) ? L+1: R+1 );
}
void printLevel(BST *root, int n)
{
if (!root)
return;
if (n == 0)
{
cout << root->data << endl;
return;
}
else
{
printLevel(root->left, n - 1);
printLevel(root->right, n - 1);
}
}
void LevelOrder(BST *root)
{
// using stack memory ;
int height = Height(root);
for (int i = 0; i < height; i++)
printLevel(root, i);
}
void Print2D(BST* root,int space){
if(!root)
return;
space+=5;
Print2D(root->right,space);
cout << endl;
for(int i=5;i<space; i++)
cout << " ";
cout << root->data <<endl;
Print2D(root->left,space);
}
int getBF(BST* root){
return ((Height(root->left)) - (Height(root->right)));
}
void LLrotation(BST* &root){
BST* n2 = root->left;
BST* c3 = n2->right;
n2->right = root;
root->left =c3;
root = n2;
}
void RRrotation(BST* &root){
BST* n2 = root->right;
BST* c3 = n2->left;
n2->left = root;
root->right = c3;
root = n2;
}
void Insert(BST *&root, int data)
{
if (!root){
BST *newNode = new BST;
newNode->data = data;
root = newNode;
return;
}
if (data > root->data){
Insert(root->right,data);
}
else if (data < root->data){
Insert(root->left,data);
}
else{
cout << "Exist :"<<endl;
}
// AVL PART :
int bf = getBF(root);
if(bf > 1 && data < root->left ->data){
LLrotation(root);
}
else if(bf > 1 && data > root->left->data){
// LRrotation(root);
RRrotation(root->left);
LLrotation(root);
}
else if(bf < -1 && data > root->right->data){
RRrotation(root);
}
else if(bf < -1 && data < root->right->data){
// RLrotaion(root);
LLrotation(root->right);
RRrotation(root);
}
else
return;
}
BST * findMax(BST* root){
while(root->right)
root= root->right;
return root;
}
void Delete(BST* &root, int data){
if(!root)
return;
if(data < root->data )
Delete(root->left, data);
else if(data > root->data )
Delete(root->right,data);
else{
if(root->right && root->left){
BST* temp = findMax(root->left);
root->data = temp->data;
Delete(root->left,temp->data);
}
else{
BST* temp = root;
if(!root->left)
root = root->right;
else if(!root->right)
root = root->left;
delete temp;
return;
}
}
int bf = getBF(root);
if(bf >= 2 && getBF(root->left) >= 0){
LLrotation(root);
}
else if(bf >= 2 && getBF(root->left) <= 0 ){
RRrotation(root->left);
LLrotation(root);
}
else if(bf <= -2 && getBF(root->right) <= 0)
RRrotation(root);
else if(bf <= -2 && getBF(root->right) >= 1){
LLrotation(root->right);
RRrotation(root);
}
return;
}
BST * Find(BST *root, int data){
if(!root)
return NULL;
if(data < root->data)
return Find(root->left, data);
else if(data > root->data)
return Find(root->right,data);
else
return root;
}
void DeleteTree(BST* &root){
if(!root)
return;
DeleteTree(root->left);
DeleteTree(root->right);
BST* temp = root;
root= NULL;
delete temp;
return;
}
int main()
{
BST *root = NULL;
int ch;
while (1)
{
cout << "...1:insert 2:Find 3:Delete 4:Print2d 5:Height 6:LevelOrder 8:DeleteTree 9:EXIT..." << endl;
cin >> ch;
switch (ch)
{
case 1:
{
cout << "Enter Value " << endl;
cin >> ch;
Insert(root, ch);
break;
}
case 2:
{
cout << "Enter Value " << endl;
cin >> ch;
if (Find(root, ch))
cout << "Found: ";
else
cout << "Not found " << endl;
break;
}
case 3:
{
cout << "Enter Value " << endl;
cin >> ch;
Delete(root, ch);
break;
}
case 4:
Print2D(root,5);
cout << endl;
break;
case 5:
cout << "Height: " << Height(root)-1<< endl;
break;
case 6:
cout << "Level order: " << endl;
LevelOrder(root);
break;
case 8:
DeleteTree(root);
break;
case 9:
cout << "Exit code 000 " << endl;
return 0;
default:
cout << "Wrong Choise " << endl;
}
}
}