0

I am constructing a binary tree consisting of words from a sample paragraph, sorted alphabetically. So far, I have implemented all of the basic "behind the scenes" work to define the binary tree (constructors, methods), and I am now working on adding elements (words) to the tree.
Every word has had its non-alphanumeric characters removed and every letter in the word is converted to lowercase. I am wondering how I could enter words into the tree alphabetically? All I have done with binary trees have to do with numbers, so I am not sure what to do in this case. (I was thinking of something to do with ASCII values?)

3
  • 3
    "Any assistance is appreciated!" - assistance with what? I don't see any code... Commented Apr 9, 2012 at 1:53
  • I'm asking about a general algorithm on how to do this. I haven't coded anything yet, I'm still in the problem solving stages. Commented Apr 9, 2012 at 1:57
  • 1
    @lollercopter You say you've done this with numbers.. nothing changes with Strings. Your comparison is now alphabetical is all.. check stackoverflow.com/questions/6203411/… for coming up with a comparison function. Commented Apr 9, 2012 at 2:00

2 Answers 2

2

You say you've done this with numbers before.

Nothing has really changed with your new tree.

You can think of an alphabetical comparison as just a way of giving something a precedence ranking over something else.

So, think of these strings as being a number, the smaller the number, the lower level in the tree the string will occupy. You're simply making your tree sort by smallest numbers first. A is smaller than B, B is smaller than C and so on.

Check out this related question for coming up with a comparison function to give you the "numbers" you're looking for.

Sign up to request clarification or add additional context in comments.

Comments

0

You don't enter stuff into a binary tree "alphabetically", you simply enter stuff into the tree. The tree does the sorting. (Keep in mind that a character string is just a sequence of numbers.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.