I will provide you with a function which reads a forest from a file in the following format. :
11 A S A M P L E T R E E 2 2 9 7 7 7 7 8 9 9 9Your main program file should look as follows. You will be supplied with the file forest.h which contains the function load_forest().
#include "forest.h"
#define MAX_TREE_NODE 100
FILE *f1;
struct tnode {
char key;
tnode *left;
tnode *right;
};
typedef struct tnode * tnode_ptr;
tnode_ptr root;
main(int argc, char *argv[])
{
char c;
f1 = fopen(argv[1],"r");
root = load_forest();
preorder_traverse(root);
fclose(f1);
exit(0);
}
To complie and run tour code on unix systems do the following:
To Compile: g++ -o traverse traverse.c loadf.o
I assume that your code file is traverse.c and I will supply a file called loadf.o which has the code for loading the forest from a file.
To run: traverse forest.dat
where forest.dat is a data file containing a forest in the format specified above.
Extra Credit: I will give extra credit to anyone who writes their own code to handling the loading of a forest as a binary tree.
Hint: You will need to implement the queue data structure in order to perform level-order traversal of a forest.
Last Updated: April 5, 1999 8:47 pm by