-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tree.cpp
328 lines (246 loc) · 8.81 KB
/
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
//Kanellaki Maria Anna - 1115201400060
#include "Tree.h"
treeNode::treeNode(PatientRecord *rec) //initializes a Tree node with data from given PatientRecord and null children
{
record = rec;
date = rec->getEntryDate();
left = NULL;
right = NULL;
height = -1; //height is set by Tree class during insertion
}
treeNode::~treeNode() //deletes node and pointers without deleting the children
{
//delete record; //patient records destroyed by list
//delete date; //dates deleted by list
left = NULL;
right = NULL;
delete left;
delete right;
}
Tree::Tree() //initializes empty Tree
{
root = NULL;
}
Tree::~Tree() //calls custom recursive function to delete Tree
{
destroyTree(root);
}
void Tree::insertInTree(treeNode *node) //inserts given treeNode
{
if (!root)
{
root = node;
node->setHeight(1); //root height = 1
return;
}
treeNode * temp = root;
while(temp->getRight() && temp->getLeft()) //finds 1st null child and inserts new node there
{
//moves to suitable child depending on the date
if (node->getDate()->earlierThan(temp->getDate()))
temp = temp->getLeft();
else
temp = temp->getRight();
}
if (temp->getRight())
temp->setLeft(node);
else
temp->setRight(node);
node->setHeight(temp->getHeight() + 1);
BalanceTree(node);
}
treeNode * Tree::rotateLeft(treeNode *node) //rotates left from given node
{
treeNode *curr = node->getRight();
treeNode *temp = curr->getLeft();
//rotate
curr->setLeft(node);
node->setRight(temp);
//set new heights
node->setHeight(max(node->getLeft()->getHeight(), node->getRight()->getHeight()) + 1);
curr->setHeight(max(curr->getLeft()->getHeight(), curr->getRight()->getHeight()) + 1);
//if root changed return new root else return null
if (curr->getHeight() == 1)
return curr;
if (node->getHeight() == 1)
return node;
if (temp->getHeight() == 1)
return temp;
return NULL;
}
treeNode * Tree::rotateRight(treeNode *node) //rotates right from given node
{
treeNode *curr = node->getLeft();
treeNode *temp = curr->getRight();
//rotate
curr->setRight(node);
node->setLeft(temp);
//set new heights
node->setHeight(max(node->getLeft()->getHeight(), node->getRight()->getHeight()) + 1);
curr->setHeight(max(curr->getLeft()->getHeight(), curr->getRight()->getHeight()) + 1);
//if root changed return new root else return null
if (curr->getHeight() == 1)
return curr;
if (node->getHeight() == 1)
return node;
if (temp->getHeight() == 1)
return temp;
return NULL;
}
void Tree::BalanceTree(treeNode *node) //does necessary rotations to keep Tree balanced
{
if (!node)
return;
if (!node->getLeft())
return;
if (!node->getRight())
return;
int balance = node->getLeft()->getHeight() - node->getRight()->getHeight();
while (balance < -1 || balance > 1)
{
if (balance > 1 && node->getDate()->earlierThan(node->getLeft()->getDate()))
rotateRight(node);
if (balance < -1 && node->getDate()->earlierThan(node->getRight()->getDate()))
rotateLeft(node);
if (balance > 1 && node->getDate()->earlierThan(node->getLeft()->getDate()))
{
rotateLeft(node->getLeft());
rotateRight(node);
}
if (balance < -1 && node->getDate()->earlierThan(node->getRight()->getDate()))
{
rotateRight(node->getRight());
rotateLeft(node);
}
balance = node->getLeft()->getHeight() - node->getRight()->getHeight();
}
}
void Tree::destroyTree(treeNode *node) //destroys whole Tree recursively
{
if (!node)
return;
destroyTree(node->getLeft());
destroyTree(node->getRight());
delete(node);
}
int Tree::countDiseased(treeNode * node) //accesses recursively all elements of the tree, counts them and returns their count
{
if (!root)
return 0;
int count = 1;
if (node->getLeft())
count += countDiseased(node->getLeft());
if (node->getRight())
count += countDiseased(node->getRight());
return count;
}
void Tree::countDiseasedDates(treeNode* node, Date *from, Date *to, int *count) //accesses recursively all elements of the tree and counts those between the 2 dates. returns their count
{
if (!node)
return;
//checks both enter and exit date
if (from->earlierThan(node->getDate()) && node->getDate()->earlierThan(to))
*(count) += 1;
else if (node->getRecord()->getExitDate() && from->earlierThan(node->getRecord()->getExitDate()) && node->getDate()->earlierThan(node->getRecord()->getExitDate()))
*(count) += 1;
if (node->getLeft())
countDiseasedDates(node->getLeft(), from, to, count);
if (node->getRight())
countDiseasedDates(node->getRight(), from, to, count);
}
void Tree::countDiseasedCountry(treeNode *node, Date *from, Date *to, string disease, int *count) //accesses recursively all elements of the tree and counts those between the 2 dates that had the given disease. returns their count
{ //created to work with country table
if (!node)
return;
//checks both enter and exit date
if (disease == node->getRecord()->getDiseaseId())
{
if (from->earlierThan(node->getDate()) && node->getDate()->earlierThan(to))
*(count) += 1;
else if (node->getRecord()->getExitDate() && from->earlierThan(node->getRecord()->getExitDate()) && node->getDate()->earlierThan(node->getRecord()->getExitDate()))
*(count) += 1;
}
if (node->getLeft())
countDiseasedCountry(node->getLeft(), from, to, disease, count);
if (node->getRight())
countDiseasedCountry(node->getRight(), from, to, disease, count);
}
//new functions
void Tree::countEnterOrExit(treeNode *node, Date *from, Date *to, int *count, string status) //exactly like CountDiseasedDates but counts only entered or exited
{ //status points to which dates are counted
if (!node)
return;
if (status == "ENTER") //counts only entered
{
if (from->earlierThan(node->getDate()) && node->getDate()->earlierThan(to))
*(count) += 1;
}
else //counts only exited
{
if (node->getRecord()->getExitDate() && from->earlierThan(node->getRecord()->getExitDate()) && node->getDate()->earlierThan(to))
*(count) += 1;
}
if (node->getLeft())
countEnterOrExit(node->getLeft(), from, to, count, status);
if (node->getRight())
countEnterOrExit(node->getRight(), from, to, count, status);
}
void Tree::countEnterOrExitCountry(treeNode *node, Date *from, Date *to, string disease, int *count, string status) //exactly like CountDiseasedCountry but counts only entered or exited
{ //status points to which dates are counted
if (!node)
return;
if (disease == node->getRecord()->getDiseaseId())
{
if (status == "ENTER") //counts only entered
{
if (from->earlierThan(node->getDate()) && node->getDate()->earlierThan(to))
*(count) += 1;
}
else //counts only exited
{
if (node->getRecord()->getExitDate() && from->earlierThan(node->getRecord()->getExitDate()) && node->getDate()->earlierThan(to))
*(count) += 1;
}
}
if (node->getLeft())
countEnterOrExitCountry(node->getLeft(), from, to, disease, count, status);
if (node->getRight())
countEnterOrExitCountry(node->getRight(), from, to, disease, count, status);
}
//getters and setters for both classes
treeNode *treeNode::getLeft() const
{
return left;
}
treeNode *treeNode::getRight() const
{
return right;
}
Date *treeNode::getDate() const
{
return date;
}
void treeNode::setLeft(treeNode *left)
{
treeNode::left = left;
}
void treeNode::setRight(treeNode *right)
{
treeNode::right = right;
}
void treeNode::setHeight(int height)
{
treeNode::height = height;
}
int treeNode::getHeight() const
{
return height;
}
PatientRecord *treeNode::getRecord() const
{
return record;
}
treeNode *Tree::getRoot() const
{
return root;
}