-
Notifications
You must be signed in to change notification settings - Fork 0
/
interval_tree.cpp
157 lines (142 loc) · 2.98 KB
/
interval_tree.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
#include "iostream"
using namespace std;
struct Node {
int start;
int end;
int max; // represents the max end in my subtree
Node *left;
Node *right;
};
Node * insert(Node *tree, int start, int end);
// returns NULL or a node that intersects with interval
Node * search(Node *tree, int start, int end);
Node * remove(Node *tree, int start);
void print(Node * tree);
int main() {
Node *t = NULL;
t = insert(t, 6, 8);
t = insert(t, 1, 4);
t = insert(t, 5, 10);
print(t);
cout << endl;
cout << "removing 6-8" << endl;
t = remove(t, 6);
print(t);
cout << endl;
cout << "searching for interval intersecting 7-12" << endl;
Node *s = search(t, 7, 12);
if (s != NULL) {
cout << s->start << " - " << s->end << endl;
}
else {
cout << "no intersection" << endl;
}
}
Node *insert(Node *tree, int start, int end)
{
if (tree == NULL) {
tree = new Node;
tree->start = start;
tree->end = end;
tree->max = end;
tree->left = NULL;
tree->right = NULL;
return tree;
}
else {
tree->max = max(tree->max, end);
if (start < tree->start) {
tree->left = insert(tree->left, start, end);
}
else {
tree->right = insert(tree->right, start, end);
}
return tree;
}
}
Node * search(Node *tree, int start, int end)
{
if (tree == NULL) {
return tree;
}
if (!(start > tree->end || end < tree->start)) {
return tree;
}
else {
// if left's max is < the start, go left
// otherwise go right
if (tree->left != NULL && start <= tree->max) {
return search(tree->left, start, end);
}
else {
return search(tree->right, start, end);
}
}
}
Node * remove(Node *tree, int start)
{
if (tree == NULL) {
return tree;
}
if (start < tree->start) {
tree->left = remove(tree->left, start);
}
else if (tree->start < start) {
tree->right = remove(tree->right, start);
}
else if (tree->start == start) {
// case I have no children
if (tree->left == NULL && tree->right == NULL) {
return NULL;
}
// case I have only left child
else if (tree->left != NULL) {
return tree->left;
}
// case I have only a right child
else if (tree->right != NULL) {
return tree->right;
}
// case I have two children
else {
Node *temp = tree;
// get the min on the right
temp = temp->right;
while (temp->left != NULL) {
temp = temp->left;
}
tree->start = temp->start;
tree->end = temp->end;
tree->right = remove(tree->right, temp->start);
}
}
// recalculate max value
int max = tree->max;
if (tree->left != NULL && max < tree->left->max) {
max = tree->left->max;
}
if (tree->right != NULL && max < tree->right->max) {
max = tree->right->max;
}
tree->max = max;
return tree;
}
void print(Node * tree)
{
if (tree == NULL) {
cout << " * ";
return;
}
else {
cout << "( s";
cout << tree->start;
cout << "-e";
cout << tree->end;
cout << "-m";
cout << tree->max;
cout << " ";
print(tree->left);
print(tree->right);
cout << ")";
}
}