Results 1 to 4 of 4
  1. #1
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default need help with post order traversal

    Hi, my problem with my post order traversal method of a binary tree is that it prints everything in the correct order just in decending order instead of ascending. Was wondering if anyone could see why. Thanks in advance.

    Java Code:
    private void postOrderItrLRN(Node localRoot)
        {
            Node current = localRoot;
            
            while(true)
            {
                if(current != null)
                {
                    theStack.push(current);
                    System.out.println(current.getCData());
                    current = current.getRightChild();
                }
                else if(theStack.isEmpty())
                    break;
                else
                    current = theStack.pop().getLeftChild();
                    
            }
        }
    Last edited by Metastar; 11-07-2010 at 10:15 PM.

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    If you build a Binary Search Tree, the traversal that outputs the elements in ascending order is inorder, not postorder.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    Metastar's Avatar
    Metastar is offline Member
    Join Date
    Jul 2010
    Posts
    62
    Rep Power
    0

    Default

    I do understand that, I'm just trying to figure out why it's printing them that way and how I can get my method to print them in postorder. I still cannot figure this one out. My method as it stands now has a few errors, just seeing if someone can point them out and guide me in the right direction.

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Hmm, glancing over your code, it seems to do the printout in preorder, since you print out the root, and then traverse the left and right subtrees. One tip, writing tree traversals recursively is much simpler. Write up a recursive version of post order traversal, study the results and then go back to your iterative version.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Replies: 3
    Last Post: 05-04-2009, 03:15 AM
  2. Level order binary tree traversal
    By H3rtaherta in forum New To Java
    Replies: 1
    Last Post: 04-20-2009, 07:34 AM
  3. Focus Traversal Demo in SWT
    By Java Tip in forum SWT
    Replies: 0
    Last Post: 07-07-2008, 04:37 PM
  4. Bidirectional Traversal with ListIterator
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:37 PM
  5. JTable Focus Traversal
    By helios_lie in forum AWT / Swing
    Replies: 1
    Last Post: 12-20-2007, 10:27 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •