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
/
Copy pathQuadTree2.cpp
282 lines (282 loc) · 9.46 KB
/
QuadTree2.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
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
// Comprehensive implementation of Quad Tree
#include <iostream>
#include <vector>
#include <cmath>
#include <limits>
#include <algorithm>
class Point {
public:
double x, y;
Point(double x, double y) : x(x), y(y) {}
};
class Rectangle {
public:
double x, y, width, height;
Rectangle(double x, double y, double width, double height)
: x(x), y(y), width(width), height(height) {}
bool contains(const Point& point) const {
return point.x >= x && point.x <= x + width && point.y >= y && point.y <= y + height;
}
bool intersects(const Rectangle& other) const {
return !(x + width < other.x || other.x + other.width < x ||
y + height < other.y || other.y + other.height < y);
}
};
template <typename T>
class QuadtreeNode {
public:
Rectangle bounds;
std::vector<T> data;
QuadtreeNode<T>* children[4];
QuadtreeNode(const Rectangle& bounds) : bounds(bounds) {
for (int i = 0; i < 4; ++i) {
children[i] = nullptr;
}
}
~QuadtreeNode() {
for (int i = 0; i < 4; ++i) {
delete children[i];
}
}
};
template <typename T>
class Quadtree {
public:
Quadtree(const Rectangle& bounds) : root(new QuadtreeNode<T>(bounds)) {}
~Quadtree() {
clear();
}
void insert(const Point& point, const T& data) {
insert(root, point, data);
}
void remove(const Point& point) {
remove(root, point);
}
bool search(const Point& point) const {
return search(root, point);
}
std::vector<T> queryRange(const Rectangle& range) const {
std::vector<T> result;
queryRange(root, range, result);
return result;
}
T nearestNeighbor(const Point& query) const {
if (root == nullptr) {
throw std::runtime_error("Quadtree is empty");
}
QuadtreeNode<T>* nearest = nullptr;
double minDistance = std::numeric_limits<double>::max();
nearestNeighbor(root, query, nearest, minDistance);
return nearest->data.front();
}
void update(const Point& point, const T& newData) {
remove(point);
insert(point, newData);
}
void clear() {
delete root;
root = new QuadtreeNode<T>(root->bounds);
}
void inOrderTraversal() const {
inOrderTraversal(root);
std::cout << std::endl;
}
void preOrderTraversal() const {
preOrderTraversal(root);
std::cout << std::endl;
}
void postOrderTraversal() const {
postOrderTraversal(root);
std::cout << std::endl;
}
std::size_t count() const {
return count(root);
}
bool isEmpty() const {
return count() == 0;
}
private:
QuadtreeNode<T>* root;
void insert(QuadtreeNode<T>* node, const Point& point, const T& data) {
if (!node->bounds.contains(point)) {
return;
}
if (node->data.size() < 4) {
node->data.push_back(data);
} else {
if (node->children[0] == nullptr) {
splitNode(node);
}
for (int i = 0; i < 4; ++i) {
insert(node->children[i], point, data);
}
}
}
void remove(QuadtreeNode<T>* node, const Point& point) {
if (node == nullptr) {
return;
}
auto it = std::find_if(node->data.begin(), node->data.end(),
[point](const T& d) { return d == point; });
if (it != node->data.end()) {
node->data.erase(it);
} else {
for (int i = 0; i < 4; ++i) {
remove(node->children[i], point);
}
}
}
bool search(QuadtreeNode<T>* node, const Point& point) const {
if (node == nullptr) {
return false;
}
auto it = std::find_if(node->data.begin(), node->data.end(),
[point](const T& d) { return d == point; });
if (it != node->data.end()) {
return true;
} else {
for (int i = 0; i < 4; ++i) {
if (node->children[i]->bounds.contains(point)) {
if (search(node->children[i], point)) {
return true;
}
}
}
return false;
}
}
void queryRange(QuadtreeNode<T>* node, const Rectangle& range, std::vector<T>& result) const {
if (node == nullptr) {
return;
}
if (node->bounds.intersects(range)) {
for (const T& data : node->data) {
if (range.contains(data)) {
result.push_back(data);
}
}
for (int i = 0; i < 4; ++i) {
queryRange(node->children[i], range, result);
}
}
}
void nearestNeighbor(QuadtreeNode<T>* node, const Point& query,
QuadtreeNode<T>*& nearestNode, double& minDistance) const {
if (node == nullptr) {
return;
}
if (node->bounds.intersects(getBoundingCircle(query, minDistance))) {
for (const T& data : node->data) {
double distance = distanceBetweenPoints(query, data);
if (distance < minDistance) {
minDistance = distance;
nearestNode = node;
}
}
for (int i = 0; i < 4; ++i) {
nearestNeighbor(node->children[i], query, nearestNode, minDistance);
}
}
}
Rectangle getBoundingCircle(const Point& center, double radius) const {
return Rectangle(center.x - radius, center.y - radius, 2 * radius, 2 * radius);
}
void splitNode(QuadtreeNode<T>* node) {
double x = node->bounds.x;
double y = node->bounds.y;
double width = node->bounds.width / 2;
double height = node->bounds.height / 2;
node->children[0] = new QuadtreeNode<T>(Rectangle(x, y, width, height));
node->children[1] = new QuadtreeNode<T>(Rectangle(x + width, y, width, height));
node->children[2] = new QuadtreeNode<T>(Rectangle(x, y + height, width, height));
node->children[3] = new QuadtreeNode<T>(Rectangle(x + width, y + height, width, height));
for (const T& data : node->data) {
for (int i = 0; i < 4; ++i) {
if (node->children[i]->bounds.contains(data)) {
insert(node->children[i], data);
break;
}
}
}
node->data.clear();
}
void inOrderTraversal(QuadtreeNode<T>* node) const {
if (node != nullptr) {
inOrderTraversal(node->children[0]);
for (const T& data : node->data) {
std::cout << data << " ";
}
inOrderTraversal(node->children[1]);
inOrderTraversal(node->children[2]);
inOrderTraversal(node->children[3]);
}
}
void preOrderTraversal(QuadtreeNode<T>* node) const {
if (node != nullptr) {
for (const T& data : node->data) {
std::cout << data << " ";
}
preOrderTraversal(node->children[0]);
preOrderTraversal(node->children[1]);
preOrderTraversal(node->children[2]);
preOrderTraversal(node->children[3]);
}
}
void postOrderTraversal(QuadtreeNode<T>* node) const {
if (node != nullptr) {
postOrderTraversal(node->children[0]);
postOrderTraversal(node->children[1]);
postOrderTraversal(node->children[2]);
postOrderTraversal(node->children[3]);
for (const T& data : node->data) {
std::cout << data << " ";
}
}
}
std::size_t count(QuadtreeNode<T>* node) const {
if (node == nullptr) {
return 0;
}
std::size_t count = node->data.size();
for (int i = 0; i < 4; ++i) {
count += count(node->children[i]);
}
return count;
}
double distanceBetweenPoints(const Point& a, const Point& b) const {
return std::sqrt(std::pow(a.x - b.x, 2) + std::pow(a.y - b.y, 2));
}
};
int main() {
Quadtree<std::string> quadtree(Rectangle(0, 0, 10, 10));
quadtree.insert(Point(2, 2), "A");
quadtree.insert(Point(5, 4), "B");
quadtree.insert(Point(9, 6), "C");
quadtree.insert(Point(4, 7), "D");
quadtree.insert(Point(8, 1), "E");
std::cout << "In-Order Traversal: ";
quadtree.inOrderTraversal();
std::cout << "Pre-Order Traversal: ";
quadtree.preOrderTraversal();
std::cout << "Post-Order Traversal: ";
quadtree.postOrderTraversal();
Rectangle queryRange(3, 3, 4, 4);
std::vector<std::string> result = quadtree.queryRange(queryRange);
std::cout << "Points in the range: ";
for (const std::string& point : result) {
std::cout << point << " ";
}
std::cout << std::endl;
Point queryPoint(6.0, 3.0);
std::cout << "Nearest neighbor to query point (6.0, 3.0): "
<< quadtree.nearestNeighbor(queryPoint) << std::endl;
quadtree.update(Point(5, 4), "F");
std::cout << "In-Order Traversal (After Update): ";
quadtree.inOrderTraversal();
std::cout << "Number of points in the Quadtree: " << quadtree.count() << std::endl;
std::cout << "Is it empty? " << (quadtree.isEmpty() ? "Yes" : "No") << std::endl;
quadtree.clear();
std::cout << "In-Order Traversal (After Clear): ";
quadtree.inOrderTraversal();
return 0;
}