示例#1
0
    def __init__(self, points):
        if not points:
            raise Exception('points must NOT be empty')
        points = [p for p in points]
        random.shuffle(points)

        super(StaticKDTree, self).__init__(len(points[0]))
        self._size = len(points)
        self._rect = HyperRect.point(points[0])
        for p in points:
            self._rect.enlarge_to(p)
        self.root = self._build(points, 0, 0, len(points)-1)
示例#2
0
    def delete(self, point):

        ''' delete a point from the kdtree '''

        node, _disc = self.root, 0
        while node:
            if node.point == point:
                break
            node = node.children[self._successor(node, point, _disc)]
            _disc = (_disc + 1) % self.dim
        if node:
            if node is self.root:
                self.root = self._delete(node, _disc)
            else:
                self._delete(node, _disc)
            self._size -= 1
            if self._size == 1:
                self._rect = HyperRect.point(point)
            elif self._size == 0:
                self._rect = None
        return node
示例#3
0
    def insert(self, point):

        ''' insert a point into kdtree '''

        new_node = KDNode(point)
        node, _disc = self.root, 0
        if not node:
            self.root = new_node
            self._rect = HyperRect.point(point)
        else:
            while True:
                if node.point == point:
                    return node
                son = self._successor(node, point, _disc)
                child = node.children[son]
                if not child:
                    break
                node = child
                _disc = (_disc + 1) % self.dim
            node.children[son] = new_node
            new_node.parent = node
            self._rect.enlarge_to(point)
        self._size += 1
        return None