Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归过程中的奇怪错误_Java_Recursion_Linked List_Tree - Fatal编程技术网

Java 递归过程中的奇怪错误

Java 递归过程中的奇怪错误,java,recursion,linked-list,tree,Java,Recursion,Linked List,Tree,我有下面的代码,它构建了一个二叉树,其中所有节点都是0或1,因此从根到叶的每个路径都是一个特定长度的二进制字符串。最初,我的代码只是打印所有路径(路径是整数列表,即[0,0,0,1,0,1])。现在,我试图实际保存列表列表中的所有路径,但得到了意外的输出。以下是相关代码: public class Tree{ Node root; int levels; LinkedList<LinkedList<Integer>> all; Tree(int v){

我有下面的代码,它构建了一个二叉树,其中所有节点都是0或1,因此从根到叶的每个路径都是一个特定长度的二进制字符串。最初,我的代码只是打印所有路径(路径是整数列表,即[0,0,0,1,0,1])。现在,我试图实际保存列表列表中的所有路径,但得到了意外的输出。以下是相关代码:

public class Tree{
  Node root;
  int levels;
  LinkedList<LinkedList<Integer>> all;

  Tree(int v){
    root = new Node(v);
    levels = 1;
    all = new LinkedList<LinkedList<Integer>>();
  }

  public static void main(String[] args){
    Tree tree = new Tree(0);
    populate(tree, tree.root, tree.levels);
    tree.printPaths(tree.root);  // this is the part that prints the paths one by one
    for (LinkedList<Integer> l: tree.all){ // this is when i later tried to save all paths to the
      System.out.println(l);               // list all and then print them out from that list
    }
  }

  
   public void printPaths(Node node)
   {
       LinkedList<Integer> path = new LinkedList<Integer>();
       printPathsRecur(node, path, 0);
   }

  void printPathsRecur(Node node, LinkedList<Integer> path, int pathLen)
    {
        if (node == null)
            return;

        // append this node to the path array
        path.add(node.value);
        path.set(pathLen, node.value);
        pathLen++;

        // it's a leaf, so print the path that led to here
        if (node.left == null && node.right == null){
            printArray(path, pathLen); // Initial version which prints the paths one by one - WORKS FINE
            all.add(path);  // This is when I try to actually keep the paths in a list - doesn't work
        }
        else
        {
            printPathsRecur(node.left, path, pathLen);
            printPathsRecur(node.right, path, pathLen);
        }
    }
...}

但是,当我尝试将路径保存到列表中,并打印该列表的每个元素时,我得到以下结果:

[0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1]
[0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1]
...

看起来列表只是一次又一次地保存同一个超长条目。

我无法运行您发布的代码,因为它不完整。下一次发布一个和一些显示“奇怪”行为的特定测试数据可能是个好主意

但是从我所看到的情况来看,我猜想问题在于您在
printpathsrefur
方法中传递
LinkedList path
参数的方式


您正在
printPaths()
方法中创建路径链表。然后将对它的引用传递给
printPathsRecur()
方法。它修改列表,然后递归运行两次,将与您在
printPaths()
方法中创建的原始路径链表的引用传递回原处。这意味着在任何时候,
printpathsrefur()
方法的所有递归调用实际上都在使用它刚刚添加到的同一个列表,在所有2D链接列表中创建一个长列表。只是对同一个链表的多个引用。

看起来您将所有内容都写在同一个列表中。了解Java中的深度复制。我建议使用debugger,它是一个非常方便的工具,可以帮助您更好地理解代码的工作方式,以及它在哪里做了一些不符合您计划的事情。
[0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1]
[0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1]
...