【发布时间】:2012-08-10 18:23:30
【问题描述】:
我正在尝试自学数据结构,并且我正在用 Python 实现一个 k-d 树。我有一种方法可以在我的 k-d 树类中某个点的某个半径内搜索树中的点:
def within_radius(self, point, radius, result=[]):
"""
Find all items in the tree within radius of point
"""
d = self.discriminator
if in_circle(point, radius, self.data):
result.append(self.data)
# Check whether any of the points in the subtrees could be
# within the circle
if point[d] - radius < self.data[d] and self.l_child:
result.append(self.l_child.within_radius(point, radius, result))
if point[d] + radius > self.data[d] and self.r_child:
result.append(self.r_child.within_radius(point, radius, result))
return result
它可以工作,但它返回的列表非常时髦,带有来自result 的递归调用的重复值。将树递归返回的值“累积”到列表中的好方法是什么?我已经考虑了一段时间,但我真的不知道怎么做。
【问题讨论】:
-
您可以使用本地结果而不是不断传递的结果,但仍将本地值传回...