-
Notifications
You must be signed in to change notification settings - Fork 0
/
AvlTree.h
300 lines (291 loc) · 7.25 KB
/
AvlTree.h
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
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include "LinkedList.h"
using namespace std;
//-------------------------------------------------------------------------------------------------------------------//
//------------------------------------------------- AVL Node Class --------------------------------------------------//
//-------------------------------------------------------------------------------------------------------------------//
template <class T>
class AVLNode
{
public:
string fileName; //File which has the key/index
int lineNum; //Line Number of the file having the key/index
int height; //Height of the tree
LinkedList<T> list; //List to store duplicates
T key; //Index
AVLNode<T>* left; //Pointer for left child
AVLNode<T>* right; //Pointer for right child
AVLNode() //Default constructor for node class
{
key;
height = 0;
lineNum = 0;
fileName = "";
left = NULL;
right = NULL;
list.head = NULL;
}
AVLNode(T k, int l, string f) //Parameterized constructor
{
key = k;
lineNum = l;
fileName = f;
height = 0;
left = NULL;
right = NULL;
//list.insert(k,l,f);
}
int calculateHeight(AVLNode<T>* n) //Function to calculate height of the tree
{
if (n == NULL)
return -1;
else
return n->height;
}
int max(int l, int r) //Function to find maximum height out of left and right subtrees
{
if (l < r)
return r;
else
return l;
}
~AVLNode() //Destructor for node class
{
key = 0;
height = 0;
lineNum = 0;
fileName = "";
delete left;
left = NULL;
delete right;
right = NULL;
}
};
//-------------------------------------------------------------------------------------------------------------------//
//------------------------------------------------- AVL Tree Class --------------------------------------------------//
//-------------------------------------------------------------------------------------------------------------------//
template <class T>
class AVLTree
{
public:
AVLNode<T>* root; //Root of the tree
AVLTree() //Default constructor
{
root = NULL;
}
void retrieve(AVLNode<T>* r, T k)
{
if (r == NULL)
{
cout << "Data Not Found" << endl;
return;
}
else
{
if (r->key == k)
{
string fileN = r->fileName;
fstream file;
string line;
file.open(fileN.c_str(), ios::app);
if (file.is_open())
{
int lineN = 0;
while (getline(file, line))
{
stringstream str(line);
if (lineN == r->lineNum)
cout << line << endl;
}
}
else
{
cout << "Couldn't open the file." << endl;
}
}
else if (k < r->key)
{
retrieve(r->left, k);
}
else if (k > r->key)
{
retrieve(r->right, k);
}
}
}
AVLNode<T>* insertNode(T k, AVLNode<T>*& r, int l, string f) //Insertion of a node in the tree
{
if (r == NULL) //In case root is NULL(no node in the tree)
{
r = new AVLNode<T>(k, l, f);
}
else
{ //In case there is some node in the tree
if (k < r->key) //If data to be inserted is lesser than current node
{
r->left = insertNode(k, r->left, l, f);
if ((r->calculateHeight(r->left) - r->calculateHeight(r->right)) == 2 || (r->calculateHeight(r->left) - r->calculateHeight(r->right)) == -2)
if (k < r->left->key)
r = rotateRR(r);
else
r = rotateRL(r);
}
else if (k > r->key) //If data to be inserted is greater than current node
{
r->right = insertNode(k, r->right, l, f);
if ((r->calculateHeight(r->left) - r->calculateHeight(r->right)) == 2 || (r->calculateHeight(r->left) - r->calculateHeight(r->right)) == -2)
if (k > r->right->key)
r = rotateLL(r);
else
r = rotateLR(r);
}
else if (k == r->key)
{ //If the data is a duplicate
//cout << "Here will be the linked list of duplicates" << endl;
r->list.insert(k, l, f);
}
}
r->height = r->max(r->calculateHeight(r->left), r->calculateHeight(r->right)) + 1; //Calculating the height
return r; //Returning the updated tree
}
AVLNode<T>* rotateRR(AVLNode<T>* v1)
{
AVLNode<T>* v2 = new AVLNode<T>;
if (v1->left != NULL)
{
v2 = v1->left;
v1->left = v2->right;
v2->right = v1;
}
v1->height = v1->max(v1->calculateHeight(v1->left), v1->calculateHeight(v1->right)) + 1;
v2->height = v2->max(v2->calculateHeight(v2->left), v1->height) + 1;
return v2;
}
AVLNode<T>* rotateLL(AVLNode<T>* v1)
{
AVLNode<T>* v2 = new AVLNode<T>;
if (v1->right != NULL)
{
v2 = v1->right;
v1->right = v2->left;
v2->left = v1;
}
v1->height = v1->max(v1->calculateHeight(v1->left), v1->calculateHeight(v1->right)) + 1;
v2->height = v2->max(v2->calculateHeight(v2->left), v1->height) + 1;
return v2;
}
AVLNode<T>* rotateRL(AVLNode<T>* v)
{
v->left = rotateRR(v->left);
v = rotateLL(v);
return v;
}
AVLNode<T>* rotateLR(AVLNode<T>* v)
{
v->right = rotateLL(v->right);
v = rotateRR(v);
return v;
}
void inOrdertraversal(AVLNode<T>* r)
{
if (r != NULL)
{
inOrdertraversal(r->left);
cout << r->key << " " << r->lineNum << " " << r->fileName << endl;
if (r->list.head != NULL)
{
cout << "--------------------------------------Duplicates-----------------------------------------" << endl;
r->list.printLL();
}
inOrdertraversal(r->right);
}
}
void saveInIDFile(AVLNode<T>* r, string filename)
{
fstream file;
file.open(filename.c_str(), ios::app);
if (file.is_open())
{
if (r != NULL)
{
saveInIDFile(r->left, filename);
file << r->key << "," << r->lineNum << "," << r->fileName << endl;
if (r->list.head != NULL)
{
Node<T>* temp;
temp = r->list.head;
while (temp)
{
file << temp->data << "," << temp->line << "," << temp->file << endl;
temp = temp->next;
}
}
saveInIDFile(r->right, filename);
}
}
else
{
cout << "File not open" << endl;
}
}
//void searchNode()
//{
//}
//void updateNode()
//{
//}
//AVLNode<T>* deletionOfNode(T k,AVLNode<T>* &r)
//{
// /Node<T> temp = root;
// Node* rec = NULL;
// while (temp)
// {
// if (temp->data == val)
// break;
// else if (val < temp->data)
// {
// rec = temp;
// temp = temp->left;
// }
// else if (val > temp->data)
// {
// rec = temp;
// temp = temp->right;
// }
// }
// if (temp->left == NULL)
// {
// Node* del = temp->right;
// rec->right = del;
// delete temp;
// }
// else if (temp->right == NULL)
// {
// Node* del = temp->left;
// rec->left = del;
// delete temp;
// }
// else
// {
// Node* temp2 = temp->right;
// Node* rec2 = NULL;
// while (temp2->left != NULL)
// {
// rec2 = temp;
// temp2 = temp2->left;
// }
// if (rec2 == NULL)
// temp->right = temp2->right;
// else
// rec2->left = temp2->right;
// temp->data = temp2->data;
// delete temp2;
// }*/
//}
~AVLTree()
{
}
};