Ios KD树搜索速度慢

Ios KD树搜索速度慢,ios,nearest-neighbor,kdtree,recursive-datastructures,Ios,Nearest Neighbor,Kdtree,Recursive Datastructures,我正在实现一个KD树,将地图上的点分组。我一直在用它作为参考。搜索返回正确的最近邻点,但速度比我预期的要慢。这是我的密码: - (FDRKDTree *)nnSearchForPoint:(id <MKAnnotation>)point best:(FDRKDTree *)best { // consider the current node distToPoint = [self distanceBetweenAnnotations:self.location and:point]

我正在实现一个KD树,将地图上的点分组。我一直在用它作为参考。搜索返回正确的最近邻点,但速度比我预期的要慢。这是我的密码:

- (FDRKDTree *)nnSearchForPoint:(id <MKAnnotation>)point best:(FDRKDTree *)best {
// consider the current node
distToPoint = [self distanceBetweenAnnotations:self.location and:point];
if (distToPoint < best.distToPoint) {
    best = self;
}
// search near branch
int axis = depth % 2;
FDRKDTree *child = nil;
if (axis) {
    if (point.coordinate.latitude > location.coordinate.latitude)
        child = rightChild;
    else
        child = leftChild;
} else {
    if (point.coordinate.longitude > location.coordinate.longitude)
        child = rightChild;
    else
        child = leftChild;
}
if (child != nil)
    best = [child nnSearchForPoint:point best:best];

child = nil;
//search the away branch - maybe
if (axis) {
    if (fabs(point.coordinate.latitude - self.location.coordinate.latitude) <
        best.distToPoint) {
        if (point.coordinate.latitude > location.coordinate.latitude)
            child = leftChild;
        else
            child = rightChild;
    }
} else {
    if (fabs(point.coordinate.longitude - self.location.coordinate.longitude) <
        best.distToPoint) {
        if (point.coordinate.longitude > location.coordinate.longitude)
            child = leftChild;
        else
            child = rightChild;
    } 
}


if (child != nil) {
    best = [child nnSearchForPoint:point best:best];
}

return best;
}
-(FDRKDTree*)nnSearchForPoint:(id)point best:(FDRKDTree*)best{
/考虑当前节点
distToPoint=[符号之间的自距离:self.location和:point];
if(distToPoint位置坐标纬度)
child=右child;
其他的
child=leftChild;
}否则{
if(点坐标经度>位置坐标经度)
child=右child;
其他的
child=leftChild;
}
if(child!=nil)
最佳=[child nnSearchForPoint:point best:best];
儿童=零;
//搜索远处的树枝——也许吧
如果(轴){
if(fabs(点坐标纬度-自身位置坐标纬度)<
最佳距离点){
if(点坐标纬度>位置坐标纬度)
child=leftChild;
其他的
child=右child;
}
}否则{
if(fabs(point.coordinate.longitude-self.location.coordinate.longitude)<
最佳距离点){
if(点坐标经度>位置坐标经度)
child=leftChild;
其他的
child=右child;
} 
}
if(child!=nil){
最佳=[child nnSearchForPoint:point best:best];
}
回报最好;
}
我的问题是,我对“的解释是否正确,这是一个简单的比较,以查看搜索点和当前节点的分割坐标之间的差异是否小于从搜索点到当前最佳点的距离(总体坐标)。”。我的解释是:

if(fabs(point.coordinate.latitude-self.location.coordinate.latitude)<
最佳。distToPoint)

if(fabs(point.coordinate.longitude-self.location.coordinate.longitude)<
最佳。distToPoint)

分别。任何其他建议也欢迎


谢谢。

假设您的
distToPoint
sqrt((x1-x0)**2+(y1-y0)**2),我觉得您做的很好。我用Python实现了该算法,这可能有助于交叉检查您的版本,并澄清Wikipedia文章中的一些要点: