#include < stdlib.h > #include < stdio.h > #define MAX 10000 void extern exit(int); FILE *inFile; char wordsbuffer[MAX]; char *thisword; char *nextword = wordsbuffer; int done = 0; typedef struct treenode { /* Write code to define the fields here */ } TreeNode, *TreeNodePtr; /* Is my character alphabetic? */ int alpha(char ch) { return ((ch > = 'a' && ch < = 'z')||(ch > = 'A'&&ch < = 'Z')); } /* Convert to lower case */ char lower(char c) { return (c > = 'A' && c < = 'Z') ? c + 'a' - 'A' : c; } char *getword() { if (done) return (char*) EOF; thisword = nextword; while (!alpha(*nextword = fgetc(inFile)) && (*nextword != EOF)); if (*nextword == EOF) return (char*) EOF; *nextword = lower(*nextword); while (alpha(*++nextword = lower(fgetc(inFile)))); if (*nextword == EOF) done = 1; *nextword++ = '\0'; return thisword; } void print_tree(t) TreeNodePtr t; { /* Write code to print the information in the tree in a sensible order */ } /* To compare two strings. Returns negative, 0 or positive depending as s precedes, equals, or follows t alphabetically */ int strcomp(s, t) char *s, *t; { for ( ; *s == *t; s++, t++) if (*s == 0) return 0; return (*s - *t); } TreeNodePtr insert(t, w) TreeNodePtr t; char *w; { /* Complete this code */ int comp; if (t == NULL) { /* insert word into empty tree or subtree */ t = malloc(sizeof(TreeNode)); t->myword = w; t->count = 1; t->leftchild = t->rightchild = NULL; } else if ((comp = strcomp(w, t->myword)) == 0) /* Write code to deal with inserting word w identical to that in root t */ ; else if (comp < 0) /* Write code to deal with word w preceding that in root t */ ; else /* Write code to deal with word w following the word in root t */ ; return t; } void recycle(TreeNodePtr t) { /* Write code to free all the memory you malloc'ed */ } main(argc, argv) int argc; char *argv[]; { TreeNodePtr root; if (argc < 2) { fprintf(stderr, "Usage: fread < filename >\n"); exit(-1); } inFile = fopen(argv[1], "r"); char *this; root = NULL; while ((this = getword()) != (char*) EOF) root = insert(root, this); print_tree(root); recycle(root); }