Ok, my loop to go through the dictionary and "add" is
Code:
do
do
{
fgets(buffer, 20, filePtr);
printf ("%s", buffer);
treeNode_insert(root, buffer);
} while (!feof (filePtr));
treeNode_insert that is being called everytime. The one thing that I am not 100% sure about, is my insertion function is returning a pointer to a node, however I am continuously calling (root) in the loop to get through my dictionary. Maybe this has something to do with it?
Code:
struct treeNode *treeNode_insert(struct treeNode *p, char value[])
{
struct treeNode *tmp_one = NULL;
struct treeNode *tmp_two = NULL;
int resultforTemp1;
int resultforTemp2;
if(p == NULL)
{
/* insert [new] treeNode as root node */
p = (struct treeNode *)malloc(sizeof(struct treeNode));
p->data = malloc(strlen(value) + 1);
strcpy(p->data, value);
p->left = p->right = NULL;
}
else
{
tmp_one = p;
/* Traverse the tree to get a pointer to the specific treeNode */
/* The child of this treeNode will be the [new] treeNode */
while(tmp_one != NULL)
{
tmp_two = tmp_one;
resultforTemp1 = strcmp (tmp_one->data, value);
if(resultforTemp1 > 0)
tmp_one = tmp_one->left;
else
tmp_one = tmp_one->right;
}
resultforTemp2 = strcmp(tmp_two->data, value);
if(resultforTemp2 > 0)
{
/* insert [new] treeNode as left child */
tmp_two->left = (struct treeNode *)malloc(sizeof(struct treeNode));
tmp_two = tmp_two->left;
strcpy(tmp_two->data, value);
// tmp_two->data = value;
tmp_two->left = tmp_two->right = NULL;
} else {
/* insert [new] treeNode as left child */
tmp_two->right = (struct treeNode *)malloc(sizeof(struct treeNode));
tmp_two = tmp_two->right;
strcpy(tmp_two->data, value);
// tmp_two->data = value;
tmp_two->left = tmp_two->right = NULL;
}
}
return(p);
My tree print
Code:
void print_tree (struct treeNode *p)
{
print_node (p);
if (p->left != NULL) print_tree(p->left);
if (p->right != NULL) print_tree (p->right);
}
void print_node (struct treeNode *p)
{
if (p==NULL)
{
printf ("Empty top\n");
return;
}
printf ("The word is %s", p->data);
}