• Post Reply Bookmark Topic Watch Topic
  • New Topic

A little help would b appreciate for creating binary search tree  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm trying to create binary serach tree that contains two int values and one string value sorted in the lexicographic but im not sure what to do. I've created an array list, which has been already sorted, but the binary tree has to be a reference-based which is not sorted and I'm thinking about sorting the list while creating it. Can any one help with this please?? Any brief idea would b appreciated!! Thank you!!
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It's not "b" but "be", please.

Most binary trees I know sort automatically as values are entered; they usually (but not always) behave as sets in that duplicates are not entered.
Try drawing a tree and see how you get on with it. Work out how to enter values and how to search recursively. If I remember correctly, there is a nice example (using ints) in the Deitel and Deitel book.
 
Max Cho
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I'll watch out
So what I'm trying to accomplish is creating a binary tree from an array list, which contains String id, name, age, and sorts it by the name in lexicographic. I know how to use a comparator to sort the array list but I'm not sure about how to sort the nodes while adding onto the tree.
According to your post, the tree automatically sorts when the value is int. Am I correct? But the value im trying to sort is the String so do I need to use comparator or comparable??
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to see whether the class you are using can implement the Comparable<T> interface, in which case it ought to. Then you sue Comparable<T>. If it can't implement Comparable<T>, you would have to use a Comparator<T>.
The reason for using ints in Deitel and Deitel is so as to show the tree, unencumbered by Comparators.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!