-
Notifications
You must be signed in to change notification settings - Fork 0
/
avl_tree.cpp
352 lines (338 loc) · 11 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
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
#include "avl_tree.h"
#include <iostream>
using namespace std;
namespace Tree {
/**
* Inserts a value into a tree. This method is called recursively.
* On first call, receives a tree root node. In each call, recomputes
* nodes height and balance and make rotations if necessary.
* @param node
* @param info
* @return
*/
Node* insert(Node *node, const int &info) {
if (node == NULL) {
node = (Node*)malloc(sizeof(Node));
node->left = NULL;
node->right = NULL;
node->info = info;
node->balance = 0;
node->height = 0;
return node;
} else {
if (info < node->info) {
node->left = insert(node->left, info);
} else if (info > node->info) {
node->right = insert(node->right, info);
} else {
cout << "Couldn't add node " << info << ": node is already on tree." << endl;
}
}
resetHeightAndBalance(node);
node = fix(node);
return node;
}
/**
* Removes a value from a tree. This method is called recursively.
* On first call, receives a tree root node. In each call, recomputes
* nodes height and balance and make rotations if necessary.
* @see removeNodeReference(Node *)
* @param node
* @param info
* @return
*/
Node *remove(Node *node, const int &info) {
Node *ptr = node;
Node *ptrParent = node;
int direction = 0;
do {
if (ptr->info == info) {
Node *newReference = removeNodeReference(ptr);
resetHeightAndBalance(newReference);
newReference = fix(newReference);
if (direction == +1) {
ptrParent->right = newReference;
} else if (direction == -1) {
ptrParent->left = newReference;
} else {
return newReference;
}
resetHeightAndBalance(node);
node = fix(node);
return node;
} else {
ptrParent = ptr;
if (info < ptr->info) {
direction = -1;
ptr = ptr->left;
} else if (info > ptr->info) {
direction = +1;
ptr = ptr->right;
}
}
} while (ptr != NULL);
cout << "Couldn't remove node " << info << ": node not found." << endl;
return node;
}
/**
* Removes a reference to a node.
* - If node is a leaf, the method just removes it.
* - If one of the children is null, removes parent and
* returns the not null child.
* - Otherwise, replaces node content with the rightmost child
* and removes that node.
* @param node
* @return
*/
Node *removeNodeReference(Node *node) {
Node *ptr;
if (node->left == NULL && node->right == NULL) {
free(node);
return NULL;
} else if (node->left == NULL && node->right != NULL) {
ptr = node->right;
free(node);
return ptr;
} else if (node->left != NULL && node->right == NULL) {
ptr = node->left;
free(node);
return ptr;
} else {
if (node->left->right == NULL) {
ptr = node->left;
node->left->right = node->right;
free(node);
return ptr;
} else {
Node *ptrParent;
ptr = node->left;
while (ptr->right != NULL) {
ptrParent = ptr;
ptr = ptr->right;
}
if (ptr->left != NULL) {
ptrParent->right = ptr->left;
} else {
ptrParent->right = NULL;
}
ptr->right = node->right;
ptr->left = node->left;
free(node);
return ptr;
}
}
return NULL;
}
Node *search(Node *node, const int &info) {
if (node == NULL) {
cout << "Couldn't find node " << info << ": node is not on tree. Returning NULL..." << endl;
return NULL;
} else {
if (info < node->info) {
node = search(node->left, info);
} else if (info > node->info) {
node = search(node->right, info);
}
}
return node;
}
/**
* Fixes a tree configuration by rotations.
* - If a node has a inverse balance in relation to
* his parent after the insertion, a double rotation
* on parent must be done. The direction depends only
* of the balance.
* - If both has the same balance signal, a single rotation
* on unbalanced node must be done.
* @param node
* @return
*/
Node *fix(Node* node) {
if (node != NULL) {
if (node->balance >= 2) {
if (node->left && node->left->balance > 0) {
cout << "Rotacao simples a direita no node " << node->info << endl;
node = singleRotationRight(node);
} else {
cout << "Rotacao dupla a direita no node " << node->info << endl;
node = doubleRotationRight(node);
}
} else if (node->balance <= -2) {
if (node->right && node->right->balance < 0) {
cout << "Rotacao simples a esquerda no node " << node->info << endl;
node = singleRotationLeft(node);
} else {
cout << "Rotacao dupla a esquerda no node " << node->info << endl;
node = doubleRotationLeft(node);
}
}
resetHeightAndBalance(node);
node->left = fix(node->left);
node->right = fix(node->right);
}
return node;
}
/**
* Prints all nodes of a tree or subtree according
* to the following order:
* 1º - Parent node;
* 2º - Left child;
* 3º - Right child;
* @param node
*/
void printPreOrder(Node *node) {
if (node != NULL) {
cout << node->info << "\t" << node->height << "\t" << node->balance << endl;
printPreOrder(node->left);
printPreOrder(node->right);
}
}
/**
* Recalculates a node height and balance and set it into the node.
* Height is given by: 1 + max(left_subtree, right_subtree);
* Balance is given by: height(left_child) - height(right_child);
* @brief resetHeightAndBalance
* @param node
*/
void resetHeightAndBalance(Node *node) {
int h_left = 0;
int h_right = 0;
if (node != NULL) {
if (node->left == NULL && node->right == NULL) {
node->height = 0;
node->balance = 0;
} else {
if (node->left != NULL) {
resetHeightAndBalance(node->left);
h_left = node->left->height + 1;
}
if (node->right != NULL) {
resetHeightAndBalance(node->right);
h_right = node->right->height + 1;
}
node->height = h_left > h_right ? h_left : h_right;
node->balance = h_left - h_right;
}
}
}
/**
* @brief singleRotationLeft
* @param oldRoot
* @return
*/
Node* singleRotationLeft(Node *oldRoot) {
Node *newRoot = oldRoot->right;
if (newRoot->left != NULL) {
oldRoot->right = newRoot->left;
} else {
oldRoot->right = NULL;
}
newRoot->left = oldRoot;
return newRoot;
}
/**
* @brief singleRotationRight
* @param oldRoot
* @return
*/
Node* singleRotationRight(Node *oldRoot) {
Node *newRoot = oldRoot->left;
if (newRoot->right != NULL) {
oldRoot->left = newRoot->right;
} else {
oldRoot->left = NULL;
}
newRoot->right = oldRoot;
return newRoot;
}
/**
* @brief doubleRotationRight
* @param oldRoot
* @return
*/
Node* doubleRotationRight(Node *oldRoot) {
Node *aux = oldRoot->left;
Node *newRoot = aux->right;
if (newRoot->left != NULL) {
aux->right = newRoot->left;
} else {
aux->right = NULL;
}
if (newRoot->right != NULL) {
oldRoot->left = newRoot->right;
} else {
oldRoot->left = NULL;
}
newRoot->left = aux;
newRoot->right = oldRoot;
return newRoot;
}
/**
* @brief doubleRotationLeft
* @param oldRoot
* @return
*/
Node* doubleRotationLeft(Node *oldRoot) {
Node *aux = oldRoot->right;
Node *newRoot = aux->left;
if (newRoot->left != NULL) {
oldRoot->right = newRoot->left;
} else {
oldRoot->right = NULL;
}
if (newRoot->right != NULL) {
aux->left = newRoot->right;
} else {
aux->left = NULL;
}
newRoot->left = oldRoot;
newRoot->right = aux;
return newRoot;
}
/**
* For each node, generates a string on .dot format
* for graph drawing and inserts it into 'nodes' and
* 'relationships'.
* @see drawTreeR(Node *, string &, string &)
* @param node
* @param nodes
* @param relationships
*/
void drawTreeR(Node *node, string &nodes, string &relationships) {
if (node != NULL) {
nodes.append(putNodeOnDotFormat(node->info));
nodes.append("\n");
if (node->left != NULL) {
relationships.append(putRelationshipOnDotFormat(node->info, node->left->info, -1));
relationships.append("\n");
drawTreeR(node->left, nodes, relationships);
}
if (node->right != NULL) {
relationships.append(putRelationshipOnDotFormat(node->info, node->right->info, +1));
relationships.append("\n");
drawTreeR(node->right, nodes, relationships);
}
}
}
/**
* Generate, compile and display a .dot file based on
* tree nodes. Writes it on a .png file.
* @param node
*/
void drawTree(Node *node) {
string header = "digraph g {\n";
header.append("node [shape = record,height=.1];\n");
string nodes;
string relationships;
drawTreeR(node, nodes, relationships);
header.append(nodes);
header.append(relationships);
header.append("}");
stringToFile(header, "temp.dot");
system("dot -Tpng temp.dot -o output.png");
// system("rm temp.dot");
system("echo OFF");
system("taskkill >nul 2>&1 /FI \"WINDOWTITLE eq output - Visualizador de Fotos do Windows");
system("output.png");
}
}