I am writing code for a binary search tree that gets input from a text file and sorts it into a binary tree.. I have a pre-order, post-order, and in-order function and they all work fine. My issue is that when I try and display the original list of words, I can't get it to print in that order.
For example, if the text file contains:
test
sample
binary
tree
It should output in that order. my problem is that it outputs it in pre-order instead. any help. I won't post the whole code, but I'll post the insert tree function and the printTree function.
struct node *inserttree(struct node *node,char *x)
{
int i;
if (node==NULL) //if node==null, it is at root, because I haven't created any leaf nodes.
//if the data is = node, allocate memory to fit Struct node
if ((node=(struct node *)
malloc(sizeof (struct node)))!=NULL)
//if the data is = to the 50 letters allowed, then allocate 50 + 1 for memory
if ((node->data=(char *)
malloc(strlen(x)+1))==NULL)
{
free(node);
node=NULL;
}
else //
{
node->left=NULL;
node->right=NULL;
strcpy(node->data,x); //copy string data into node.
}
}
elseif ((i = strcmp(node->data,x))<0)
node->left=inserttree(node->left,x);
elseif (i > 0) //checks if i is less than 0
node->right=inserttree(node->right,x);
return node;
}
void printTree(struct node *node)
{
if (node!=NULL)
{
printf("\t%s\n",node->data);
printTree(node->left);
printTree(node->right);
}
}