This repository has been archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Interval_Tree.cpp
164 lines (164 loc) · 5.66 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
157
158
159
160
161
162
163
164
#include <iostream>
#include <vector>
#include <algorithm>
class Interval {
public:
int low, high;
Interval(int low, int high) : low(low), high(high) {}
};
class IntervalTreeNode {
public:
Interval interval;
int maxEnd;
IntervalTreeNode* left;
IntervalTreeNode* right;
IntervalTreeNode(const Interval& interval) : interval(interval), maxEnd(interval.high), left(nullptr), right(nullptr) {}
};
class IntervalTree {
private:
IntervalTreeNode* root;
public:
IntervalTree() : root(nullptr) {}
~IntervalTree() {
clear(root);
}
void insert(const Interval& interval) {
root = insert(root, interval);
}
void remove(const Interval& interval) {
root = remove(root, interval);
}
bool search(const Interval& interval) const {
return search(root, interval);
}
std::vector<Interval> queryOverlap(const Interval& interval) const {
std::vector<Interval> result;
queryOverlap(root, interval, result);
return result;
}
void inOrderTraversal() const {
inOrderTraversal(root);
std::cout << std::endl;
}
private:
IntervalTreeNode* insert(IntervalTreeNode* node, const Interval& interval) {
if (node == nullptr) {
return new IntervalTreeNode(interval);
}
if (interval.low < node->interval.low) {
node->left = insert(node->left, interval);
} else {
node->right = insert(node->right, interval);
}
node->maxEnd = std::max(node->maxEnd, interval.high);
return node;
}
IntervalTreeNode* remove(IntervalTreeNode* node, const Interval& interval) {
if (node == nullptr) {
return nullptr;
}
if (interval.low < node->interval.low) {
node->left = remove(node->left, interval);
} else if (interval.low > node->interval.low) {
node->right = remove(node->right, interval);
} else {
if (interval.high != node->interval.high) {
node->right = remove(node->right, interval);
} else {
if (node->left == nullptr) {
IntervalTreeNode* temp = node->right;
delete node;
return temp;
} else if (node->right == nullptr) {
IntervalTreeNode* temp = node->left;
delete node;
return temp;
}
IntervalTreeNode* successor = findMin(node->right);
node->interval = successor->interval;
node->right = remove(node->right, successor->interval);
}
}
if (node != nullptr) {
node->maxEnd = std::max(getMaxEnd(node->left), getMaxEnd(node->right));
}
return node;
}
bool search(IntervalTreeNode* node, const Interval& interval) const {
while (node != nullptr) {
if (doOverlap(node->interval, interval)) {
return true;
}
if (node->left != nullptr && node->left->maxEnd >= interval.low) {
node = node->left;
} else {
node = node->right;
}
}
return false;
}
void queryOverlap(IntervalTreeNode* node, const Interval& interval, std::vector<Interval>& result) const {
if (node == nullptr) {
return;
}
if (doOverlap(node->interval, interval)) {
result.push_back(node->interval);
}
if (node->left != nullptr && node->left->maxEnd >= interval.low) {
queryOverlap(node->left, interval, result);
}
queryOverlap(node->right, interval, result);
}
void inOrderTraversal(IntervalTreeNode* node) const {
if (node != nullptr) {
inOrderTraversal(node->left);
std::cout << "[" << node->interval.low << ", " << node->interval.high << "] ";
inOrderTraversal(node->right);
}
}
IntervalTreeNode* findMin(IntervalTreeNode* node) const {
while (node->left != nullptr) {
node = node->left;
}
return node;
}
int getMaxEnd(IntervalTreeNode* node) const {
return (node != nullptr) ? node->maxEnd : 0;
}
bool doOverlap(const Interval& interval1, const Interval& interval2) const {
return (interval1.low <= interval2.high && interval2.low <= interval1.high);
}
void clear(IntervalTreeNode* node) {
if (node != nullptr) {
clear(node->left);
clear(node->right);
delete node;
}
}
};
int main() {
IntervalTree intervalTree;
intervalTree.insert(Interval(15, 20));
intervalTree.insert(Interval(10, 30));
intervalTree.insert(Interval(5, 12));
intervalTree.insert(Interval(17, 19));
intervalTree.insert(Interval(30, 40));
std::cout << "In-Order Traversal: ";
intervalTree.inOrderTraversal();
std::cout << std::endl;
Interval searchInterval(14, 16);
std::cout << "Search for interval [" << searchInterval.low << ", " << searchInterval.high << "]: "
<< (intervalTree.search(searchInterval) ? "Found" : "Not Found") << std::endl;
Interval removeInterval(15, 20);
intervalTree.remove(removeInterval);
std::cout << "In-Order Traversal (After Removal): ";
intervalTree.inOrderTraversal();
std::cout << std::endl;
std::vector<Interval> overlapIntervals = intervalTree.queryOverlap(Interval(6, 16));
std::cout << "Overlapping Intervals with [6, 16]: ";
for (const Interval& interval : overlapIntervals) {
std::cout << "[" << interval.low << ", " << interval.high << "] ";
}
std::cout << std::endl;
return 0;
}