hi, need some help trying to do an in-order traverse of a binary
search tree
ive got this code in my BST Class:
public static void printInOrder(BSTNode top){
if( top!= null)
{
printInOrder(top.left);
System.out.println(top.element);
printInOrder(top.right);
}
}
However is there anything more i need?
And also how do i call this method from the driver class? as i dont
know the BSTNode top. to pass into the method??
tree.printInOrder(what do i put in here???);
any help would be appreciated! thanks!
Murph
Yoyoma_2 - 08 Apr 2004 16:57 GMT
> hi, need some help trying to do an in-order traverse of a binary
> search tree
[quoted text clipped - 11 lines]
>
> However is there anything more i need?
No i think that's fine. Its up to you to ask if you want to do the test
for null before or after you call the function. Right now if top.left
is null you are still going down a level. But thats not really a big
problem. Making it static is also a good idea, way to go :)
> And also how do i call this method from the driver class? as i dont
> know the BSTNode top. to pass into the method??
your "BinaryTree" class should alwaise have a pointer to the root node
or "top" in this case. To call it you simply call binTree.printInOrder(
binTree.getRootNode() );
Hope it helps
Daniel Sjöblom - 08 Apr 2004 18:06 GMT
> hi, need some help trying to do an in-order traverse of a binary
> search tree
[quoted text clipped - 15 lines]
>
> tree.printInOrder(what do i put in here???);
You put a method in BST that returns the root? You make printInOrder an
instance method instead of a static method?

Signature
Daniel Sjöblom
Remove _NOSPAM to reply by mail