• Post Reply Bookmark Topic Watch Topic
  • New Topic

Binary Search Tree Method Help  RSS feed

 
Christopher Calderon
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have an assignment where I have to write several methods for a binary search tree. The node for the binary search tree has two sets of data, a name and a correlating weight. The tree is sorted by names. I am having trouble with one of the requirements for a method.  The method is to search for a name entered by the user and return the weight of that person if found. If not found, the method is to return an error message. I am having trouble with the error message component. I was only able to come up with solutions where an error message was printed several times because the method is recursive. I would appreciate it if anyone could point me in the right direction.

I've included the node class and method down below. The search method does not include the error component.

Thanks.



 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of returning weight you could return root if found or null if not found. Then the calling code can extract weight if the returned node is not null.
 
Carey Brown
Saloon Keeper
Posts: 3328
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to handle the calling of searchName() a little differently, you can't just add the two return values.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!