I need to insert strings into a binary search tree but each run through my insert function updates all the nodes and not just the appropriate one. Its required that each word placed in the binary search tree has the exact amount of memory allocated for it (+1 for the NULL pointer).
Here's the struct being used:
typedef struct node_t{
char *word;
struct node_t *left, *right;
} node_t;
Here's how I am passing in the word:
for(i=0; i< original_words -1; i++)
{
fscanf(ifp, "%s", y);
head = insert(head, y);
}
And here's my insert function:
node_t *insert(struct node_t *head, char *word)
{
if(strcmp(head->word, word) > 0)
{
if(head->left == NULL)
{
head->left = create_node(word);
}
else
{
head->left = insert(head->left, word);
}
}
else
{
if(head->right == NULL)
{
head->right = create_node(word);
}
else
{
head->right = insert(head->right, word);
}
}
return head;
}
EDIT: Heres an example of the input file.
4
-------
bravo
-------
alpha
-------
gamma
-------
delta