简体   繁体   中英

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

I believe it is because it simply prints the array which stores the children of E rather than accessing them individually? 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:

  1. Visit the root and display its value/content/etc.
  2. Visit the first subtree by recursively calling the pre-order-traversal(tree) function.
  3. Continue with all the other subtrees from left to right, by calling pre-order-traversal(tree) recursively.

Your Code does not traverse the subtrees and only (implicitly) calls their toString() methods which show the given subtree with its children in parentheses.

Thus, the solution is to recursively call GetPreOrder(...) on the children, because they are trees, too.

Your second problem arises because you reset root to be the first child. Hence, in the second iteration of the for-loop not the real root is used, but the first subtrees root. Simply do not reuse the parameter root and use a local variable instead.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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