hi i need to get a program done that reads a series of numbers and operations from a file using in order traversal to build a tree from the file data and evaluate the data in post order by traversing the tree while modifying a stack.
here is what i got so far:
its compiles but it doesnt seem to run can anyone help me please!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_WORD_SIZE 4
typedef struct {
char word[MAX_WORD_SIZE + 1];
int freq;
} NodeData;
typedef struct treenode {
NodeData data;
struct treenode *left, *right;
} TreeNode, *TreeNodePtr;
typedef struct {
TreeNodePtr root;
} BinaryTree;
void inOrder(TreeNodePtr node); //struct
void postOrder(TreeNodePtr node); //struct
TreeNodePtr newTreeNode(FILE *read);
NodeData newNodeData(char str[]);
void visit(TreeNodePtr node);
int main (){
FILE *read;
if((read = fopen("treeInput.txt", "r")) == NULL){ //read file
printf("File doesnt exist");
return 1;
}
BinaryTree bt;
bt.root = newTreeNode(read);
inOrder(bt.root);
return 0;
}
void visit(TreeNodePtr node) {
printf("%s ", node -> data.word);
}
void postOrder(TreeNodePtr node) { //post order
if (node != NULL) {
postOrder(node -> left);
postOrder(node -> right);
visit(node);
}
}
void inOrder(TreeNodePtr node) { //in order
if (node != NULL) {
inOrder(node -> left);
printf("%s ", node -> data.word);
inOrder(node -> right);
}
}
TreeNodePtr newTreeNode(FILE *read){ //creating the tree
char str[MAX_WORD_SIZE + 1];
fscanf(read, "%s", str);
if (strcmp(str, "@") == 0) return NULL;
TreeNodePtr p = (TreeNodePtr)malloc(sizeof(TreeNode));
strcpy(p -> data.word, str);
p -> left = newTreeNode(read);
p -> right = newTreeNode(read);
return p;
}
NodeData newNodeData(char str[]) { //adding a node
NodeData temp;
strcpy(temp.word, str);
return temp;
}