-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntervalTree.cpp
More file actions
298 lines (260 loc) · 7.68 KB
/
Copy pathIntervalTree.cpp
File metadata and controls
298 lines (260 loc) · 7.68 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
/*
* This file defines the class functions
* declared in "IntervalTree.h" header
*
* Author: Yash Gupta
* Copyright: Yash Gupta, SSRC - UC Santa Cruz
*/
#include "IntervalTree.h"
/* Default Constructor */
IntervalTree::IntervalTree(bool initialize, unsigned long passedDepthLevel)
{
tree = NULL;
_treeSize = 0;
_depthLevel = passedDepthLevel;
nodesAdded = 0;
isInitialized = false;
if (_depthLevel == 0) {
_treeSize = 1;
} else {
_treeSize = (1 << _depthLevel) - 1;
}
if (initialize == true) {
isInitialized = true;
tree = new IntervalNode[_treeSize];
_constructTree(ROOT_BEG, ROOT_END);
}
}
/* Destructor */
IntervalTree::~IntervalTree()
{
_garbageCollect();
}
/**
* Free's all the heap memory
* acquired by the Tree
*/
void
IntervalTree::_garbageCollect()
{
delete [] tree;
}
/**
* User facing contruct call which checks
* for a valid boolean and then initializes
* tree if valid
*/
void
IntervalTree::constructTree()
{
if (isInitialized) {
std::cout << "[CONSTRUCT] Tree has already been Initialized" << std::endl;
return;
} else {
isInitialized = true;
tree = new IntervalNode[_treeSize];
_constructTree(ROOT_BEG, ROOT_END);
}
}
/**
* Construct an interval tree iteratively
* using the intervals provided
*
* Arguments
* low: Low Interval value
* high: High Interval value
*/
void
IntervalTree::_constructTree(double low, double high)
{
unsigned long traverseLength;
if (_depthLevel == 0) {
traverseLength = 0;
} else {
traverseLength = (1 << (_depthLevel - 1)) - 1;
}
tree[0].observationsInInterval = 0;
tree[0].intervalSpan.low = low;
tree[0].intervalSpan.high = high;
for (unsigned long i = 0; i < traverseLength; i++) {
long leftChild = (i << 1) + 1;
long rightChild = (i << 1) + 2;
Interval temp = tree[i].intervalSpan;
tree[leftChild].observationsInInterval = 0;
tree[leftChild].intervalSpan.low = temp.low;
tree[leftChild].intervalSpan.high = (temp.low + temp.high) / 2.0;
tree[rightChild].observationsInInterval = 0;
tree[rightChild].intervalSpan.low = (temp.low + temp.high) / 2.0;
tree[rightChild].intervalSpan.high = temp.high;
}
}
/**
* Creates an interval structure based on
* the observation and Creates a node
* based on it
*
* Arguments
* observation: Observation to be added
*/
void
IntervalTree::add(double observation)
{
if (observation >= ROOT_BEG && observation <= ROOT_END) {
nodesAdded += 1;
_add(0, observation);
} else {
std::cout << "[ADD] Observation not within limit" << std::endl;
}
}
/**
* Traverses the tree and adds the
* observation to it
*
* Arguments
* index: Index of tree to add observation
* observation: value of observation
*/
void
IntervalTree::_add(long index, double observation)
{
long leftChild;
long rightChild;
Interval temp;
tree[index].observationsInInterval += 1;
leftChild = (index << 1) + 1;
rightChild = (index << 1) + 2;
temp = tree[index].intervalSpan;
// Check for left child interval
if (observation >= temp.low && observation < ((temp.low + temp.high) / 2.0)) {
if (leftChild < _treeSize) {
_add(leftChild, observation);
}
}
// Check for right child interval. Have a closed right interval if the high value is 1
if (temp.high == 1) {
if (observation >= ((temp.low + temp.high) / 2.0) && observation <= temp.high) {
if (rightChild < _treeSize) {
_add(rightChild, observation);
}
}
}
else if (observation >= ((temp.low + temp.high) / 2.0) && observation < temp.high) {
if (rightChild < _treeSize) {
_add(rightChild, observation);
}
}
}
/**
* Call median calculator
*/
double
IntervalTree::getApproxMedian()
{
if (nodesAdded == 0) {
std::cout << "[MEDIAN] Tree is Empty" << std::endl;
return -1;
} else {
return _getApproxMedian(0, ceil(nodesAdded / 2.0));
}
}
/**
* Calculate median based on EDM
* implementation.
*
* Arguments
* index: Calculate median of Tree/SubTree at index
* K: EDM implementation specific variable
*/
double
IntervalTree::_getApproxMedian(long index, long K)
{
long leftChild = (index << 1) + 1;
long rightChild = (index << 1) + 2;
Interval temp = tree[index].intervalSpan;
Interval tempLeft;
Interval tempRight;
if (isLeafNode(index)) {
if (K != tree[index].observationsInInterval) {
double low = temp.low;
double high = temp.high;
double weight = (double)K / (double)(tree[index].observationsInInterval);
return low + ((high - low) * weight);
} else if (K == tree[index].observationsInInterval) {
if (index + 1 < _treeSize) {
Interval tempNext = tree[index + 1].intervalSpan;
long observationsInNextInterval = tree[index + 1].observationsInInterval;
long observationsInCurrentInterval = tree[index].observationsInInterval;
double currentLow = temp.low;
double currentHigh = temp.high;
double nextLow = tempNext.low;
double nextHigh = tempNext.high;
double currentWeight = (currentHigh - currentLow) / (double)observationsInCurrentInterval;
double nextWeight = (nextHigh - nextLow) / (double)observationsInNextInterval;
return (currentWeight + nextWeight) / 2.0;
} else {
double low = temp.low;
double high = temp.high;
double weight = (double)K / (double)(tree[index].observationsInInterval);
return low + ((high - low) * weight);
}
}
}
tempLeft = tree[leftChild].intervalSpan;
tempRight = tree[rightChild].intervalSpan;
if (tree[index].observationsInInterval == K) {
long leftObservation = tree[leftChild].observationsInInterval;
long rightObservation = tree[rightChild].observationsInInterval;
double leftMidPoint = (tempLeft.low + tempLeft.high) / 2.0;
double rightMidPoint = (tempRight.low + tempRight.high) / 2.0;
double leftWeight = (double)leftObservation * leftMidPoint;
double rightWeight = (double)rightObservation * rightMidPoint;
double overallWeight = leftWeight + rightWeight;
return overallWeight / (double)(leftObservation + rightObservation);
}
if (tree[leftChild].observationsInInterval >= K) {
return _getApproxMedian(leftChild, K);
} else {
K = K - tree[leftChild].observationsInInterval;
return _getApproxMedian(rightChild, K);
}
// Control flow should never reach here. Error!
return -1;
}
/**
* Check tree for valid value and
* call displayer if valid
*/
void
IntervalTree::displayTree()
{
if (!isInitialized) {
std::cout << "[DISPLAY] Tree is not Initialized" << std::endl;
return;
}
_displayTree(0);
}
/**
* In order traversal of Tree
* to display all the intervals
*
* Arguments
* index: The index to begin traversing at
*/
void
IntervalTree::_displayTree(long index)
{
long leftChild = (index << 1) + 1;
long rightChild = (index << 1) + 2;
Interval temp = tree[index].intervalSpan;
if (leftChild < _treeSize) {
_displayTree(leftChild);
}
printf("%ld [%0.3f, %0.3f] Observations: %ld\n",
index,
temp.low,
temp.high,
tree[index].observationsInInterval);
if (rightChild < _treeSize) {
_displayTree(rightChild);
}
}