Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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_Algorithm_Tree_Binary Tree - Fatal编程技术网

Java 这个程序是如何进行预顺序遍历的?

Java 这个程序是如何进行预顺序遍历的?,java,algorithm,tree,binary-tree,Java,Algorithm,Tree,Binary Tree,此函数返回以节点为根的二叉树中的节点数。有几篇文章说这是一个预顺序遍历,但对我来说这看起来像是一个后顺序遍历,因为我们在访问根之前访问了左部分和右部分。我错了吗?还是我的“访问”概念有错?是的。你对访问的看法是错误的!“此处已访问”表示您处于当前节点,然后尝试遍历树。首先在“根”处进行计数,然后是您的计数权限,然后是左侧,因此是的,它是预先排序的。是的。你对访问的看法是错误的!“此处已访问”表示您处于当前节点,然后尝试遍历树。首先在“根”处进行计数,然后在“计数权”处进行计数,然后在“左”处进行

此函数返回以节点为根的二叉树中的节点数。有几篇文章说这是一个预顺序遍历,但对我来说这看起来像是一个后顺序遍历,因为我们在访问根之前访问了左部分和右部分。我错了吗?还是我的“访问”概念有错?

是的。你对访问的看法是错误的!“此处已访问”表示您处于当前节点,然后尝试遍历树。首先在“根”处进行计数,然后是您的计数权限,然后是左侧,因此是的,它是预先排序的。

是的。你对访问的看法是错误的!“此处已访问”表示您处于当前节点,然后尝试遍历树。首先在“根”处进行计数,然后在“计数权”处进行计数,然后在“左”处进行计数,因此它是预排序的。

我们可以说这是预排序遍历,因为
count
函数应用于节点之前,而不是应用于其子节点

但是这个问题相当棘手,因为您使用的是直接递归,在同一个函数中执行遍历和“操作”。

我们可以说这是预顺序遍历,因为
count
函数应用于之前的节点,而不是其子节点


但问题相当棘手,因为您使用的是直接递归,在同一个函数中执行遍历和“操作”。

计算节点的情况下,很难说算法是预序还是后序,因为我们不知道“何时”计算当前节点的“1”

但是,如果我们将案例改为打印,那么很明显:

预购:

int count(Node node) {

  if (node == null)
      return 0;

  int r = count (node.right);
  int l = count (node.left);

  return 1 + r + l;
}
后订单

int visit(Node node) {
  ...
  node.print();          // pre-order  : root cames first
  visit(node.left);
  visit(node.right);
  ...
}
int visit(Node node) {
  ...
  visit(node.left);
  visit(node.right);
  node.print();          // post-order  : root cames last
  ...
}
int count(Node node) {

  if (node == null)
      return 0;

  process(node);    

  int r = count (node.right);
  int l = count (node.left);

  return 1 + r + l;
}
正如您所看到的,我们可以说哪个print()首先出现。 对于计数,我们不能说根是否在子树之前计数(+1)


这是约定的问题。

计算节点是一种很难说算法是预排序还是后排序的情况,因为我们不知道“何时”计算当前节点的“1”

但是,如果我们将案例改为打印,那么很明显:

预购:

int count(Node node) {

  if (node == null)
      return 0;

  int r = count (node.right);
  int l = count (node.left);

  return 1 + r + l;
}
后订单

int visit(Node node) {
  ...
  node.print();          // pre-order  : root cames first
  visit(node.left);
  visit(node.right);
  ...
}
int visit(Node node) {
  ...
  visit(node.left);
  visit(node.right);
  node.print();          // post-order  : root cames last
  ...
}
int count(Node node) {

  if (node == null)
      return 0;

  process(node);    

  int r = count (node.right);
  int l = count (node.left);

  return 1 + r + l;
}
正如您所看到的,我们可以说哪个print()首先出现。 对于计数,我们不能说根是否在子树之前计数(+1)


这是约定的问题。

在这段代码中,每个节点都没有进行实际的处理,因此预订单遍历和后订单遍历之间没有区别。如果有处理,差异将是:

预购

int visit(Node node) {
  ...
  node.print();          // pre-order  : root cames first
  visit(node.left);
  visit(node.right);
  ...
}
int visit(Node node) {
  ...
  visit(node.left);
  visit(node.right);
  node.print();          // post-order  : root cames last
  ...
}
int count(Node node) {

  if (node == null)
      return 0;

  process(node);    

  int r = count (node.right);
  int l = count (node.left);

  return 1 + r + l;
}
后订单

int visit(Node node) {
  ...
  node.print();          // pre-order  : root cames first
  visit(node.left);
  visit(node.right);
  ...
}
int visit(Node node) {
  ...
  visit(node.left);
  visit(node.right);
  node.print();          // post-order  : root cames last
  ...
}
int count(Node node) {

  if (node == null)
      return 0;

  process(node);    

  int r = count (node.right);
  int l = count (node.left);

  return 1 + r + l;
}

(实际上,在这些情况下,与您的代码不同,您可能希望在
节点上递归。left
节点之前。right
保留处理子节点的传统从左到右顺序。)

在这段代码中,没有在每个节点上执行实际处理,因此,预序遍历和后序遍历之间没有区别。如果有处理,差异将是:

预购

int visit(Node node) {
  ...
  node.print();          // pre-order  : root cames first
  visit(node.left);
  visit(node.right);
  ...
}
int visit(Node node) {
  ...
  visit(node.left);
  visit(node.right);
  node.print();          // post-order  : root cames last
  ...
}
int count(Node node) {

  if (node == null)
      return 0;

  process(node);    

  int r = count (node.right);
  int l = count (node.left);

  return 1 + r + l;
}
后订单

int visit(Node node) {
  ...
  node.print();          // pre-order  : root cames first
  visit(node.left);
  visit(node.right);
  ...
}
int visit(Node node) {
  ...
  visit(node.left);
  visit(node.right);
  node.print();          // post-order  : root cames last
  ...
}
int count(Node node) {

  if (node == null)
      return 0;

  process(node);    

  int r = count (node.right);
  int l = count (node.left);

  return 1 + r + l;
}

(实际上,在这些情况下,与您的代码不同,您可能希望在
节点上递归。left
节点之前。right
保留处理子节点的传统从左到右顺序。)

我想如果是
则返回r+l+1那么它将是邮政订单?:)后序遍历的示例是什么?我假设它是
返回r+l+1那么它将是邮政订单?:)后序遍历的示例是什么?