-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredBlackTree.cpp
305 lines (295 loc) · 7.24 KB
/
redBlackTree.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
#pragma once
#include <map>
#include <iostream>
#include <iomanip>
#include <string>
#include <queue>
#include <vector>
#include "Game.h"
#include "Node.h"
#include "redBlackTree.h"
using namespace std;
void redBlackTree::insert(int season, Game game)
{
if (root == nullptr) //empty tree
{
Node* newNode = new Node(season, game);
newNode->changeColor(true);
root = newNode;
}
else
{
Node* newNode = new Node(season, game);
insertInside(root, newNode, game); // recursive function
if (newNode->parent != nullptr) // balance if game was not added to the root
{
balance(newNode);
}
}
}
Node* redBlackTree::insertInside(Node* currNode, Node* node, Game game) //Inspiration from project 1 made by Eitan Kogutek
{
if (currNode == nullptr) // creates new season
{
return node;
}
else if (currNode->season == node->season) // if season already existed on the tree
{
currNode->data.emplace(make_pair(game.homeTeam, game.awayTeam), game);
}
else if (node->season < currNode->season)
{
if (currNode->left == nullptr) // sets the parent if the new node is going to be set in the next iteration
{
node->parent = currNode;
}
currNode->left = insertInside(currNode->left, node, game);
}
else if (node->season > currNode->season)
{
if (currNode->right == nullptr) // sets the parent if the new node is going to be set in the next iteration
{
node->parent = currNode;
}
currNode->right = insertInside(currNode->right, node, game);
}
return currNode;
}
void redBlackTree::balance(Node* node) // inspired by algorithmtutor https://algorithmtutor.com/Data-Structures/Tree/Red-Black-Trees/
{
Node* uncle;
while (node->parent->black == false) //repeats as long as the parent is red
{
if (node->parent == node->parent->parent->right)
{
uncle = node->parent->parent->left; //sets the uncle
if (uncle != nullptr)
{
if (uncle->black == false) // 3.1 case
{
uncle->changeColor(true);
node->parent->changeColor(true);
node->parent->parent->changeColor(false);
node = node->parent->parent;
}
else
{
if (node == node->parent->left)
{
node = node->parent;
rotationLL(node);
}
node->parent->changeColor(true);
node->parent->parent->changeColor(false);
rotationRR(node->parent->parent);
}
}
else
{
if (node == node->parent->left)
{
node = node->parent;
rotationLL(node);
}
node->parent->changeColor(true);
node->parent->parent->changeColor(false);
rotationRR(node->parent->parent);
}
}
else
{
uncle = node->parent->parent->right; //sets the uncle
if (uncle != nullptr)
{
if (uncle->black == false)
{
uncle->changeColor(true);
node->parent->changeColor(true);
node->parent->parent->changeColor(false);
node = node->parent->parent;
}
else
{
if (node == node->parent->right)
{
node = node->parent;
rotationRR(node);
}
node->parent->changeColor(true);
node->parent->parent->changeColor(false);
rotationLL(node->parent->parent);
}
}
else
{
if (node == node->parent->right)
{
node = node->parent;
rotationRR(node);
}
node->parent->changeColor(true);
node->parent->parent->changeColor(false);
rotationLL(node->parent->parent);
}
}
if (node == root)
{
break;
}
}
root->changeColor(true);
}
Game redBlackTree::search(int season, string home, string visitor) //searches the specific game
{
Game temp = searchIterator(season, home, visitor, root);
return temp;
}
Game redBlackTree::searchIterator(int season, string home, string visitor, Node* node) //iterator for searcher
{
if (node->season == season)
{
auto iter = node->data.begin();
pair<string, string> searchGame = make_pair(home, visitor);
for (; iter != node->data.end(); ++iter)
{
if (iter->first == searchGame)
{
return iter->second;
}
}
}
if (node->season > season)
{
return searchIterator(season, home, visitor, node->left);
}
else if (node->season < season)
{
return searchIterator(season, home, visitor, node->right);
}
}
void redBlackTree::search(int season, string team) {
searchIterator(season, team, root);
}
void redBlackTree::searchIterator(int season, string team, Node* node) {
if (node->season == season)
{
team = "\"" + team + "\"";
int goalsFor = 0;
int goalsAgainst = 0;
int totalGames = 0;
for (auto& it : node->data) {
if (it.first.first == team || it.first.second == team) {
totalGames++;
if (it.first.first == team) {
goalsFor += it.second.hGoals;
goalsAgainst += it.second.aGoals;
}
if (it.first.second == team) {
goalsFor += it.second.aGoals;
goalsAgainst += it.second.hGoals;
}
}
}
//set precision to display only 2 decimals. snippet from
//https://stackoverflow.com/questions/22515592/how-to-use-setprecision-in-c
cout << fixed << showpoint;
cout << setprecision(2);
cout << endl;
cout << team.substr(1, team.size() - 2) << " record for the " << season << "-" << season + 1 << " season : " << endl;
cout << "Goals for: " << goalsFor << endl;
cout << "Goals against: " << goalsAgainst << endl;
cout << "Goal difference: " << goalsFor - goalsAgainst << endl;
cout << "Average goals scored: " << (float)goalsFor / (float)totalGames << endl;
cout << "Average goals received: " << (float)goalsAgainst / (float)totalGames << endl;
}
if (node->season > season)
{
searchIterator(season, team, node->left);
}
else if (node->season < season)
{
searchIterator(season, team, node->right);
}
}
void redBlackTree::levelOrder() // inspired by module4 stepik solution. used for testing the tree
{
queue<Node*> queue;
int levelNum = 1;
if (root == nullptr) { return; }
queue.push(root);
while (!queue.empty())
{
int size = queue.size();
vector<int> level;
for (int i = 0; i < size; i++)
{
Node* node = queue.front();
level.push_back(node->season);
queue.pop();
if (node->left != nullptr)
{
queue.push(node->left);
}
if (node->right != nullptr)
{
queue.push(node->right);
}
}
cout << "Level: " << levelNum << endl;
for (int item : level)
{
cout << item << " ";
}
cout << endl;
levelNum++;
}
}
//Rotations
void redBlackTree::rotationLL(Node* node)
{
Node* temp = node->left;
node->left = temp->right;
if (node->left != nullptr)
{
node->left->parent = node;
}
temp->parent = node->parent;
if (node->parent == nullptr)
{
root = temp;
}
else if (node == node->parent->left)
{
node->parent->left = temp;
}
else
{
node->parent->right = temp;
}
temp->right = node;
node->parent = temp;
}
void redBlackTree::rotationRR(Node* node)
{
Node* temp = node->right;
node->right = temp->left;
if (node->right != nullptr)
{
node->right->parent = node;
}
temp->parent = node->parent;
if (node->parent == nullptr)
{
root = temp;
}
else if (node == node->parent->left)
{
node->parent->left = temp;
}
else
{
node->parent->right = temp;
}
temp->left = node;
node->parent = temp;
}