Results 1 to 4 of 4
- 11-07-2010, 09:51 PM #1
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.
- 11-08-2010, 01:53 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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.
- 11-08-2010, 08:27 PM #3
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.
- 11-08-2010, 08:40 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
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
-
DOnt know if 1st post if did, I am VERY sorry for duplicate post. I have error messg
By afisher300 in forum New To JavaReplies: 3Last Post: 05-04-2009, 03:15 AM -
Level order binary tree traversal
By H3rtaherta in forum New To JavaReplies: 1Last Post: 04-20-2009, 07:34 AM -
Focus Traversal Demo in SWT
By Java Tip in forum SWTReplies: 0Last Post: 07-07-2008, 04:37 PM -
Bidirectional Traversal with ListIterator
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 10:37 PM -
JTable Focus Traversal
By helios_lie in forum AWT / SwingReplies: 1Last Post: 12-20-2007, 10:27 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks