/* ================================================================ Loads the nodes of a previously stored BSST from a binary file. Constructs a binary search sequence tree and then the BSST is traversed in in-order fashion and the sorted list is printed. Compile as gcc -ansi inpfile.c -o objfile Prabu 3/15/94 ================================================================ */ #include #define FNAMELEN 15 /* maximum length of the filename */ struct node { int info; struct node *left; /* left child */ struct node *right; /* right child */ }; const int nodesize = sizeof(struct node); /* in bytes */ int getfile(char *message, FILE **fp, char *mode) /* get the file and open it */ { char filename[FNAMELEN + 1]; int i; printf("%s filename : ",message); scanf("%s",filename); if ((*fp = fopen(filename,mode)) == NULL) { printf("can't open %s \n",filename); return(-1); } return(0); } void inorder(struct node *r) /* print the integers in sorted form */ { if (r != NULL) { inorder(r->left); printf(" %d ", r->info); inorder(r->right); } return; } void preordload(struct node **r, FILE **f) /* load the node in binary format from the file -- preorder fashion */ { struct node *p; p = (struct node *) malloc(nodesize); /* make a new node */ if (fread(p, nodesize, 1, *f)) { *r = p; if ((*r)->left) preordload(&(*r)->left, f); if ((*r)->right) preordload(&(*r)->right, f); } return; } main() { struct node *root, *p; int n; FILE *fp; root = NULL; if (getfile("Input",&fp,"rb") < 0) return; preordload(&root, &fp); printf("This binary search sequence tree is loaded from the file \n"); printf("\nSorted numbers are :\n"); inorder(root); printf("\n\n"); }