-
Notifications
You must be signed in to change notification settings - Fork 0
/
kd_tree.py
398 lines (362 loc) · 12.8 KB
/
kd_tree.py
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
from point import point
from kd_node import kd_node
import heapq
class kd_tree:
"K DIMENSIONAL TREE"
def __init__(self, dim):
self.head = None
self.dim = dim
self.count = 0
def build(self, point_data):
"""the dim arguemnt tells which dimesnsion the split happened on previously so change it accordingly first;
on and self.dim is the total number of dimensions"""
self.count = len(point_data)
dim = 0
new_dim = (dim+1)%self.dim
L, R, T= self.split(point_data, dim)
new_node = kd_node(split_over = T, dim = dim)
new_node.L = self._buildRec(L, new_dim, new_node)
new_node.R = self._buildRec(R, new_dim, new_node)
self.head = new_node;
return
def _buildRec(self, point_data, dim, parent):
"""the dim arguemnt tells which dimesnsion the split happened on previously so change it accordingly first;
on and self.dim is the total number of dimensions"""
if point_data == None:
return None
l = len(point_data)
if l==0:
return None
if l==1:
return point_data[0]
"""above are the terminating conditions"""
new_dim = (dim+1)%self.dim
L, R, T = self.split(point_data, dim)
new_node = kd_node(split_over = T, dim = dim, parent = parent)
new_node.L = self._buildRec(L, new_dim, new_node)
new_node.R = self._buildRec(R, new_dim, new_node)
return new_node;
def split(self, point_data, dim):
avg = 0
for i in point_data:
avg += i.get(dim)
avg = avg/(len(point_data))
L = []
R = []
for i in point_data:
if i.get(dim) <= avg:
L.append(i)
else:
R.append(i)
return L, R, avg
def travel(self):
print("==================ABOUT TO TRAVEL==================")
if self.head == None:
print("NOTHING")
return
self.head.print()
self._levelorder([self.head])
return
def _levelorder(self, q):
nq = []
if(len(q)==0):
return
for i in q:
if i==None:
print("NULL")
else:
i.print()
input("")
if type(i) == type(kd_node()):
nq.append(i.L)
nq.append(i.R)
else:
continue
print("NEXT LEVEL")
return self._levelorder(nq)
def insert(self, data):
"""data is object of type point"""
if self.head == None:
self.head = kd_node(split_over = data.getx(), dim = 0)
self.head.L = data.copy()
self.count += 1
return
self._insert(self.head, data, 0, self.head)
return
def _insert(self, root, data, dim, parent):
new_dim = (dim+1)%self.dim
if root == None:
"""return the point object"""
self.count += 1
return data.copy()
if type(root) == point:
a = root.get(dim)
b = data.get(dim)
avg = (a+b)/2
t = kd_node(split_over=avg, dim = dim, parent = parent)
if a<b:
t.L = root.copy()
t.R = data.copy()
self.count += 1
elif a>b:
t.L = data.copy()
t.R = root.copy()
self.count += 1
else:
"""a==b==avg"""
t.L = root.copy()
t.L = self._insert(t.L, data, new_dim, t)
return t
if data.get(dim) <= root.split_over:
root.L = self._insert(root.L, data, new_dim, root)
else:
root.R = self._insert(root.R, data, new_dim, root)
return root
def delete(self, data):
"""data is object of type point"""
t = self._delete(root = self.head, data = data, pos = False, parent = None, dim = 0)
return t
def _delete(self, root, data, pos, dim, parent):
new_dim = (dim+1)%self.dim
if root == None:
"""point not found"""
return None
if type(root)==point:
if root == data:
"""Node to be deleted found"""
self.count -= 1
if pos:
parent.R = None
else:
parent.L = None
return root.copy()
else:
"""point object encountered but not equal to what we want hence point not found"""
return None
else:
pass
if data.get(dim) <= root.split_over:
t = self._delete(root = root.L, data = data, pos = False, dim = new_dim, parent = root)
else:
t = self._delete(root = root.R, data = data, pos = True, dim = new_dim, parent = root)
return t
def range(self, bounds):
"""bounds is a multi array"""
t = self._range(self.head, bounds, 0)
return t
def _range(self, root, bounds, dim):
new_dim = (dim+1)%self.dim
start = bounds[dim][0]
end = bounds[dim][1]
if type(root) == point:
if(root.in_range(bounds)):
# invoke copy constructor
return [root.copy()]
return []
if start<=root.split_over<end:
u = self._range(root.L, bounds, new_dim)
v = self._range(root.R, bounds, new_dim)
return u+v
elif root.split_over>=end:
u = self._range(root.L, bounds, new_dim)
return u
elif root.split_over<start:
u = self._range(root.R, bounds, new_dim)
return u
else:
raise Exception("UNEXPECTED CONDITION")
def radius(self, p, radius):
"""function to return the number of points within a hypershere of radius r"""
centre = p.coordinate # reference to array in object
l = self._radius(self.head, centre, radius, 0, p)
return l
def _radius(self, root, centre, radius, dim, p):
lst = []
new_dim = (dim+1)%self.dim
if(root == None):
return []
if type(root) == point:
"""root is point then check for its elegibilty and return suitably"""
dist = root.distance(p)
if 0 < dist <= radius:
# do not include the point p
t = root.copy()
lst.append(t)
return lst
if type(root) == kd_node:
cur_radius = root.split_over - centre[dim]
if -radius <= cur_radius <= radius:
"""can lie in both left left subtree and left right subtree"""
t1 = self._radius(root.L, centre, radius, new_dim, p)
t2 = self._radius(root.R, centre, radius, new_dim, p)
lst += t1 + t2
elif cur_radius < -radius:
"""req'd points would lie to right"""
t = self._radius(root.R, centre, radius, new_dim, p)
lst += t
elif cur_radius > radius:
"""req'd points would lie to left"""
t = self._radius(root.L, centre, radius, new_dim, p)
lst += t
else:
"""req'd points would lie to left"""
raise Exception("INVALID CONDITION WHILE DECING TO MOVE FOR HYPERSPHERE SEARCH")
return lst
def rough(self, data):
"""data is object of type point
search a point which is roughly in vicinity"""
t = self._rough(self.head, data, 0)
return t
def _rough(self, root, p, dim):
"""search a point which is roughly in vicinity"""
new_dim = (dim+1)%self.dim
if root == None:
return None
if type(root) == point:
if root == p:
return None
return root
if p.get(dim) > root.split_over:
t = self._rough(root.R,p,new_dim)
if t == None:
t = self._rough(root.L,p,new_dim)
else:
t = self._rough(root.L,p,new_dim)
if t == None:
t = self._rough(root.R,p,new_dim)
return t
def search(self,data):
if type(data) == list and len(data) == self.dim:
return self._search(self.head, point(data), 0)
elif type(data) == point and data.dim == self.dim:
return self._search(self.head, data, 0)
else:
raise Exception("INVALID DATA TYPE TO SEARCH IN KD_TREE")
return None
def _search(self, root, pointData, dim):
new_dim = (dim+1)%self.dim
if root == None:
return None
if type(root) == point:
if root == pointData:
return root.copy()
return None
elif type(root) == kd_node:
if pointData.get(dim) <= root.split_over:
return self._search(root.L, pointData, new_dim)
else:
return self._search(root.R, pointData, new_dim)
else:
return None
def nearest_nbr(self, data):
"""data is object of type point"""
t = rough(data)
arr = self.radius(t, data.distance(t))
for i in range(len(arr)):
arr[i] = (data.distance(arr[i]), arr[i])
heapq.heapify(arr)
t = heapq.nsmallest(1,arr)
return t[0][1]
def k_nearest_nbr_distance(self, p, distance, k):
"""list of points in a cluster within a range=distance and maximum k points"""
t = self.radius(p, distance)
for i in range(len(t)):
t[i] = (p.distance(t[i]), t[i])
heapq.heapify(t)
t = heapq.nsmallest(k,t)
for i in range(len(t)):
t[i] = t[i][1]
return t
def _copy(self, root, parent):
if root == None:
return None
if type(root) == kd_node:
t = root.copy()
t.P = parent
t.L = self._copy(root.L, t)
t.R = self._copy(root.R, t)
return t
if type(root) == point:
return root.copy()
raise Exception("INVALID DATA TYPE WHILE COPYING KD TREE")
def copy(self):
if self.head == None:
return None
tree = kd_tree(self.dim)
tree.head = self.head.copy()
tree.head.P = None
tree.head.L = self._copy(self.head.L, tree.head)
tree.head.R = self._copy(self.head.R, tree.head)
tree.count = self.count
return tree
def get_farthest(self, data):
'''returns the farthest point from the given point'''
m = [(0,None)]
self._get_farthest(self.head, data,m)
return m[0][1]
def _get_farthest(self, root, data, m):
"""brute force for farthest point"""
if root == None:
return None
if type(root) == point:
t = root.distance(data)
if t > m[0][0]:
m[0] = (t,root)
return
if type(root) == kd_node:
self._get_farthest(root.L, data, m)
self._get_farthest(root.R, data, m)
return
raise Exception("INAVLID DATA TYPE WHILING GETTING FARTHEST POINT")
def smallestPoint(self):
t = self._smallestPoint(self.head)
return t
def _smallestPoint(self, root):
if root == None:
return None
if type(root) == point:
return root.copy()
l = self._smallestPoint(root.L)
if type(l) == point:
return l
r = self._smallestPoint(root.R)
return r
# driver code
'''obj = kd_tree(2)
arr = []
arr.append(point(name=1, dim=2, coordinate=[1,1]))
arr.append(point(name=2, dim=2, coordinate=[1,2]))
arr.append(point(name=5, dim=2, coordinate=[2,1]))
arr.append(point(name=6, dim=2, coordinate=[2,2]))
arr.append(point(name=9, dim=2, coordinate=[3,1]))
arr.append(point(name=10, dim=2, coordinate=[3,2]))
arr.append(point(name=11, dim=2, coordinate=[3,3]))
arr.append(point(name=12, dim=2, coordinate=[3,4]))
arr.append(point(name=7, dim=2, coordinate=[2,3]))
arr.append(point(name=8, dim=2, coordinate=[2,4]))
arr.append(point(name=4, dim=2, coordinate=[1,4]))
arr.append(point(name=3, dim=2, coordinate=[1,3]))
arr.append(point(name=13, dim=2, coordinate=[4,1]))
arr.append(point(name=14, dim=2, coordinate=[4,2]))
arr.append(point(name=15, dim=2, coordinate=[4,3]))
arr.append(point(name=16, dim=2, coordinate=[4,4]))
obj.build(arr)
#obj.travel()
#bounds = [[2,2],[3.2,3.7]]
#t = obj.k_nearest_nbr_distance(arr[3],1,2)
print("=======")
for i in t:
i.print()'''
'''arr = []
cid = 0
a = 4
for i in range(1):
for j in range(1):
for k in range(a):
arr.append(point([i,j,k],name=cid))
cid+=1
obj = kd_tree(3)
obj.build(arr)
print("DONE")
obj.travel()
print("COUNT->",obj.count)'''