-
Notifications
You must be signed in to change notification settings - Fork 1
/
AVL.cpp
305 lines (249 loc) · 5.81 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
#include "NodeInterface.h"
#include "Node.h"
#include "AVL.h"
#include <algorithm>
#include <sstream>
#include <queue>
#include <string>
using namespace std;
string tsb(Node* bst) {
queue<NodeInterface*> readQ; // used to read in the levels of the tree, contains Node*
stringstream nodeReader_ss; // used to store the values of the nodes and the level-order sequence
int depth = 0; // the depth of a node on the tree
if (bst == NULL) {
return "BST is empty\n";
}
readQ.push(bst); // push the root node of the tree into the queue
while (!readQ.empty()) { // as long as the queue has a remaining node:
int i = readQ.size(); // store the number of nodes on this level of the tree
nodeReader_ss << depth << ": ";
for (; i > 0; i--) { // for each node on this level,
NodeInterface* nextNode = readQ.front(); // store the next node in the queue
nodeReader_ss << nextNode->getData() << " "; // store the data from the node into the ss
if (nextNode->getLeftChild() != NULL) { // if there is a left child, push the left child into the queue
readQ.push(nextNode->getLeftChild());
}
if (nextNode->getRightChild() != NULL) { // if there is a right child, push the left child into the queue
readQ.push(nextNode->getRightChild());
}
readQ.pop(); // pop the node off of the queue, leaving its children in the queue
}
nodeReader_ss << "\n"; // push an endl into the ss to distinguish levels
depth++;
}
return nodeReader_ss.str();
}
//Please note that the class that implements this interface must be made
//of objects which implement the NodeInterface
/*
* Returns the root node for this tree
*
* @return the root node for this tree.
*/
NodeInterface * AVL::getRootNode() const
{
return root;
}
/*
* Attempts to add the given int to the AVL tree
*
* @return true if added
* @return false if unsuccessful (i.e. the int is already in tree)
*/
bool AVL::add(int data)
{
add_function(root, data);
}
bool AVL::add_function(Node*& n, int value)
{
//cout << "Add_Function adding " << value << endl;
if (n == NULL)
{
n = new Node(value);
n->height = 0;
return true;
}
if (n->data == value)
{
//cout << "Add_Function FALSE FALSE FALSE" << endl;
return false;
}
if (n->data > value)
{
//cout << "Add_Function n->data > value" << endl;
bool fluff = add_function(n->leftChild, value);
if (fluff == true)
{
if (n->getBalance() > 1)
balanceLeft(n);
if (n->getBalance() < -1)
balanceRight(n);
}
return fluff;
}
if (n->data < value)
{
//cout << "Add_Function n->data < value" << endl;
bool fluff2 = add_function(n->rightChild, value);
if (fluff2 == true)
{
if (n->getBalance() > 1)
balanceLeft(n);
if (n->getBalance() < -1)
balanceRight(n);
}
return fluff2;
}
if (n->getBalance() > 1)
balanceLeft(n);
if (n->getBalance() < -1)
balanceRight(n);
return false;
}
/*
* Attempts to remove the given int from the AVL tree
*
* @return true if successfully removed
* @return false if remove is unsuccessful(i.e. the int is not in the tree)
*/
bool AVL::remove(int data)
{
bool fluff5;
fluff5 = remove_function(root, data);
balance(root);
return fluff5;
}
bool AVL::remove_function(Node*& n, int value)
{
//cout << "Remove_Function Removing " << value << endl;
if (n == NULL)
{
return false;
}
if (n->data > value)
{
//cout << "Remove_Function n > value " << value << endl;
bool fluff3 = remove_function(n->leftChild, value);
balance(n);
return fluff3;
}
if (n->data < value)
{
//cout << "Remove_Function n < value " << value << endl;
bool fluff4 = remove_function(n->rightChild, value);
balance(n);
return fluff4;
}
if (n->rightChild == NULL && n->leftChild == NULL) //No next of kin
{
//cout << "Remove_Function no children " << value << endl;
delete n;
n = NULL;
return true;
}
if (n->rightChild == NULL || n->leftChild == NULL) //one child
{
//cout << "Remove_Function one child " << value << endl;
Node* temp;
temp = n->leftChild;
if (n->leftChild == NULL)
{
temp = n->rightChild;
}
delete n;
n = temp;
balance(n);
return true;
}
Node* temp = fosterParent(n->leftChild);
temp->leftChild = n->leftChild;
temp->rightChild = n->rightChild;
delete n;
n = temp;
return true;
}
Node* AVL::fosterParent(Node*& n)
{
if (n->rightChild == NULL)
{
Node* temp = n;
n = n->leftChild;
return temp;
}
Node* temp = fosterParent(n->rightChild);
balance(n);
return temp;
}
/*
* Removes all nodes from the tree, resulting in an empty tree.
*/
void AVL::clear()
{
clear_function(root);
root = NULL;
}
void AVL::clear_function(Node*& n)
{
if (n == NULL)
{
return;
}
if (n->leftChild != NULL)
{
clear_function(n->leftChild);
}
if (n->rightChild != NULL)
{
clear_function(n->rightChild);
}
delete n;
return;
}
void AVL::balanceRight(Node*& n) //right-right and right-left
{
if (n == NULL)
return;
//cout << "balanceRight" << endl;
if (n->leftChild->getBalance() >= 1)
{
rotateLeft(n->leftChild);
}
rotateRight(n);
}
void AVL::balanceLeft(Node*& n) //left-left and left-right
{
if (n == NULL)
return;
//cout << "balanceLeft" << endl;
if (n->rightChild->getBalance() <= -1)
{
rotateRight(n->rightChild);
}
rotateLeft(n);
}
void AVL::rotateRight(Node*& n)
{
//cout << "rotateRight" << endl;
Node* temp = n->leftChild;
n->leftChild = temp->rightChild;
temp->rightChild = n;
n = temp;
}
void AVL::rotateLeft(Node*& n)
{
Node* temp = n->rightChild;
n->rightChild = temp->leftChild;
temp->leftChild = n;
n = temp;
}
void AVL::balance(Node*& n)
{
if (n == NULL)
return;
if (n->getBalance() > 1)
balanceLeft(n);
else if (n->getBalance() < -1)
balanceRight(n);
balance(n->leftChild);
balance(n->rightChild);
}