在对K-D Tree 类的编写的时候定义the nearest neighbor query时发生错误
问题显示“缩进”出现问题
尝试调整缩进但似乎出现了更多的问题
def nearest_neighbor(self, point: Point) -> Point:
closest = None
depth = 0
return self._nearest_neighbor(point, self._root, closest, depth)
def _nearest_neighbor(self, point, node, closest, depth):
if node is None:
return closest
if closest is None or self.distance(point, node.location) < self.distance(point, closest):
closest = node.location
axis = depth % 2
if point[axis] < node.location[axis]:
closest = self._nearest_neighbor(point, node.left, closest, depth+1)
else:
closest = self._nearest_neighbor(point, node.right, closest, depth+1)
return closest
def distance(self, p1, p2):
return ((p1.x - p2.x) ** 2 + (p1.y - p2.y) ** 2) ** 0.5
def range_test():
points = [Point(7, 2), Point(5, 4), Point(9, 6), Point(4, 7), Point(8, 1), Point(2, 3)]
kd = KDTree()
kd.insert(points)
result = kd.range(Rectangle(Point(0, 0), Point(6, 6)))
assert sorted(result) == sorted([Point(2, 3), Point(5, 4)])