-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkdnode.py
39 lines (33 loc) · 1.33 KB
/
kdnode.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
from plancomparator import *
class kdNode:
def __init__(self, v, comp=None):
self.value=v
self.comparator=comp
self.isLeaf= True
def addElt(self, elt):
if self.isLeaf:
moy = elt.mean(self.value)
self.comparator = PlanComparator(moy, elt)
left = kdNode(self.value)
right = kdNode(elt)
self.value=moy
self.setChildren(left, right)
else:
#~ print "inserting in node: ",self.getChildren(elt).value.values
self.getChildren(elt).addElt(elt)
def getNearest(self, elt):
if self.isLeaf:
return (self.value, self.value.dist(elt))
else:
(val, cost) = self.getChildren(elt).getNearest(elt)
if self.comparator.distToPlan(elt) < cost:
nexttree = self.children[1] if self.getChildren(elt)==self.children[0] else self.children[0]
(val2, cost2) = nexttree.getNearest(elt)
if cost2 <cost:
(val,cost) = (val2, cost2)
return (val, cost)
def getChildren(self, elt):
return self.children[1] if self.comparator.isAfter(elt) else self.children[0]
def setChildren(self, left, right):
self.children= (left, right)
self.isLeaf = False