java로 KdTree를 구현하고 있습니다. 대부분의 프로그램을 끝내지 만, 가장 가까운 이웃 검색 알고리즘을 제대로 작동시키지 못합니다. 항상 루트 노드의 값을 반환합니다. 여기 내 코드입니다 :KdTree 가장 근접한 검색 알고리즘이 제대로 작동하지 않습니다.
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 노드의 지점의 경계에 해당하는 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 (p.x() < 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 (p.y() < 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;
}
내 알고리즘이 작업에 대해 너무 간단하다는 생각 해요. 내 근거는 쿼리 포인트와 후보 포인트의 사각형 사이의 distanceSquared가 이미 설정된 가장 가까운 포인트보다 큰 경우 해당 트리를 검색하지 않는 것입니다.