-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIntervalTree.cpp
More file actions
302 lines (246 loc) · 7.05 KB
/
Copy pathIntervalTree.cpp
File metadata and controls
302 lines (246 loc) · 7.05 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
#include "IntervalTree.h"
#include <stddef.h>
#include <iostream>
using namespace std;
// Utility function to insert nodes in the Interval tree
TreeNode* insert(TreeNode *root, TreeNode *p)
{
if (root == NULL){
return p;
}
float start = root->span->start_time;
// Check for duplicates. If so, just increase the counter of this node - useful for Red-Black Trees
if (p->span->start_time == root->span->start_time && p->span->end_time == root->span->end_time){
std::cout << "Time Span " << p->span->start_time << "," << p->span->end_time << " exists" << endl;
(p->count)++;
return p;
}
if (p->span->start_time < start){
root->left = insert(root->left, p);
root->left->parent = root;
}
else{
root->right = insert(root->right, p);
root->right->parent = root;
}
if (root->max < p->span->end_time)
root->max = p->span->end_time;
return root;
}
// Find max between two floats
float findMax(float a, float b){
return ((a > b) ? a : b);
}
// Rotate tree left - needed for Red-Black Trees
void IntervalTree::rotateLeft(TreeNode *&root, TreeNode *&pt)
{
TreeNode *pt_right = pt->right;
pt->right = pt_right->left;
if (pt->right != NULL)
pt->right->parent = pt;
pt_right->parent = pt->parent;
if (pt->parent == NULL)
root = pt_right;
else if (pt == pt->parent->left)
pt->parent->left = pt_right;
else
pt->parent->right = pt_right;
pt_right->left = pt;
pt->parent = pt_right;
// If statements to update the max value of the interval tree after rotation
if (pt->left == NULL && pt->right != NULL){
pt->max = findMax(pt->right->max, pt->span->end_time);
}
if (pt->left != NULL && pt->right != NULL){
pt->max = findMax(pt->left->max, findMax(pt->right->max, pt->span->end_time));
}
if (pt->left == NULL && pt->right == NULL){
pt->max = pt->span->end_time;
}
if (pt->left != NULL && pt->right == NULL){
pt->max = findMax(pt->left->max, pt->span->end_time);
}
if (pt_right->right == NULL){
pt_right->max = findMax(pt->max, pt_right->span->end_time);
}
else{
pt_right->max = findMax(pt->max, findMax(pt_right->right->max, pt_right->span->end_time));
}
}
// Rotate tree right - needed for Red-Black Trees
void IntervalTree::rotateRight(TreeNode *&root, TreeNode *&pt)
{
TreeNode *pt_left = pt->left;
pt->left = pt_left->right;
if (pt->left != NULL)
pt->left->parent = pt;
pt_left->parent = pt->parent;
if (pt->parent == NULL)
root = pt_left;
else if (pt == pt->parent->left)
pt->parent->left = pt_left;
else
pt->parent->right = pt_left;
pt_left->right = pt;
pt->parent = pt_left;
// If statements to update the max value of the interval tree after rotation
if (pt->left == NULL && pt->right != NULL){
pt->max = findMax(pt->right->max, pt->span->end_time);
}
if (pt->left != NULL && pt->right != NULL){
pt->max = findMax(pt->left->max, findMax(pt->right->max, pt->span->end_time));
}
if (pt->left == NULL && pt->right == NULL){
pt->max = pt->span->end_time;
}
if (pt->left != NULL && pt->right == NULL){
pt->max = findMax(pt->left->max, pt->span->end_time);
}
if (pt_left->left == NULL){
pt_left->max = findMax(pt->max, pt_left->span->end_time);
}
else{
pt_left->max = findMax(pt_left->left->max, findMax(pt->max, pt_left->span->end_time));
}
}
// This function fixes violations caused by Interval Tree insertion
void IntervalTree::balanceTree(TreeNode *&root, TreeNode *&pt)
{
TreeNode *parent_pt = NULL;
TreeNode *grand_parent_pt = NULL;
while ((pt != root) && (pt->color != BLACK) &&
(pt->parent->color == RED))
{
parent_pt = pt->parent;
grand_parent_pt = pt->parent->parent;
/* Case : A
Parent of pt is left child of Grand-parent of pt */
if (parent_pt == grand_parent_pt->left)
{
TreeNode *uncle_pt = grand_parent_pt->right;
/* Case : 1
The uncle of pt is also red
Only Recoloring required */
if (uncle_pt != NULL && uncle_pt->color == RED)
{
grand_parent_pt->color = RED;
parent_pt->color = BLACK;
uncle_pt->color = BLACK;
pt = grand_parent_pt;
}
else
{
/* Case : 2
pt is right child of its parent
Left-rotation required */
if (pt == parent_pt->right)
{
rotateLeft(root, parent_pt);
pt = parent_pt;
parent_pt = pt->parent;
}
/* Case : 3
pt is left child of its parent
Right-rotation required */
rotateRight(root, grand_parent_pt);
swap(parent_pt->color, grand_parent_pt->color);
pt = parent_pt;
}
}
/* Case : B
Parent of pt is right child of Grand-parent of pt */
else
{
TreeNode *uncle_pt = grand_parent_pt->left;
/* Case : 1
The uncle of pt is also red
Only Recoloring required */
if ((uncle_pt != NULL) && (uncle_pt->color == RED))
{
grand_parent_pt->color = RED;
parent_pt->color = BLACK;
uncle_pt->color = BLACK;
pt = grand_parent_pt;
}
else
{
/* Case : 2
pt is left child of its parent
Right-rotation required */
if (pt == parent_pt->left)
{
rotateRight(root, parent_pt);
pt = parent_pt;
parent_pt = pt->parent;
}
/* Case : 3
pt is right child of its parent
Left-rotation required */
rotateLeft(root, grand_parent_pt);
swap(parent_pt->color, grand_parent_pt->color);
pt = parent_pt;
}
}
}
root->color = BLACK;
}
// Check whether a time is within a range in the interval tree node
bool withinRange(Time t, float search_time){
if (t.start_time <= search_time && search_time <= t.end_time){
return true;
}
else{
return false;
}
}
// Utility function for Interval tree search
bool intervalSearch(TreeNode *root, float search_time)
{
// Base Case, tree is empty
if (root == NULL){
return NULL;
}
// Return true if the current node overlaps with the given time
if (withinRange(*(root->span), search_time)){
return true;
}
// If left child of root is present and max of left child is
// greater than or equal to given interval, then i may
// overlap with an interval is left subtree
if (root->left != NULL && root->left->max >= search_time){
return intervalSearch(root->left, search_time);
}
else{
// Else interval can only overlap with right subtree
return intervalSearch(root->right, search_time);
}
}
// Interface function to insert rush hours in the Interval tree
void IntervalTree::AddTimeSpan(float start_time, float end_time){
// Create a new Interval node
Time *tmp = new Time;
tmp->start_time = start_time;
tmp->end_time = end_time;
TreeNode *p = new TreeNode(tmp);
// Check whether root is empty. Then check whether the node to be added already exists and it is the root
if (root == NULL){
root = insert(root, p);
}
else if (p->span->start_time == root->span->start_time && p->span->end_time == root->span->end_time){
std::cout << "Time Span " << p->span->start_time << "," << p->span->end_time << " exists" << endl;
(root->count)++;
p = root;
}
else{
root = insert(root, p);
}
// Balancing step - Balance the tree using Red-Black Tree Algorithm
if (p->count == 0){
balanceTree(root, p);
}
}
// Interface function to check for rush hours
bool IntervalTree::IsRushHour(float time){
bool rush = intervalSearch(root, time);
return rush;
}