Use binary search tree to count the frequency of words appearing in text

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.