Java中的基本递归算法

Java中的基本递归算法,java,algorithm,recursion,arraylist,Java,Algorithm,Recursion,Arraylist,我试图用Java实现一个基本的RRT(快速探索随机树)算法。我有一个类TreeNode,它保存节点的x和y位置,并在ArrayList中保存它拥有的所有子节点。在我的主程序中,我有树的根。如果我想在树中找到最近的邻居,我调用这个代码。调用该方法时,根节点为“node”,minAbstand为Integer.MAX_值 private void NEAREST_NEIGHBOUR(int xrand, int yrand, TreeNode node, int minAbstand) {

我试图用Java实现一个基本的RRT(快速探索随机树)算法。我有一个类TreeNode,它保存节点的x和y位置,并在ArrayList中保存它拥有的所有子节点。在我的主程序中,我有树的根。如果我想在树中找到最近的邻居,我调用这个代码。调用该方法时,根节点为“node”,minAbstand为Integer.MAX_值

private void NEAREST_NEIGHBOUR(int xrand, int yrand, TreeNode node, int minAbstand) {
    if((Math.sqrt(Math.pow(node.getxPos()-xrand, 2) + Math.pow(node.getyPos()-yrand, 2))) <= minAbstand){//normal method to find the distance
        minAbstand = (int) Math.sqrt(Math.pow(node.getxPos()-xrand, 2) + Math.pow(node.getyPos()-yrand, 2));
        xnearest = node;
    }
    for(int i = 0; i < node.getNodes().size(); i++){
        NEAREST_NEIGHBOUR(xrand, yrand, node.getNodes().get(i), minAbstand);
    }
}
和TreeNode类:

TreeNode(int x, int y){
    xPos = x;
    yPos = y;
    nodes = new ArrayList<TreeNode>();
}

public ArrayList<TreeNode> getNodes() {
    return nodes;
}

public int getxPos() {
    return xPos;
}

public int getyPos() {
    return yPos;
}

public void addNode(TreeNode node){
    nodes.add(node);
}
TreeNode(intx,inty){
xPos=x;
yPos=y;
节点=新的ArrayList();
}
公共ArrayList getNodes(){
返回节点;
}
public int getxPos(){
返回XPO;
}
公共int getyPos(){
返回YPO;
}
公共void addNode(TreeNode节点){
nodes.add(node);
}
解决方案?

这是现在吗?递归真的很棘手

private int NEAREST_NEIGHBOUR(int xrand, int yrand, TreeNode node, int minAbstand) {
  if((Math.sqrt(Math.pow(node.getxPos()-xrand, 2) + Math.pow(node.getyPos()-yrand, 2))) < minAbstand){
      minAbstand = (int) Math.sqrt(Math.pow(node.getxPos()-xrand, 2) + Math.pow(node.getyPos()-yrand, 2));
      xnearest = node;
  }
  for(int i = 0; i < node.getNodes().size(); i++){
      minAbstand = NEAREST_NEIGHBOUR(xrand, yrand, node.getNodes().get(i), minAbstand);
  }
  return minAbstand;
private int近邻(int-xrand、int-yrand、TreeNode节点、int-minAbstand){
if((Math.sqrt(Math.pow(node.getxPos()-xrand,2)+Math.pow(node.getyPos()-yrand,2))

}

我必须纠正自己:
最近邻
方法有一个失败的逻辑。看:

当该方法启动时,将接收的
minAbstand
与到当前评估节点的距离进行比较。并且,如果它较低,则更新
minAbstand
值,以便递归地将搜索标准限制在当前子树中。到目前为止,一切顺利

但是,兄弟子树呢?我的意思是:当当前子树被完全浏览时,当前方法调用结束,因此,将控件返回给调用方法,调用方法本身在上一个递归框架中但是局部变量的值会丢失(如
minAbstand


您需要返回
minAbstand
作为
最近邻
的返回值,并在每次递归调用时更新它。

有一点不清楚:在找到比输入节点最近的节点后,您实际在做什么?你说你把它添加到一个列表中,但是在代码中你写了
xnearest
变量。你发布的图是树还是dag(直接非循环图)?如果它实际上是一棵树,突出显示的区域覆盖了两个不同的分支,我无法清楚地看到哪一个是输入节点,哪一个是在您的实验中假设为最近的。我在
最近邻
中没有看到失败,但是
NEW\u STATE
对我来说是黑暗的:一旦你已经拥有了
xnearest
xrand
,为什么要创建一个新节点?我不知道你为什么认为你应该这样诋毁你的问题,但不,你不应该。我回滚了编辑,所以发布的答案不再毫无意义。是的!这正是我的意思。最后一个建议:你最好提高代码的可读性,避免长表达式(特别是重复的表达式),比如计算距离:只在一个私有方法中编写计算代码,并在需要时调用它。
private int NEAREST_NEIGHBOUR(int xrand, int yrand, TreeNode node, int minAbstand) {
  if((Math.sqrt(Math.pow(node.getxPos()-xrand, 2) + Math.pow(node.getyPos()-yrand, 2))) < minAbstand){
      minAbstand = (int) Math.sqrt(Math.pow(node.getxPos()-xrand, 2) + Math.pow(node.getyPos()-yrand, 2));
      xnearest = node;
  }
  for(int i = 0; i < node.getNodes().size(); i++){
      minAbstand = NEAREST_NEIGHBOUR(xrand, yrand, node.getNodes().get(i), minAbstand);
  }
  return minAbstand;