My program is suppose to read characters from a file and display the pre-order, in-order, and post-order traversal of the content in the file. The issue is that it only displays half the content in the file. Not sure where and why it stops reading from the file?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MaxWordSize 50
typedef struct {
char word[MaxWordSize + 1];
}NodeData;
typedef struct treeNode {
NodeData data;
struct treeNode *left, *right;
}TreeNode, *TreeNodePtr;
typedef struct {
TreeNodePtr root;
}BinaryTree;
void visit(TreeNodePtr node) {
printf("%s", node -> data.word);
}//end visit
void preOrder(TreeNodePtr node) {
void visit(TreeNodePtr);
if (node != NULL) {
visit(node);
preOrder(node -> left);
preOrder(node -> right);
}
}
void inOrder(TreeNodePtr node) {
void visit(TreeNodePtr);
if (node != NULL) {
inOrder(node -> left);
visit(node);
inOrder(node -> right);
}
}
void postOrder(TreeNodePtr node) {
void visit(TreeNodePtr);
if (node != NULL) {
postOrder(node -> left);
postOrder(node -> right);
visit(node);
}
}
TreeNodePtr buildTree(FILE *in) {
char str[MaxWordSize + 1];
fscanf(in, "%s", str);
if (strcmp(str, "@") == 0) {
return NULL;
}
TreeNodePtr p = (TreeNodePtr)malloc(sizeof(TreeNode));
strcpy(p -> data.word, str);
p -> left = buildTree(in);
p -> right = buildTree(in);
return p;
}
int main() {
TreeNodePtr buildTree(FILE *);
void preOrder(TreeNodePtr);
void inOrder(TreeNodePtr);
void postOrder(TreeNodePtr);
FILE *in = fopen("./c/btree.in.txt", "r");
BinaryTree bt;
bt.root = buildTree(in);
printf("\n The pre-order traversal is : ");
preOrder(bt.root);
printf("\n The in-order traversal is : ");
inOrder(bt.root);
printf("\n The post-order traversal is : ");
postOrder(bt.root);
printf("\n\n");
fclose(in);
system ("PAUSE");
return 0;
}
My input file content is:
C E F @ @ H @ @ B @ @ G A @ @ N J @ @ K @ @
My output is:
The pre-order traversal is: CEFHB
The in-order traversal is: FEHCB
The post-order traversal is: FHEBC