-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIntervalTree.cpp
150 lines (126 loc) · 3.32 KB
/
IntervalTree.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
// https://www.geeksforgeeks.org/interval-tree/
// https://iq.opengenus.org/interval-tree/
#include <iostream>
#include <vector>
#include <climits>
class Interval
{
private:
int low;
int high;
public:
Interval() { }
Interval(int l, int h) : low(l), high(h) {}
int getLow() { return low; }
int getHigh() { return high; }
void setLow(int l) { low = l; }
void setHigh(int h) { high = h; }
bool overlap(Interval& other)
{
if (this->low <= other.low && other.low <= this->high) {
return true;
}
return false;
}
};
class IntervalTree
{
private:
class Node
{
public:
Interval interval;
int max;
Node* left;
Node* right;
Node(Interval inter) : left{nullptr}, right{nullptr}
{
interval.setLow(inter.getLow());
interval.setHigh(inter.getHigh());
max = inter.getHigh();
}
};
Node* root;
void inorder(Node* node, std::vector<Node*>& path)
{
if (node == nullptr) {
return;
}
inorder(node->left, path);
path.push_back(node);
inorder(node->right, path);
}
void print(std::vector<Node*>& path)
{
for (auto node : path) {
std::cout << "[" << node->interval.getLow() << ", " << node->interval.getHigh() << "]"
<< " max = " << node->max << "\n";
}
}
Node* insert(Node* node, Interval& interval)
{
if (node == nullptr) {
return new Node(interval);
}
if (interval.getLow() < node->interval.getLow()) {
node->left = insert(node->left, interval);
} else {
node->right = insert(node->right, interval);
}
if (node->max < interval.getHigh()) {
node->max = interval.getHigh();
}
return node;
}
Node* search(Node* node, Interval& interval)
{
if (node == nullptr) {
return nullptr;
}
if (interval.overlap(node->interval)) {
return node;
}
if (node->left != nullptr && node->left->max >= interval.getLow()) {
return search(node->left, interval);
}
return search(node->right, interval);
}
public:
IntervalTree() : root{nullptr} { }
IntervalTree(std::vector<Interval>& intervals) : root{nullptr}
{
for (auto interval : intervals) {
root = insert(root, interval);
}
}
void inorder()
{
std::vector<Node*> path;
inorder(root, path);
print(path);
}
void search(Interval& interval)
{
if (root == nullptr) {
return;
}
Node* node = search(root, interval);
if (node == nullptr) {
std::cout << " No overlapping interval\n";
} else {
std::cout << " Overlaps with [" << node->interval.getLow() << ", " << node->interval.getHigh() << "]\n";
}
}
};
int main()
{
std::vector<Interval> intervals {{15, 20}, {10, 30}, {17, 19},
{5, 20}, {12, 15}, {30, 40}};
IntervalTree tree(intervals);
std::cout << "Inorder : \n";
tree.inorder();
Interval x {5, 6};
std::cout << "Searching for interval [" << x.getLow() << "," << x.getHigh() << "]";
tree.search(x);
return 0;
}