You need to move down the left side pushing all the nodes onto the stack, then when you hit the smallest(no more left children) you pop smallest off the top of stack and print it, test it for a right child.
If it has one, push that child onto stack and try run down its left side, then again, you pop them back off, printing and checking for right subtrees after you print each node. If it doesn't have a right child pop the next off the stack, print it and check for right child.
This logic works but you'll need to form it into loops.
Main Topics
Browse All Topics





by: twobitadderPosted on 2004-03-25 at 19:35:14ID: 10683984
I can tell you why you have a null pointer exception but I've never attempted iterative in-order traversal of a tree.
tValue());
You get the null pointer because you fly down the left of the tree until you cannot go any further, then here:
System.out.println(temp.ge
temp = temp.getRight();
you try to get the right child of the smallest element in the tree, this may or may not exists:
picture might help:
/ /
^ ^
smallest smallest
^ ^ ^ ^ <----no right child.
2ndsmallest