# Include
# Include
# Include
# Include
# Define MAXWORD 100 struct tree_node * addtree (struct tree_node *, char *); void treeprint (struct tree_node *); int getword (char *, int ); // define the binary tree structure struct tree_node {char * word; // pointer to the word int * count; // count the number of times the word appears struct tree_node * left; // pointer to the left subtree self-reference struct tree_node * right; // pointer to the right subtree}; int main () {struct tree_node * root; char word [MAXWORD]; root = NULL; while (getword (word, MAXWORD )! = EOF) if (isalpha (word [0]) root = addtree (root, word); treeprint (root); return 0 ;} // allocate space for the new node struct tree_node * tree_alloc (void); // copy the new word char * strdup (char *); struct tree_node * addtree (struct tree_node * p, char * w) {int cond; if (p = NULL) {// a new word has arrved; p = tree_alloc (); // make a new node; p-> word = strdup (w); p-> count = 1; p-> next = p-> right = NULL;} else if (cond = strcmp (w, p-> word) = 0) p-> Count ++; // The word else if (cond <0) that exists in the binary tree) // The new word is smaller than the node in the tree and placed on the left p-> left = addtree (p-> left, w); elsep-> right = addtree (p-> right, w); return p;} // The treeprint function prints the tree in sequence ==> print the left subtree and then print itself, print the right subtree void treeprint (struct tree_node * p) {if (p! = NULL) {treeprint (p-> left); printf ("% 8d % s \ n", p-> count, p-> word ); treeprint (p-> right) ;}// allocate space required by the new node // struct tree_node * tree_alloc (void) {return (struct tree_node *) malloc (sizeof (struct tree_node);} // The strdup function copies the input string to a safe position char * strdup (char * s) {char * p by calling the malloc function; p = (char *) malloc (strlen (s) + 1); // allocate one more byte to store '\ 0 ', the length returned by the strlen function does not include '\ 0. if (p! = NULL) // space allocation may fail, so judge strcpy (p, s); return p;} first ;}