Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/384.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_Tree Traversal - Fatal编程技术网

Java 树遍历,读取代码

Java 树遍历,读取代码,java,tree-traversal,Java,Tree Traversal,我正在查找树遍历,到目前为止,我对它的理解没有问题,意思是预排序、顺序、后排序。简单的代码如下: sub P(TreeNode) Output(TreeNode.value) If LeftPointer(TreeNode) != NULL Then P(TreeNode.LeftNode) If RightPointer(TreeNode) != NULL Then P(TreeNode.RightNode) end sub 我发现了一个更长的代码,我很困惑,输出是什么 publ

我正在查找树遍历,到目前为止,我对它的理解没有问题,意思是预排序、顺序、后排序。简单的代码如下:

sub P(TreeNode)
Output(TreeNode.value)
If LeftPointer(TreeNode) != NULL Then
   P(TreeNode.LeftNode)
If RightPointer(TreeNode) != NULL Then
   P(TreeNode.RightNode)
end sub
我发现了一个更长的代码,我很困惑,输出是什么

public class My {
 public static void print(Node n){
    if(n != null) {
       System.out.print(n.info +"");
       print(n.left);
       print(n.right);
    }
} 
 public static void print2(Node n){
    if(n != null) {           
       print2(n.left);
       System.out.print(n.info +"");
       print2(n.right);
    }
 }

 public static void print3(Node n){
    if(n != null) {           
       print3(n.left);
       print3(n.right);
       System.out.print(n.info +"");           
    }
}
public static void main(String[] args) {
Tree t = new Tree();
   t.createTree();
   print(t.root);
   System.out.println();
   print2(t.root);
   System.out.println();
   print3(t.root);
}
} 
所以我更容易理解,假设输入是10,-10,12,8,21,34
输出将是什么样子?因为如果我理解正确的话,那么就有3个(预订单、顺序、后订单)

这棵树看起来像:

     10
-10     12
8  21  34
原来这是我的一所学校的考试,他们必须在试卷上给出答案,这是他们得到的所有信息。没有人告诉他们这棵树会是什么样子

简单的代码如下:

sub P(TreeNode)
Output(TreeNode.value)
If LeftPointer(TreeNode) != NULL Then
   P(TreeNode.LeftNode)
If RightPointer(TreeNode) != NULL Then
   P(TreeNode.RightNode)
end sub
有预订

发现一个更长的代码,我感到困惑

它更长,因为你有所有的遍历

输入是10,-10,12,8,21,34输出是什么样子的


如果不知道实际的树是什么样子,没有人能回答这个问题(数字列表可以是任何可能的遍历的输出)。获得这些信息后,请自己运行代码(最好是在调试器中执行),然后查看它是什么

我们看不到您的树或节点类,因此无法打印任何内容。。。您应该在调试器中运行代码though@cricket_007是的,brainfart时刻,Sorry在论文中添加了树的外观,以及我在这个例子中发现的一些附加信息。如果你不理解这种格式是如何得到的,你似乎对树是如何构建而不是遍历的感到困惑是的,最近才开始关注树遍历。