Binary Tree

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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.
}
}
else if ((i = strcmp(node->data,x))<0)
	node->left=inserttree(node->left,x);
else if (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);

}
}
Topic archived. No new replies allowed.