-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
interval_tree.h
238 lines (209 loc) · 5.83 KB
/
interval_tree.h
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
/*******************************************************************************
* DANIEL'S ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* INTERVAL-TREE
*
* Features:
* 1. red-black tree based
* 2. O(logn) lookup performance
* 3. range search [low, high]
*
* http://en.wikipedia.org/wiki/Interval_tree
*
******************************************************************************/
#ifndef ALGO_INTERVAL_TREE_H__
#define ALGO_INTERVAL_TREE_H__
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include "generic.h"
#include "stack.h"
#include "double_linked_list.h"
#include "rbtree.h"
namespace alg {
class IntervalTree:public RBTreeAbstract {
public:
/**
* Interval-Tree node definition
*/
typedef struct ivltree_node_t : public rbtree_node_t {
int low; // lower-bound
int high; // higher-bound
int m; // max subtree upper bound value
} * ivltree_node;
#define IVLNODE(rbnode) static_cast<ivltree_node>(rbnode)
#define IVLNODE_M(rbnode) (rbnode?IVLNODE(rbnode)->m:INT_MIN)
public:
/**
* ivltree_create
* initialized an interval tree
* same as init an red-black tree
*/
IntervalTree() { }
/**
* ivltree_lookup
*
* search range [low, high] for overlap, return only one element
* use lookup & delete & insert schema to get multiple elements
*
* NULL is returned if not found.
*/
ivltree_node lookup(int low, int high) {
ivltree_node n = IVLNODE(get_root());
while (n != NULL && (low > n->high || n->low > high)) { // should search in childs
if (n->left !=NULL && low <=IVLNODE(n->left)->m) n = IVLNODE(n->left); // path choice on m.
else n = IVLNODE(n->right);
}
return n;
}
/**
* ivltree_insert
* insert range [low, high] into red-black tree
*/
void insert(int low, int high) {
ivltree_node inserted_node = new_node(low, high, RED, NULL, NULL);
if (get_root() == NULL) {
set_root(inserted_node);
} else {
ivltree_node n = IVLNODE(get_root());
while (1) {
// update 'm' for each node traversed from root
if (inserted_node->m > n->m) {
n->m = inserted_node->m;
}
// find a proper position
if (low < n->low) {
if (n->left == NULL) {
n->left = inserted_node;
break;
} else {
n = IVLNODE(n->left);
}
} else {
if (n->right == NULL) {
n->right = inserted_node;
break;
} else {
n = IVLNODE(n->right);
}
}
}
inserted_node->parent = n;
}
insert_case1(inserted_node);
}
/**
* delete the key in the red-black tree
*/
void delete_key(ivltree_node n) {
rbtree_node child;
if (n == NULL) return;
/* Copy fields from predecessor and then delete it instead */
if (n->left != NULL && n->right != NULL) {
ivltree_node pred = IVLNODE(maximum_node(n->left));
n->low = pred->low;
n->high= pred->high;
n = pred;
}
// fixup the 'm' value until m is not the max value of the path.
fixup_m(n);
assert(n->left == NULL || n->right == NULL);
child = n->right == NULL ? n->left : n->right;
if (node_color(n) == BLACK) {
n->color = node_color(child);
delete_case1(n);
}
replace_node(n, child);
if (n->parent == NULL && child != NULL) // root
child->color = BLACK;
delete(n);
}
void print() {
print_helper(IVLNODE(get_root()), 0);
puts("");
}
void print_helper(ivltree_node n, int indent) {
int i;
if (n == NULL) {
fputs("<empty tree>", stdout);
return;
}
if (n->right != NULL) {
print_helper(IVLNODE(n->right), indent + INDENT_STEP);
}
for(i=0; i<indent; i++)
fputs(" ", stdout);
if (n->color == BLACK)
printf("[%d %d, m->%d]\n", n->low,n->high,n->m);
else
printf("*[%d %d, m->%d]\n", n->low, n->high,n->m);
if (n->left != NULL) {
print_helper(IVLNODE(n->left), indent + INDENT_STEP);
}
}
~IntervalTree() {
destruct(IVLNODE(get_root()));
}
private:
void destruct(ivltree_node n) {
if (n==NULL) return;
destruct(IVLNODE(n->left));
destruct(IVLNODE(n->right));
delete n;
}
/**
* fix 'm' value caused by rotation
*/
void rotate_left_callback(rbtree_node n, rbtree_node parent) {
// parent inherit max m value
IVLNODE(parent)->m = IVLNODE(n)->m;
// update node 'm' value by it's children.
IVLNODE(n)->m = Max(IVLNODE(n)->high, Max(IVLNODE_M(n->left), IVLNODE_M(n->right)));
}
void rotate_right_callback(rbtree_node n, rbtree_node parent) {
rotate_left_callback(n, parent);
}
/**
* fix up 'm' value caued by deletion
*/
void fixup_m(rbtree_node n) {
int m = IVLNODE(n)->m;
int m_new = Max(IVLNODE_M(n->left), IVLNODE_M(n->right));
// if current 'm' is not decided by n->high, just return.
if (m==m_new) return;
while(n->parent !=NULL) {
/*
parent(high)
/ \
n(m_new) sibling(m)
*/
IVLNODE(n->parent)->m =
Max(IVLNODE(n->parent)->high, Max(m_new, IVLNODE_M(sibling(n))));
if(IVLNODE_M(n->parent) > m) break; // since node n does not affect
// the result anymore, we break.
n = n->parent;
}
}
/**
* create a new node, and set default vales.
*/
ivltree_node new_node(int low, int high, color rbtree_node_color, rbtree_node left, rbtree_node right) {
ivltree_node result = new ivltree_node_t;
result->low = low;
result->high = high;
result->m = high;
result->color = rbtree_node_color;
result->left = left;
result->right = right;
if(left !=NULL) left->parent = result;
if(right!=NULL) right->parent = result;
result->parent = NULL;
return result;
}
};
}
#endif //