• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Binary Search Tree Lookup Method

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, I'm having a bit of trouble creating the search method for the BST. I've come up with the following:



BNode


I need to create a getKey method to BNode so that I can get the key of the currentNode so than I can compare string against string. How do I go about doing this?

Thanks
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A better way would be to require the key to implement the Comparable interface. Conversion to String to do the comparison is fraught with errors (numbers won't compare in the correct order, and worse, the same number can have different String representations). You would then call compareTo directly on the key.

Another possibility would be to allow the user to provide a Comparator to actually compare the keys. Both approaches can be easily combined. You might want to have a look at JDK source of TreeSet/TreeMap to see how that can be done.
 
reply
    Bookmark Topic Watch Topic
  • New Topic