PLEASE help in anyway you can. I've got fed up with trying to solve this.
Lets get to the point directly...
A simple binary tree...
struct tnode {
char **word; /* this stores the different words */
int count; /* this keeps the count */
struct tnode *left;
struct tnode *right;
};
A simpler objective... to store in the above tree and print out each group of words from an input buffer that have the first few (how many? given by the variable 'till') characters identical, in alphabetical order.
The words are sent to the function addtree:
struct tnode *addtree(struct tnode *p, char *w, int till)
{
int cond;
if(p==NULL) { /* found new */
p = (struct tnode *) malloc(sizeof(struct tnode));
p->count = 1;
p->word[(p->count)-1] = strdup(w);
p->left = p->right = NULL;
} else if((cond = strncmp(w, p->word[0], till)) == 0) { /* found a match */
p->count++;
p->word[(p->count)-1] = strdup(w);
printf("Added to old... new: %s\n", w);
} else if(cond < 0)
p->left = addtree(p->left, w, till);
else
p->right = addtree(p->right, w, till);
return p;
}
The main fiunction ...
int main()
{
struct tnode* root;
int till = 6;
/* ... */
/* --------- snip ------ */
root = NULL;
while (getw(word, in) != EOF) /* getw() gets the next word from the input stream 'in' */
if(isalpha(word[0]))
root = addtree(root, word, till);
treeprint(root); /* a function traversing the binary tree for printing */
/* --------- snip ------ */
/* ... */
}
The Problem ... Its not running correctly. Sometimes it prints out only the last word. Sometimes the "This program has performed an illegal operation..." message crops up. And sometimes its not running at all!! There maybe a problem with the pointers. But where??
On stepping through with the Debugger it gave "Stopped at exception throw" error. And also the root->word (in main) gets changed with every call to the recursive function (although the address of root does not change, it should not anyway). But it seems (after much tracing through the code with the debugger) that the malloc function (bolded) in the recursive function gives the same address to p in each subsequent recursive call!!
Where did I go wrong? Give me a hint but please reply.
Can you help?? Please.
Note: I posted the above query in the C forum but since there was absolutely no response, I'm trying here.
Waiting.
Ankan. Please do correct me if I am wrong.
Lets get to the point directly...
A simple binary tree...
struct tnode {
char **word; /* this stores the different words */
int count; /* this keeps the count */
struct tnode *left;
struct tnode *right;
};
A simpler objective... to store in the above tree and print out each group of words from an input buffer that have the first few (how many? given by the variable 'till') characters identical, in alphabetical order.
The words are sent to the function addtree:
struct tnode *addtree(struct tnode *p, char *w, int till)
{
int cond;
if(p==NULL) { /* found new */
p = (struct tnode *) malloc(sizeof(struct tnode));
p->count = 1;
p->word[(p->count)-1] = strdup(w);
p->left = p->right = NULL;
} else if((cond = strncmp(w, p->word[0], till)) == 0) { /* found a match */
p->count++;
p->word[(p->count)-1] = strdup(w);
printf("Added to old... new: %s\n", w);
} else if(cond < 0)
p->left = addtree(p->left, w, till);
else
p->right = addtree(p->right, w, till);
return p;
}
The main fiunction ...
int main()
{
struct tnode* root;
int till = 6;
/* ... */
/* --------- snip ------ */
root = NULL;
while (getw(word, in) != EOF) /* getw() gets the next word from the input stream 'in' */
if(isalpha(word[0]))
root = addtree(root, word, till);
treeprint(root); /* a function traversing the binary tree for printing */
/* --------- snip ------ */
/* ... */
}
The Problem ... Its not running correctly. Sometimes it prints out only the last word. Sometimes the "This program has performed an illegal operation..." message crops up. And sometimes its not running at all!! There maybe a problem with the pointers. But where??
On stepping through with the Debugger it gave "Stopped at exception throw" error. And also the root->word (in main) gets changed with every call to the recursive function (although the address of root does not change, it should not anyway). But it seems (after much tracing through the code with the debugger) that the malloc function (bolded) in the recursive function gives the same address to p in each subsequent recursive call!!
Where did I go wrong? Give me a hint but please reply.
Can you help?? Please.
Note: I posted the above query in the C forum but since there was absolutely no response, I'm trying here.
Waiting.
Ankan. Please do correct me if I am wrong.