• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

searching method for a Binary Search Tree

 
anmadi house
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created a tree ( contains name and age)

now i have to create a method that takes the name as an input searches the tree and returns name and age.

i am not sure how to start this task.

Thank you for time and ideas.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12203
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is your tree sorted in any way? if not, you'll have to traverse every node looking for the one you want.

if it is, you can start at the root, and move to whichever child would be correct based on your comparison.
 
anmadi house
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i used inorderTraversal to print the names and ages alphabetical order.



 
Ryan Beckett
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The generic search algorithm for a BST is something like this:



You can use String.compareTo() for the conditionals.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic