-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknn.py
40 lines (32 loc) · 1.01 KB
/
knn.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
from node import Node
from heap import Heap
from collections import namedtuple
QObj = namedtuple('QObj', ('dist', 'node', 'leaf'))
def KNN(tree, query, limit, score, predicate):
node = tree.data
result = []
queue = Heap()
stop = False
while node and (not stop):
for child in node.children:
if isinstance(child, Node):
dist = score(query, child.bbox)
else:
dist = score(query, child)
o = QObj(dist, child, node.leaf)
queue.push(o)
while (not queue.is_empty()) and queue.peek().leaf:
candidate = queue.pop()
pred, stop = predicate(candidate)
if pred:
result.append(candidate.node)
if stop:
break
if limit != 0 and len(result) == limit:
return result
if not stop:
if queue.is_empty():
node = None
else:
node = queue.pop().node
return result