-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordered.cpp
More file actions
410 lines (376 loc) · 13.5 KB
/
ordered.cpp
File metadata and controls
410 lines (376 loc) · 13.5 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#include "ordered.h"
// Method for adding data to tree
void weatherMap::addData()
{
string line;
string word;
vector <string> row;
fstream file;
file.open("weather.csv", ios::in);
// How to read csv from:
// https://java2blog.com/read-csv-file-in-cpp/#:~:text=To%20read%20a%20CSV%20file%2C,variable%20as%20its%20second%20argument.
getline(file, line); //remove titles
while (getline(file, line))
{
row.clear();
stringstream str(line);
while(getline(str, word, '"'))
{
if (word == "" || word == ",")
{
continue;
}
row.push_back(word);
/*
* index legend
* 0 - precipitation
* 1 - full date
* 2 - month
* 3 - week
* 4 - year
* 5 - city
* 6 - code
* 7 - city/state
* 8 - state
* 9 - avgTemp
* 10 - maxTemp
* 11 - minTemp
* 12 - windDirection
* 13 - windSpeed
*/
}
insert({row[1], row[7]}, stoi(row[9]), stoi(row[11]), stoi(row[10]), stod(row[0]), stod(row[13]));
}
}
// Both actual code and pseudocode was referenced from my (Max Techoueyres) AVL Tree from Project 1
// Constructor && Destructor
weatherMap::weatherMap() {
root = nullptr;
this->addData();
}
weatherMap::~weatherMap() {
deleteMap(root);
}
// Insert Method and Helper
// Adds a node to the AVL Tree with all the data points while simultaneously making sure it stays balanced with rotations
void weatherMap::insert(pair <string, string> dateLocation, double avgTemp, double minTemp, double maxTemp, double precipitation, double windSpeed) {
this->root = insertHelper(this->root, dateLocation, avgTemp, minTemp, maxTemp, precipitation, windSpeed);
}
Node* weatherMap::insertHelper(Node *&root, pair <string, string> dateLocation, double avgTemp, double minTemp, double maxTemp, double precipitation, double windSpeed) {
if (root == nullptr) {
root = new Node(dateLocation, avgTemp, minTemp, maxTemp, precipitation, windSpeed);
}
else if (root->dateLocation == dateLocation) { // Delete if dateLocation is not unique
delete root;
root = nullptr;
}
else if (dateLocation < root->dateLocation)
root->left = insertHelper(root->left, dateLocation, avgTemp, minTemp, maxTemp, precipitation, windSpeed);
else
root->right = insertHelper(root->right, dateLocation, avgTemp, minTemp, maxTemp, precipitation, windSpeed);
// Check balance factor of nodes to assure the tree stays balanced using rotations
if (getBalanceFactor(root) > 1 && dateLocation < root->left->dateLocation)
return rotateRight(root);
else if (getBalanceFactor(root) > 1 && dateLocation > root->left->dateLocation)
return rotateLeftRight(root);
else if (getBalanceFactor(root) < -1 && dateLocation > root->right->dateLocation)
return rotateLeft(root);
else if (getBalanceFactor(root) < -1 && dateLocation < root->right->dateLocation)
return rotateRightLeft(root);
return root;
}
// Main command functions
vector <pair <int, string>> weatherMap::avgTemp(double low, double high) {
results.clear();
return avgTempHelper(this->root, low, high);
}
vector <pair <int, string>> weatherMap::minTemp(double min) {
results.clear();
minTempHelper(this->root, min);
sort(results.begin(), results.end(), greater<pair<int, string>>());
return results;
}
vector <pair <int, string>> weatherMap::maxTemp(double max) {
results.clear();
return maxTempHelper(this->root, max);
}
vector <pair <int, string>> weatherMap::windSpeed(double low, double high) {
results.clear();
return windSpeedHelper(this->root, low, high);
}
vector <pair <int, string>> weatherMap::precipitation(double low, double high) {
results.clear();
return precipitationHelper(this->root, low, high);
}
// Main command function helpers
vector <pair <int, string>> weatherMap::avgTempHelper(Node *node, double low, double high) {
/*
* Takes two ints as input, low and high.
* low acts as floor for avgTemp and high acts as ceiling.
* Adds locations to vector if their avgTemp data point is within range.
* If the location already exists in the vector it increases its frequency by one
* Location name is stored as the second part of the pair
* Frequency is stored as the first part of the pair
*/
if (node == nullptr)
return results;
else {
avgTempHelper(node->left, low, high);
if (node->avgTemp >= low && node->avgTemp <= high)
{
if (results.size() == 0) // Case where results are still empty
{
results.push_back(make_pair(1, node->dateLocation.second));
}
else
{
bool unique = true;
for (int j = 0; j < results.size(); j++)
{
if (results.at(j).second == node->dateLocation.second) // Case where location already exists in results
{
unique = false;
results.at(j).first++;
break;
}
}
if (unique) // Case where location does not exist in results yet
{
results.push_back(make_pair(1, node->dateLocation.second));
}
}
}
avgTempHelper(node->right, low, high);
}
// From https://www.geeksforgeeks.org/sort-c-stl/
sort(results.begin(), results.end(), greater<pair<int, string>>());
// Sorts vector so locations with the greatest frequency are at the beginning for easy access
return results;
}
vector <pair <int, string>> weatherMap::minTempHelper(Node *node, double min) {
/*
* Takes one int as input: low
* low acts as floor for minTemp.
* Adds locations to vector if their minTemp data point is greater than the input.
* If the location already exists in the vector it increases its frequency by one
* Location name is stored as the second part of the pair
* Frequency is stored as the first part of the pair
* Refer to avgTemp for more detailed comments on how this works.
* All the below functions function exactly the same.
*/
if (node == nullptr)
return results;
else
{
minTempHelper(node->left, min);
if (node->minTemp >= min)
{
if (results.size() == 0)
{
results.push_back(make_pair(1, node->dateLocation.second));
}
else
{
bool unique = true;
for (int j = 0; j < results.size(); j++)
{
if (results.at(j).second == node->dateLocation.second)
{
unique = false;
results.at(j).first++;
break;
}
}
if (unique)
{
results.push_back(make_pair(1, node->dateLocation.second));
}
}
}
minTempHelper(node->right, min);
}
return results;
}
vector <pair <int, string>> weatherMap::maxTempHelper(Node *node, double max) {
/*
* Takes one int as input: high
* low acts as ceiling for maxTemp.
* Adds locations to vector if their maxTemp data point is less than the input.
* If the location already exists in the vector it increases its frequency by one
* Location name is stored as the second part of the pair
* Frequency is stored as the first part of the pair
* Refer to avgTemp for more detailed comments on how this works.
* All the below functions function exactly the same.
*/
if (node == nullptr)
return results;
else {
maxTempHelper(node->left, max);
if (node->minTemp <= max)
{
if (results.size() == 0)
{
results.push_back(make_pair(1, node->dateLocation.second));
}
else
{
bool unique = true;
for (int j = 0; j < results.size(); j++)
{
if (results.at(j).second == node->dateLocation.second)
{
unique = false;
results.at(j).first++;
break;
}
}
if (unique)
{
results.push_back(make_pair(1, node->dateLocation.second));
}
}
}
maxTempHelper(node->right, max);
}
sort(results.begin(), results.end(), greater<pair<int, string>>());
return results;
}
vector <pair <int, string>> weatherMap::windSpeedHelper(Node *node, double low, double high) {
/*
* Takes two ints as input, low and high.
* low acts as floor for windSpd and high acts as ceiling.
* Adds locations to vector if their windSpd data point is within range.
* If the location already exists in the vector it increases its frequency by one
* Location name is stored as the second part of the pair
* Frequency is stored as the first part of the pair
*/
if (node == nullptr)
return results;
else {
windSpeedHelper(node->left, low, high);
if (node->windSpeed >= low && node->windSpeed <= high)
{
if (results.size() == 0)
{
results.push_back(make_pair(1, node->dateLocation.second));
}
else
{
bool unique = true;
for (int j = 0; j < results.size(); j++)
{
if (results.at(j).second == node->dateLocation.second)
{
unique = false;
results.at(j).first++;
break;
}
}
if (unique)
{
results.push_back(make_pair(1, node->dateLocation.second));
}
}
}
windSpeedHelper(node->right, low, high);
}
sort(results.begin(), results.end(), greater<pair<int, string>>());
return results;
}
vector <pair <int, string>> weatherMap::precipitationHelper(Node *node, double low, double high) {
/*
* Takes two ints as input, low and high.
* low acts as floor for precip and high acts as ceiling.
* Adds locations to vector if their avgTemp data point is within range.
* If the location already exists in the vector it increases its frequency by one
* Location name is stored as the second part of the pair
* Frequency is stored as the first part of the pair
*/
if (node == nullptr)
return results;
else {
precipitationHelper(node->left, low, high);
if (node->precipitation >= low && node->precipitation <= high)
{
if (results.size() == 0)
{
results.push_back(make_pair(1, node->dateLocation.second));
}
else
{
bool unique = true;
for (int j = 0; j < results.size(); j++)
{
if (results.at(j).second == node->dateLocation.second)
{
unique = false;
results.at(j).first++;
break;
}
}
if (unique)
{
results.push_back(make_pair(1, node->dateLocation.second));
}
}
}
precipitationHelper(node->right, low, high);
}
sort(results.begin(), results.end(), greater<pair<int, string>>());
return results;
}
// Rotate helper functions based off class slides
Node *weatherMap::rotateLeft(Node *node) {
Node *grandchild = node->right->left;
Node *newParent = node->right;
newParent->left = node;
node->right = grandchild;
return newParent;
}
Node *weatherMap::rotateRight(Node *node) {
Node *grandchild = node->left->right;
Node *newParent = node->left;
newParent->right = node;
node->left = grandchild;
return newParent;
}
Node *weatherMap::rotateLeftRight(Node *node) {
Node* greatGrandChild = node->left->right->left;
Node* newChild = node->left->right;
Node* grandChild = node->left;
node->left = newChild;
newChild->left = grandChild;
grandChild->right = greatGrandChild;
Node* newParent = rotateRight(node);
return newParent;
}
Node *weatherMap::rotateRightLeft(Node *node) {
Node* greatGrandChild = node->right->left->right;
Node* newChild = node->right->left;
Node* grandChild = node->right;
node->right = newChild;
newChild->right = grandChild;
grandChild->left = greatGrandChild;
Node* newParent = rotateLeft(node);
return newParent;
}
// Additional helper functions
void weatherMap::deleteMap(Node *node) {
if (node != nullptr) {
deleteMap(node->left);
deleteMap(node->right);
delete node;
node = nullptr;
}
}
double weatherMap::getHeight(Node *node) {
if (node == nullptr)
return 0;
else
return 1 + max(getHeight(node->left), getHeight(node->right));
}
double weatherMap::getBalanceFactor(Node *node) {
if (node == nullptr)
return 0;
return getHeight(node->left) - getHeight(node->right);
}