I MISREAD, thought you said TREE, you can ignore the tree part of this response.
if this is to be a binary search tree or one of its friends, the rule for inserting is lesser values go left, greater values go right.
so if you had some kind of words list...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
my dog ate my homework
it looks like this
my <- root
my
/
dog //dog is less than my, it goes left.
my
/
dog
/
ate //ate is less than my, but the left child exists so repeat the logic. ate is less than dog, go left.
//my is already in there, don't reinsert it? (dictionary rule, one copy??)
and finally
my
/
dog
/ \
ate homework //homework is less than my, but bigger than dog, it goes right on dog...
| |
if you added the word zoo next, it goes right of my, and so on, see?
note that if you insert the dictionary in alphabetical order, you have a linked list, a degraded and less efficient tree. Order of inserts matters, and in order or in reverse order are both awful. Random is good.
you do this kind of thing with a temporary pointer.
tmp = root of tree;
if tmp is null, insert like you do now. (this needs to update root, special case) if not,
if tmp's data == new data, stop, word already in dictionary
else if it is less and tmp.left is null, insert there. if its greater and tmp.right is null, insert there. else if less, tmp = tmp.left. else if greater, tmp=tmp.right.
repeat above until inserted or ignored as duplicate.
you are using C headers. Use the C++ version, it can cause big issues in programs to mix them, but works OK in small programs in school.
<cstdlib>
<cstdio>
do you need cstdio? Its for c-strings mostly, but may have a pointer define or two in it, I forget.
you don't need the struct keyword, that is C.
struct foo {things...};
foo x; //fine, don't have to say struct foo every time, that is relic code.
you also do not need typedef on struct: this is also an archaic or C way of coding.
you also should not stick variables on the end of structs or classes }variable; style. This is also archaic and weird. Just do as my simple example above .. again, like this:
struct name
{fields};
name variable;
typedef bool BOOL;
typedef string WORD;
please do not do this. Its legal, but you are making 2 or 3 mistakes here:
1) you just renamed standard types, so now the reader of your code (imagine it was 10k files and 10million lines of code) has to figure out what your special magic BOOL type is and why it is different from bool and why you did that. Ugg!
2) word is an integer type, of the machine's word size (eg a 64 bit computer has a 64 bit word sized integer). You confuse the reader here twice, once like above because you renamed string, and once again because of the way the word word is used in c++. (this is 2 errors in 1)
3) MAX also used in C++, its a function that finds the max of multiple values, so I would not use this word, even though its OK by being all caps, its still annoying to readers, and some older macros use the all caps version on some older compilers.
if you are stuck using a 1990 compiler (a few students lately are having this problem) then you may need to keep doing what you are doing for some of the above, but get rid of as many of these ancient things as your compiler will tolerate.