Originally Posted by
laserlight
The thing is, the root is not "null". It is zero initialised.
It might make more sense to have root be a pointer instead. If you do want it to be a struct tnode object, then you can either compare its members with 0, or just the word member.
sorry i just cant get my head round it as a beginner
so i should declare it in my main as this:
Code:
struct tnode *root;
and in my method sort,
-how do i check if the 'root' had been assigned anything?
-do i need to first declare the 'root' is empty?
this is my method which sort words into the tree (made of struct tnode):
Code:
void sort(struct tnode **r, char *w){
if(r == null){
r = (struct tnode *) malloc(sizeof(struct tnode));
r->word = w;
r->count = 1;
r->*left = null;
r->*right = null;
(printf("%s\n", r->word));
}else{
int cmp = strcmp(w, r->word);
if(cmp<0){
sort(r->left, w);
}else if(cmp>0){
sort(r->right, w);
}else r->count++;
}
}
Please help~ and much thanks~!