Your addNode function has some issues.
I will point out the incorrect area
struct tnode *addNode(struct tnode *root, char *word) {
int cmp;
if (root == NULL) {
root = (struct tnode*)malloc(sizeof(struct tnode));
root->word = strdup(word);
root->num = 1;
root->left = root->right = NULL;
}
else if ((cmp = strcmp(word, root->word)) == 0)
root->word += 1; else if (cmp > 0) root->right = addNode(root->right, word);
else
root->left = addNode(root->left, word);
return root;
}
The suggested code would be:
struct tnode *addNode(struct tnode *root, char *word) {
int cmp= strcmp(word, root->word;
if (root == NULL) {
root = (struct tnode*)malloc(sizeof(struct tnode));
root->word = strdup(word);
root->num = 1;
root->left = root->right = NULL;
}
else if (cmp == 0)
root->num += 1;
else if (cmp > 0)
root->right = addNode(root->right, word);
else
root->left = addNode(root->left, word);
return root;
}
----add----
My mistake I forget to answer that one.
You are calling addNode recursively. If you have 300,000 unique words, then you will call recursively 150,000 to 300,000 times. It can be a reason. Take a look at this answer
recursion - Maximum recursive function calls in C/C++ before stack is full and gives a segmentation fault? - Stack Overflow[
^]. This is an another well written explanation of stack over flow:
memory - How does a "stack overflow" occur and how do you prevent it? - Stack Overflow[
^]