Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A binary tree and its pointers ... (PLEASE help) 1

Status
Not open for further replies.

ankan

Programmer
Jun 22, 2001
70
US
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 &quot;This program has performed an illegal operation...&quot; 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 &quot;Stopped at exception throw&quot; 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. :)
 
the 2d pointer in the struct is your problem. You define no dimentions so only one pointer to a string exists. You would have to do somethng along the lines of

struct tnode{
char* word[MAX_WORDS];
...
}

and on the insert

if p->count-1<MAX_WORDS // dont add it

Matt
 
Thankyou very much, Zyrenthian. You were right. The problem was that I did not allocate space for the whole thing first (how darn stupid of me!).
I assume that you meant &quot;if p->count-1 > MAX_WORDS // dont add it&quot;.
However, I did not change the structure,
struct tnode{
char **word;
...
}
since I wanted to allocate memory dynamically.

Here's what I did ultimately ...
[ignore]
struct tnode *addtree(struct tnode *p, char *w, int till)
{
int cond;
if(p == NULL) {
if( (p = (struct tnode *) malloc(sizeof(struct tnode))) == NULL)
return NULL;
p->count = 1;
[/ignore] if( (p->word = realloc(p->word, sizeof(p->word)+strlen(w))) == NULL) {
free(p);
return NULL;
}
[ignore]
if( (p->word[p->count - 1] = strdup(w)) == NULL) {
free(p);
return NULL;
}
strcpy(p->word[p->count - 1], w);
p->left = p->right = NULL;
}
else if((cond = strncmp(w, p->word[0], till)) == 0) {
p->count++;
[/ignore] if( (p->word = realloc(p->word, sizeof(p->word)+strlen(w))) == NULL) {
free(p);
return NULL;
}
[ignore]
if( (p->word[p->count - 1] = strdup(w)) == NULL) {
free(p);
return NULL;
}
strcpy(p->word[p->count - 1], w);
}
else if(cond < 0)
p->left = addtree(p->left, w, till);
else
p->right = addtree(p->right, w, till);
return p;
}[/ignore]

It seems to be working now (although, as yet i have checked only on small and simple inputs). :)
Bye.
Ankan.

Please do correct me if I am wrong. s-)
 
Make that
...
if( (p->word = realloc(p->word, sizeof(p->word)+strlen(w)+1)) == NULL) {
...

(the classical forgetting of the '\0'), and other small changes not worth mentioning. Bye.
Ankan.

Please do correct me if I am wrong. s-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top