-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathQuadTree.cs
More file actions
282 lines (238 loc) · 7.72 KB
/
QuadTree.cs
File metadata and controls
282 lines (238 loc) · 7.72 KB
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
using Advanced.Algorithms.Geometry;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Advanced.Algorithms.DataStructures
{
/// <summary>
/// A quadtree implementation.
/// </summary>
public class QuadTree<T> : IEnumerable<Tuple<Point, T>>
{
//used to decide when the tree should be reconstructed
private int deletionCount = 0;
private QuadTreeNode<T> root;
public int Count { get; private set; }
/// <summary>
/// Time complexity: O(n).
/// </summary>
/// <param name="point">The co-ordinate.</param>
/// <param name="value">The value associated with this co-ordinate if any.</param>
public void Insert(Point point, T value = default(T))
{
root = insert(root, point, value);
Count++;
}
private QuadTreeNode<T> insert(QuadTreeNode<T> current, Point point, T value)
{
if (current == null)
{
return new QuadTreeNode<T>(point, value);
}
//south-west
if (point.X < current.Point.X && point.Y < current.Point.Y)
{
current.SW = insert(current.SW, point, value);
}
//north-west
else if (point.X < current.Point.X && point.Y >= current.Point.Y)
{
current.NW = insert(current.NW, point, value);
}
//north-east
else if (point.X > current.Point.X && point.Y >= current.Point.Y)
{
current.NE = insert(current.NE, point, value);
}
//south-east
else if (point.X > current.Point.X && point.Y < current.Point.Y)
{
current.SE = insert(current.SE, point, value);
}
return current;
}
/// <summary>
/// Time complexity: O(n).
/// </summary>
public List<Tuple<Point, T>> RangeSearch(Rectangle searchWindow)
{
return rangeSearch(root, searchWindow, new List<Tuple<Point, T>>());
}
private List<Tuple<Point, T>> rangeSearch(QuadTreeNode<T> current, Rectangle searchWindow, List<Tuple<Point, T>> result)
{
if (current == null)
{
return result;
}
//is inside the search rectangle
if (current.Point.X >= searchWindow.LeftTop.X
&& current.Point.X <= searchWindow.RightBottom.X
&& current.Point.Y <= searchWindow.LeftTop.Y
&& current.Point.Y >= searchWindow.RightBottom.Y
&& !current.IsDeleted)
{
result.Add(new Tuple<Point, T>(current.Point, current.Value));
}
//south-west
if (searchWindow.LeftTop.X < current.Point.X && searchWindow.RightBottom.Y < current.Point.Y)
{
rangeSearch(current.SW, searchWindow, result);
}
//north-west
if (searchWindow.LeftTop.X < current.Point.X && searchWindow.LeftTop.Y >= current.Point.Y)
{
rangeSearch(current.NW, searchWindow, result);
}
//north-east
if (searchWindow.RightBottom.X > current.Point.X && searchWindow.LeftTop.Y >= current.Point.Y)
{
rangeSearch(current.NE, searchWindow, result);
}
//south-east
if (searchWindow.RightBottom.X > current.Point.X && searchWindow.RightBottom.Y < current.Point.Y)
{
rangeSearch(current.SE, searchWindow, result);
}
return result;
}
/// <summary>
/// Time complexity: O(n).
/// </summary>
public void Delete(Point p)
{
var point = find(root, p);
if (point == null || point.IsDeleted)
{
throw new Exception("Point not found.");
}
point.IsDeleted = true;
Count--;
if (deletionCount == Count)
{
reconstruct();
deletionCount = 0;
}
else
{
deletionCount++;
}
}
private void reconstruct()
{
QuadTreeNode<T> newRoot = null;
foreach(var exisiting in this)
{
newRoot = insert(newRoot, exisiting.Item1, exisiting.Item2);
}
root = newRoot;
}
private QuadTreeNode<T> find(QuadTreeNode<T> current, Point point)
{
if (current == null)
{
return null;
}
if (current.Point.X == point.X && current.Point.Y == point.Y)
{
return current;
}
//south-west
if (point.X < current.Point.X && point.Y < current.Point.Y)
{
return find(current.SW, point);
}
//north-west
else if (point.X < current.Point.X && point.Y >= current.Point.Y)
{
return find(current.NW, point);
}
//north-east
else if (point.X > current.Point.X && point.Y >= current.Point.Y)
{
return find(current.NE, point);
}
//south-east
else if (point.X > current.Point.X && point.Y < current.Point.Y)
{
return find(current.SE, point);
}
return null;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<Tuple<Point, T>> GetEnumerator()
{
return new QuadTreeEnumerator<T>(root);
}
}
internal class QuadTreeNode<T>
{
//co-ordinate
internal Point Point;
//quadrants
internal QuadTreeNode<T> NW, NE, SE, SW;
//actual data if any associated with this point
internal T Value;
//marked as deleted
internal bool IsDeleted;
internal QuadTreeNode(Point point, T value)
{
Point = point;
Value = value;
}
}
internal class QuadTreeEnumerator<T> : IEnumerator<Tuple<Point, T>>
{
private readonly QuadTreeNode<T> root;
private QuadTreeNode<T> current;
private Stack<QuadTreeNode<T>> progress;
internal QuadTreeEnumerator(QuadTreeNode<T> root)
{
this.root = root;
}
public bool MoveNext()
{
if (root == null)
{
return false;
}
if (progress == null)
{
progress = new Stack<QuadTreeNode<T>>(new[] { root.NE, root.NW, root.SE, root.SW }.Where(x => x != null));
current = root;
return true;
}
if (progress.Count > 0)
{
var next = progress.Pop();
current = next;
foreach (var child in new[] { next.NE, next.NW, next.SE, next.SW }.Where(x => x != null))
{
progress.Push(child);
}
return true;
}
return false;
}
public void Reset()
{
progress = null;
current = null;
}
object IEnumerator.Current => Current;
public Tuple<Point, T> Current
{
get
{
return new Tuple<Point, T>(current.Point, current.Value);
}
}
public void Dispose()
{
progress = null;
}
}
}