Combine a few main functions together:
1. Read single words from text (remove spaces, special symbols, etc.)
2. Use the words that are read to update the nodes of the binary tree (involving the construction of the two-fork tree, recursion)
3. Middle sequence traversal, to recursively print each node of the binary tree
Code:
#include <stdio.h>#include<stdlib.h>#include<string.h>#include<ctype.h>#defineMaxword 1000//the statistics of the frequency of the word occurrence ohstructtnode{Char*Word; intcount; structtnode*Left ; structtnode*Right ;};structtnode* Addtree (structtnode*,Char*);voidTreeprint (structtnode*);intGetword (Char*,int); FILE*FP;intMain () {FP=fopen ("//users//wuzengxiong//desktop//c//c1.c","R"); structtnode*Root; CharWord[maxword]; Root=NULL; while(Getword (word,maxword)! =EOF)if(Isalpha (word[0])) root=Addtree (Root,word); Treeprint (root); Fclose (FP); return 0;}structtnode* Talloc (void);Char* My_strdup (Char*);structtnode* Addtree (structTnode* p,Char*W) { intcond; if(p==NULL) {P=Talloc (); P->count=1; P->word=My_strdup (W); P->left=p->right=NULL; }Else if((cond=strcmp (p->word,w)) = =0) {p->count++; }Else if(cond>0) {p->left=addtree (p->left,w); }Else{p->right=addtree (p->right,w); } returnp;}voidTreeprint (structtnode*p) { if(p!=NULL) {Treeprint (P-Left ); printf ("%4d%s\n",p->count,p->word); Treeprint (P-Right ); } return;}structtnode* Talloc (void){ return(structtnode*) malloc (sizeof(structtnode));}//copy s into heap memoryChar* My_strdup (Char*s) { Char*p; P=(Char*) malloc (strlen (s) +1); if(p!=NULL) {strcpy (p,s); } returnp;}#defineBUFSIZE 100CharBuf[bufsize];//manage the fallback of a cache implementation character yourself. intbufp=0;intGetch () {return(bufp>0)? buf[--bufp]:getc (FP);}voidUngetch (intc) { if(bufp==BUFSIZE) printf ("Ungech:too many characters\n"); ElseBUF[BUFP++]=C;}intGetword (Char* Word,intLim) { intC//as the return value, to determine whether it is EOF Char) t=Word; while(Isspace (c=getch ())) ; if(c!=eof) *w++=C; if(!Isalpha (c)) { *w=' /';//if it is a symbol, return a single symbol + ' returnC; } for(;--lim>0;++W) {if(!isalnum (*w=getch ())) {//if the read-in is not a number or a letter, then exit, indicating that a word has been read, but read a more characters, need to press back to the cacheUngetch (*W); Break; } } *w=' /'; returnword[0];}
Use binary search tree to count the frequency of words appearing in text