Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / April 2004

Tip: Looking for answers? Try searching our database.

Traversing a binary search tree

Thread view: 
MUrph - 08 Apr 2004 12:43 GMT
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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.