简体   繁体   English

KdTree最近邻居搜索算法无法正常工作

[英]KdTree nearest neighbour search algorithm not working properly

I'm implementing a KdTree in java. 我在Java中实现KdTree。 I have most of the rest of the program done, but I can't seem to get my nearest neighbour search algorithm to work properly. 我已经完成了程序的其余大部分,但似乎无法让我最近的邻居搜索算法正常​​工作。 It always returns the root node's values, no matter what. 无论如何,它总是返回根节点的值。 Here is my code: 这是我的代码:

public Point2D nearest(Point2D p) {

    if (root == null) //if there are no nodes in the tree
        return null;
    else
        return nearest(p, root, root.point);            

}
  • rect is a RectHV object that corresponds to the bounds of the node's point. rect是一个RectHV对象,它与节点点的边界相对应。

     public Point2D nearest(Point2D p, Node n, Point2D nearest) { if (n == null) { return nearest; } if (p.distanceSquaredTo(nearest) > p.distanceSquaredTo(n.point)) { nearest = n.point; } if (n.xAxis) { //the xaxis value is a boolean, if it is true, //the node splits on the x axis, false it splits on y if (px() < n.point.x() && p.distanceSquaredTo(nearest) < n.rect.distanceTo(p)) { nearest = nearest(p, n.leftnode, nearest); System.out.println("look left 1"); } else { nearest = nearest(p, n.rightnode, nearest); System.out.println("look right 1"); } } else { if (py() < n.point.y() && p.distanceSquaredTo(nearest) < n.rect.distanceTo(p)) { nearest = nearest(p, n.leftnode, nearest); System.out.println("look left 2"); } else { nearest = nearest(p, n.rightnode, nearest); System.out.println("look right 2"); } } return nearest; 

    } }

I'm thinking that my algorithm is too simple for the task. 我认为我的算法对于这项任务来说太简单了。 My rationale is that if the distanceSquared between the query point and a candidate point's rectangle is greater than the already established nearest point, then don't search down that tree. 我的理由是,如果查询点和候选点的矩形之间的distanceSquared大于已经建立的最近点,则不要向下搜索该树。

The problems are (1) that the distance from the query point to the point defining a subtree is not a lower bound on the distance to all points in that subtree and (2) that the nearest point may lie in the "other" child. 问题是(1)从查询点到定义子树的点的距离不是到该子树中所有点的距离的下限,以及(2)最近的点可能位于“另一个”子级中。

To get a lower bound, you can track the bounding box of the points in the subtree on descent, and use the distance of the query point to the box. 要获得下限,您可以在下降时跟踪子树中点的边界框,并使用查询点到该框的距离。 More simplistically, you can use the distance from the point to the half-plane defined by the most recent split. 更简单地说,您可以使用从点到最近分割所定义的半平面的距离。 You need to explore both children (unless pruned). 您需要探索两个孩子(除非修剪)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM