-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbtree.cpp
359 lines (316 loc) · 9.28 KB
/
rbtree.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
353
354
355
356
357
358
359
/****************************
* Author: Uzay Uysal *
* Student Number: 150180039 *
****************************/
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <assert.h>
using namespace std;
//Simple node structure to represent players in a RBTree.
struct Node
{
//Player related
string name;
int points;
int rebounds;
int assists;
//RBTree related
Node* left;
Node* right;
Node* parent;
int color; //0 => black, 1 => red
Node();
~Node();
};
Node::Node()
{
left = NULL;
right = NULL;
parent = NULL;
}
Node::~Node()
{
delete left;
delete right;
}
//Red-Black Tree to insert player nodes which uses player names to compare the nodes.
struct RBTree
{
RBTree();
Node* root;
void insert(string, int, int, int);
void insertfix(Node*);
Node* search(string);
Node* search_helper(Node*, string);
void preorder();
void preorder_helper(Node*, int);
void inorder();
void inorder_helper(Node*);
Node* get_parent(Node*);
Node* get_grandparent(Node*);
Node* get_uncle(Node*);
Node* get_sibling(Node*);
void rotate_left(Node*);
void rotate_right(Node*);
~RBTree();
};
Node* RBTree::get_parent(Node* node)
{
return node == NULL ? NULL : node->parent;
}
Node* RBTree::get_grandparent(Node* node)
{
return get_parent(get_parent(node));
}
Node* RBTree::get_uncle(Node* node)
{
return get_sibling(get_parent(node));
}
Node* RBTree::get_sibling(Node* node)
{
Node* parent = get_parent(node);
if(parent == NULL) return NULL;
if(node == parent->left) return parent->right;
else return parent->left;
}
void RBTree::rotate_left(Node* node)
{
Node* temp = node->right;
Node* parent = get_parent(node);
//assert(temp != NULL); assertion to check if something impossible is happening
node->right = temp->left;
temp->left = node;
node->parent = temp;
if(node->right != NULL) node->right->parent = node;
if(parent != NULL)
{
if(node == parent->left) parent->left = temp;
else if (node == parent->right) parent->right = temp;
}
temp->parent = parent;
}
void RBTree::rotate_right(Node* node)
{
Node* temp = node->left;
Node* parent = get_parent(node);
//assert(temp != NULL); assertion to check if something impossible is happening
node->left = temp->right;
temp->right = node;
node->parent = temp;
if(node->left != NULL) node->left->parent = node;
if(parent != NULL)
{
if(node == parent->left) parent->left = temp;
else if (node == parent->right) parent->right = temp;
}
temp->parent = parent;
}
//Search function that calls a helper function that does BST Search over the tree.
//Since RBTrees are a special case of BST trees this works.
Node* RBTree::search(string name)
{
Node* result = search_helper(root, name);
if (result) return result;
else return NULL; //Later on it's possible to do something else if we can't find the value (like printing something to the console)
}
//Helper function that does BST Search.
Node* RBTree::search_helper(Node* root, string name)
{
if ((root == NULL) || (root->name == name)) return root; //Check if the root exists or we found the right value.
if (root->name < name) //If the key is bigger than the root then search in the right subtree.
{
return search_helper(root->right, name);
}
return search_helper(root->left, name); //If not search in the left subtree.
}
void RBTree::preorder()
{
preorder_helper(root, 0);
}
void RBTree::preorder_helper(Node* root, int level)
{
if(!root) return;
for (int i = 0; i < level; i++)
{
cout << '-';
}
if(root->color == 0)
{
cout << "(BLACK) " << root->name << endl;
}
else
{
cout << "(RED) " << root->name << endl;
}
preorder_helper(root->left, level+1);
preorder_helper(root->right, level+1);
}
void RBTree::insert(string name, int points, int rebounds, int assists)
{
Node* temp = new Node;
temp->name = name;
temp->points = points;
temp->rebounds = rebounds;
temp->assists = assists;
temp->color = 1;
Node* front = root;
Node* behind = root;
while(front)
{
behind = front;
if(front->name == name)
{
front->points += points;
front->rebounds += rebounds;
front->assists += assists;
return;
}
if(name > front->name) front = front->right;
else front = front->left;
}
if(behind == NULL) root = temp;
else if (behind->name > name) behind->left = temp;
else behind->right = temp;
temp->parent = behind;
insertfix(temp);
while(get_parent(root) != NULL) root = get_parent(root);
}
void RBTree::insertfix(Node* node)
{
if (get_parent(node) == NULL) node->color = 0; //If node is root fix the color
else if (get_parent(node)->color == 0) return; //If node's parent is black, no violation
else if (get_uncle(node) != NULL && get_uncle(node)->color == 1) //If node's uncle exists and it is red do recolor
{
get_parent(node)->color = 0;
get_uncle(node)->color = 0;
get_grandparent(node)->color = 1;
insertfix(get_grandparent(node));
}
else //If the uncle is black do rotation fixes
{
Node* parent = get_parent(node);
Node* grandparent = get_grandparent(node);
// G
// // \\
// P U
// \\
// N
if(node == parent->right && parent == grandparent->left)
{
rotate_left(parent);
node = node->left;
}
// G
// // \\
// U P
// //
// N
else if (node == parent->left && parent == grandparent->right)
{
rotate_right(parent);
node = node->right;
}
parent = get_parent(node);
grandparent = get_grandparent(node);
if(node == parent->left) rotate_right(grandparent);
else rotate_left(grandparent);
parent->color = 0;
grandparent->color = 1;
}
}
void RBTree::inorder()
{
inorder_helper(root);
}
void RBTree::inorder_helper(Node* root)
{
if(root == NULL) return;
inorder_helper(root->left);
cout << root->name << " ";
inorder_helper(root->right);
}
RBTree::RBTree()
{
root = NULL;
}
RBTree::~RBTree()
{
delete root;
}
int main(int argc, const char** argv) {
string filename = argv[1];
fstream file;
file.open(filename, ios_base::in);
bool once = true, sonce = true;
int points, assists, rebounds, mp = 0, ma = 0, mr = 0;
string line, oldseason, season, name, team, mps, mas, mrs;
getline(file, line); //Read the first dump line
RBTree* tree = new RBTree();
while(getline(file, line))
{
stringstream ss(line); //Pass the line to the ss
getline(ss, season, ','); //Read from ss using comma as delimiter
getline(ss, name, ',');
getline(ss, team, ',');
getline(ss, line, ','); rebounds = stoi(line);
getline(ss, line, ','); assists = stoi(line);
getline(ss, line); points = stoi(line);
if (sonce)
{
oldseason = season;
sonce = false;
}
if (oldseason != season)
{
cout << "End of the " << oldseason << " Season" << endl;
cout << "Max Points: " << mp << " - Player Name: " << mps << endl;
cout << "Max Assists: " << ma << " - Player Name: " << mas << endl;
cout << "Max Rebs: " << mr << " - Player Name: " << mrs << endl;
if(once)
{
tree->preorder();
once = false;
}
}
Node* search_res = tree->search(name);
if(search_res)
{
points += search_res->points;
rebounds += search_res->rebounds;
assists += search_res->assists;
}
if(points > mp)
{
mp = points;
mps = name;
}
if(assists > ma)
{
ma = assists;
mas = name;
}
if(rebounds > mr)
{
mr = rebounds;
mrs = name;
}
if(search_res)
{
points -= search_res->points;
rebounds -= search_res->rebounds;
assists -= search_res->assists;
}
tree->insert(name, points, rebounds, assists);
oldseason = season;
}
cout << "End of the " << oldseason << " Season" << endl;
cout << "Max Points: " << mp << " - Player Name: " << mps << endl;
cout << "Max Assists: " << ma << " - Player Name: " << mas << endl;
cout << "Max Rebs: " << mr << " - Player Name: " << mrs << endl;
//tree->inorder();
cout << endl;
delete tree;
return 0;
}