-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeDB.cpp
290 lines (252 loc) · 6.6 KB
/
TreeDB.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
#include "TreeDB.h"
TreeDB::TreeDB()
{
root = NULL;
probesCount = 0;
}
TreeDB::~TreeDB()
{
clear();
}
bool TreeDB::insert(DBentry* newEntry)
{
return insertNode(root, newEntry);
}
bool TreeDB::insertNode(TreeNode*& root, DBentry* newEntry)
{
if(root == NULL)
{
root = new TreeNode(newEntry);
probesCount += 1;
}
else if(newEntry->getName() < (root->getEntry())->getName())
{
//don't return here, we want balance() to execute
//for each root as the function stack pops
insertNode(root->left, newEntry);
}
else if(newEntry->getName() > (root->getEntry())->getName())
{
//same here
insertNode(root->right, newEntry);
}
else
{
//only if else case where we will return, we found a matching node
//that already exists so delete dynamically created DBentry,
//NULL the pointer, return false, no need to balance
delete newEntry;
newEntry = NULL;
return false;
}
//upon the function returning, balance each node as we move back
//up the tree and return true as we go (inserted)
balance(root);
return true;
}
DBentry* TreeDB::find(string name)
{
probesCount = 0;
return findNode(root, name);
}
DBentry* TreeDB::findNode(TreeNode*& root, string name)
{
if(root != NULL)
{
//to track probes visited (multi use function)
probesCount += 1;
if(name < (root->getEntry())->getName())
{
return findNode(root->left, name);
}
else if(name > (root->getEntry())->getName())
{
return findNode(root->right, name);
}
else
{
return root->getEntry();
}
}
else{return NULL;}
}
DBentry* TreeDB::findBiggestLeft(TreeNode*& root)
{
if(root->right != NULL)
{
return findBiggestLeft(root->right);
}
else
{
return root->getEntry();
}
}
bool TreeDB::remove(string name)
{
return removeNode(root, name);
}
bool TreeDB::removeNode(TreeNode*& root, string name)
{
if(root != NULL)
{
if(name < (root->getEntry())->getName())
{
//don't return here, we want balance() to execute
//for each root as the function stack pops
removeNode(root->left, name);
}
else if(name > (root->getEntry())->getName())
{
//same here
removeNode(root->right, name);
}
else
{
TreeNode* delPtr = root;
if(root->left == NULL && root->right == NULL)
{
delete delPtr;
root = NULL;
}
else if(root->left != NULL && root->right == NULL)
{
root = root->left;
delete delPtr;
}
else if (root->left == NULL && root->right != NULL)
{
root = root->right;
delete delPtr;
}
else
{
DBentry* biggestLeft = findBiggestLeft(root->left);
root->entryPtr = biggestLeft;
removeNode(root->left, biggestLeft->getName());
}
}
//balance the tree then return, if we are in this else block
//we now we will be deleting so we can balance and return true
balance(root);
return true;
}
else
{
//no need to balance, just return false
return false;
}
}
void TreeDB::clear()
{
clearAll(root);
}
void TreeDB::clearAll(TreeNode*& root)
{
if(root != NULL)
{
clearAll(root->left);
clearAll(root->right);
delete root;
root = NULL;
}
}
void TreeDB::printProbes() const
{
cout << probesCount << endl;
}
void TreeDB::countActive() const
{
int activeCount = 0;
TreeNode* temp_root = root;
count(temp_root, activeCount);
cout << activeCount << endl;
}
void TreeDB::count(TreeNode*& root, int& activeCount) const
{
if(root != NULL)
{
if((root->getEntry())->getActive())
activeCount += 1;
count(root->left, activeCount);
count(root->right, activeCount);
}
}
ostream& operator<< (ostream& out, const TreeDB& rhs)
{
out << rhs.root;
return out;
}
/*****************AVL TREE ADDITION**********************/
int TreeDB::getHeight(TreeNode*& root)
{
//if the pointer is NULL, return a height of 0
return (root ? root->height : 0);
}
int TreeDB::balanceFactor(TreeNode*& root)
{
//made right heavy positive, left heavy negative
return (getHeight(root->right) - getHeight(root->left));
}
void TreeDB::fixHeight(TreeNode*& root)
{
//get the height of the left and right subtrees
int hl = getHeight(root->left);
int hr = getHeight(root->right);
//whatever height is greater, this tree is one more
root->height = (hl > hr ? hl : hr) + 1;
}
void TreeDB::rotateRight(TreeNode*& rootP)
{
//rotates the root to the right
TreeNode* Q = rootP->left;
rootP->left = Q->right;
Q->right = rootP;
//fixes the heights of the changed trees
fixHeight(rootP);
fixHeight(Q);
//re assigns the root pointer (parent)
rootP = Q;
}
void TreeDB::rotateLeft(TreeNode*& rootQ)
{
//rotates the root to the left
TreeNode* P = rootQ->right;
rootQ->right = P->left;
P->left = rootQ;
//fixes the heights of the changed trees
fixHeight(rootQ);
fixHeight(P);
//re assigns the root pointer (parent)
rootQ = P;
}
void TreeDB::balance(TreeNode*& root)
{
fixHeight(root);
//by convention, we are right heavy
if(balanceFactor(root) == 2)
{
//if we have a zig-zag form, we must rotate right then left
if(balanceFactor(root->right) < 0)
{
cout << "rebalancing..." << endl;
rotateRight(root->right);
}
//if we have a linked list form, we only need to rotate left,
//if we had a zig zag form, we already rotated right, now left
cout << "rebalancing..." << endl;
rotateLeft(root);
}
else if(balanceFactor(root) == -2) //left heavy
{
//if we have a zig-zag form, we must rotate left then right
if(balanceFactor(root->left) > 0)
{
cout << "rebalancing..." << endl;
rotateLeft(root->left);
}
//if we have a linked list form, we only need to rotate right,
//if we had a zig zag form, we already rotated left, now right
cout << "rebalancing..." << endl;
rotateRight(root);
}
}