简体   繁体   English

Java非二叉树预遍历

[英]Java Non-Binary Tree Pre-Order Traversal

I am having some trouble traversing a non-binary tree and outputting the pre-order enumeration. 我在遍历非二叉树并输出预枚举时遇到一些麻烦。

The current code I have is: 我当前的代码是:

  public String GetPreOrder(ADS2TreeNode root, String res)
  {        
      res += data;
      if (numberOfChildren != 0)
      {
          for (int i = 0; i < numberOfChildren; i++)
          {
              res += "," + children[i];
          }            
      }      
   return res;              
  }


  public String GetPreOrder()
  {  
      String res = "";
      return root.GetPreOrder(root, res);
  }

Based on the test data of "(B(E(K),F))" it outputs B,E(K),F However it should output B, E, K, F 基于“((B(E(K),F))””的测试数据,它输出B,E(K),F。但是它应该输出B,E,K,F

I believe it is because it simply prints the array which stores the children of E rather than accessing them individually? 我相信这是因为它只是打印存储E子项的数组,而不是单独访问它们? However I am unsure how to do this. 但是我不确定如何做到这一点。

Could anyone shine any light as to why this is happening and how to solve this? 任何人都可以就为什么会发生这种情况以及如何解决这个问题大放异彩吗?

#

I now have an issue with the following code: 我现在遇到以下代码的问题:

  public String GetPreOrder(ADS2TreeNode root, String res) { if (root.numberOfChildren == 0) { res += root.data; root = root.parent; } else { for (int i = 0; i < root.numberOfChildren; i++) { res += root.data + ','; root = root.getChild(i); res = GetPreOrder(root, res); } } return res; } 

Whilst debugging it, it loops though each subtree as it should, however it doesn't fully reach the end of the tree - i've spent ages trying to figure it out via debugging but I can't seem to understand why. 在调试它的同时,它会循环遍历每个子树,但是并没有完全到达树的末尾-我花了很长时间尝试通过调试来找出它,但是我似乎不明白为什么。

Can anyone see the mistake with my code or something I am missing? 谁能看到我的代码或我缺少的东西的错误?

Assuming there is a function pre-order-traversal(tree) , it does the following: 假设有一个函数pre-order-traversal(tree) ,它执行以下操作:

  1. Visit the root and display its value/content/etc. 访问根目录并显示其值/内容/等。
  2. Visit the first subtree by recursively calling the pre-order-traversal(tree) function. 通过递归调用pre-order-traversal(tree)函数来访问第一个子pre-order-traversal(tree)
  3. Continue with all the other subtrees from left to right, by calling pre-order-traversal(tree) recursively. 通过递归调用pre-order-traversal(tree) ,从左到右继续所有其他子pre-order-traversal(tree)

Your Code does not traverse the subtrees and only (implicitly) calls their toString() methods which show the given subtree with its children in parentheses. 您的代码不会遍历子树,仅(隐式)调用它们的toString()方法,该方法在给定的子树及其子代中显示括号。

Thus, the solution is to recursively call GetPreOrder(...) on the children, because they are trees, too. 因此,解决方案是对GetPreOrder(...)递归调用GetPreOrder(...) ,因为它们也是树。

Your second problem arises because you reset root to be the first child. 出现第二个问题是因为您将root重置为第一个孩子。 Hence, in the second iteration of the for-loop not the real root is used, but the first subtrees root. 因此,在for循环的第二次迭代中,不使用实际的根,而是使用第一个子树的根。 Simply do not reuse the parameter root and use a local variable instead. 只需不重用参数root,而使用局部变量即可。

res += root.data + ',';
for (int i = 0; i < numberOfChildren; i++)
{
    ADS2TreeNode child = root.getChild(i);
    res += GetPreOrder(child, res);
}

If you were somehow able to access the array from the first part of your question, you could use a for-each-loop which is much nicer. 如果您能够以某种方式从问题的第一部分访问数组,则可以使用for-each-loop更好。

res += root.data + ',';
for(ADS2TreeNode child : children) {
    res += GetPreOrder(child, res);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM