Hi,
I am writing a program that takes input fr stdin, breaks it into tokens of words, and then count the occurrence of the each word by sorting them into a tree of struct.
however when i tried to put the words into the struct, it kept giving root a NULL.
Here's my code:
Code:
void sortwords(char **warr, int wc, struct tnode *r){
int c=0;
for(c=0;c<wc;c++){
sort(r, warr[c]);
}
}
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;
}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++;
}
}
and here's my struct and how i initialized it:
Code:
struct tnode { char *word;
int count;
struct tnode *left;
struct tnode *right;
};
int main (int argc, const char * argv[]) {
...
struct tnode *root = NULL;
...
//warray is an array of words, checked, no problem
//wordsread is the number of words in the array
sortwords(warray, wordsread, root);
...
return 0;
}