• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Binary Search Tree Method Help

 
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.



 
Bartender
Posts: 5915
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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
Bartender
Posts: 5915
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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!