0
class TreeTraversal
{
  public void main(String[] args) throws IOException
  {
    System.out.println("Displaying the tree");
    theTree.displayTree();

/** how to you print these kind of things? e.g theTree.preOrder(theTree.returnRoot()); in to the jTextArea1 ? */

    StringBuilder order1 = new StringBuilder("Inorder traversal: ");
    jTextArea1.append("\n"+order1);
    //System.out.println("Inorder traversal");
    theTree.inOrder(theTree.returnRoot());
    //System.out.println(" ");

    StringBuilder order2 = new StringBuilder("Preorder traversal: ");
    jTextArea1.append("\n"+order2);
    //System.out.println("Preorder traversal");
    theTree.preOrder(theTree.returnRoot());
    //System.out.println(" ");

    StringBuilder order3 = new StringBuilder("Postorder traversal: ");
    jTextArea1.append("\n"+order3);
    //System.out.println("Postorder traversal");
    theTree.postOrder(theTree.returnRoot());
    //System.out.println(" ");
  }
}
Piro
  • 1,367
  • 2
  • 19
  • 40

1 Answers1

0

Visitor pattern is suitable for traversing object structures. To learn about visitor see http://www.oodesign.com/visitor-pattern.html. If this is too complex for you, here is more understandable visitor in java http://en.wikipedia.org/wiki/Visitor_pattern

You have to create visitor that prints currently visited node.

To change traversal order you can use what you have (postOrder,preOrder,inOrder) or you can make visitor method to return some value. Value should mean noIteration/inOrder/preOrder/postOrder. Advantage of this is you can stop/change traversal at any node.

Piro
  • 1,367
  • 2
  • 19
  • 40