我已经实现了一种批量加载点四叉树的方法。但是对于某些输入,它无法正确地工作,例如如果有许多具有相同x或y坐标的点。一个例子数据集如下:
test = [(3, 1), (16, 1), (11, 4), (5, 4), (9, 6), (5, 10),
(1, 15), (11, 5), (11, 15), (12, 16), (19, 17)]
tree = create(test)
问题出现在以下点:
(11,4),(11,5),(11,15)
和(5,10),(5,4)
。这是
create
函数:def create(point_list, presorted=False):
if not point_list:
return QuadNode()
if not presorted:
point_list.sort(key=lambda p: [p[0],p[1]])
median = len(point_list) >> 1
relevantPoint = point_list[median]
relevantYCoordinate = relevantPoint[1]
node = QuadNode(data=relevantPoint)
leftBins = point_list[:median]
rightBins = point_list[median + 1:]
nwBins = [bin for bin in leftBins if bin[1] >= relevantYCoordinate]
swBins = [bin for bin in leftBins if bin[1] < relevantYCoordinate]
neBins = [bin for bin in rightBins if bin[1] >= relevantYCoordinate]
seBins = [bin for bin in rightBins if bin[1] < relevantYCoordinate]
node.nwNode = create(nwBins, presorted=True)
node.swNode = create(swBins, presorted=True)
node.neNode = create(neBins, presorted=True)
node.seNode = create(seBins, presorted=True)
return node
以及 QuadNode
:
class QuadNode(object):
def __init__(self, data=None, nwNode=None, neNode=None, swNode=None, seNode=None):
self.data = data
self.nwNode = nwNode
self.neNode = neNode
self.swNode = swNode
self.seNode = seNode
我想遵循插入、删除等规则:
swNode
:当point.x < parent.x
且point.y < parent.y
时seNode
:当point.x >= parent.x
且point.y < parent.y
时nwNode
:当point.x < parent.x
且point.y >= parent.y
时neNode
:当point.x >= parent.x
且point.y >= parent.y
时
[(1, 15), (3, 1), (5, 4), (5, 10), (9, 6), (11, 4), (11, 5), (11, 15), (12, 16), (16, 1), (19, 17)]
。你希望输出什么?我有点忘记四叉树是如何工作的了... - gsamaras