-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathQuadTree.cs
224 lines (180 loc) · 6.21 KB
/
QuadTree.cs
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Advanced.Algorithms.Geometry;
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;
private QuadTreeNode<T> root;
public int Count { get; private set; }
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<Tuple<Point, T>> GetEnumerator()
{
return new QuadTreeEnumerator<T>(root);
}
/// <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)
{
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
if (point.X < current.Point.X && point.Y >= current.Point.Y)
return Find(current.Nw, point);
//north-east
if (point.X > current.Point.X && point.Y >= current.Point.Y)
return Find(current.Ne, point);
//south-east
if (point.X > current.Point.X && point.Y < current.Point.Y) return Find(current.Se, point);
return null;
}
}
internal class QuadTreeNode<T>
{
//marked as deleted
internal bool IsDeleted;
//quadrants
internal QuadTreeNode<T> Nw, Ne, Se, Sw;
//co-ordinate
internal Point Point;
//actual data if any associated with this point
internal T Value;
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 => new(current.Point, current.Value);
public void Dispose()
{
progress = null;
}
}