C语言通过二叉树实现单词出现频率的统计
2010-09-15 11:48
1671 查看
一步步记录自己的成长,在DVE-C++下编译通过
#include <stdio.h> #include <ctype.h> #include <string.h> #include <stdlib.h> #define MAXWORD 100 struct tnode{ //树的节点 char *word; //指向单词的指针 int count; //单词出现的次数 struct tnode *left; //左子节点 struct tnode *right; //右子节点 }; struct tnode *addtree(struct tnode *,char *); void treeprint(struct tnode *); int getword(char *,int); //单词出现频率的统计 main() { struct tnode *root; char word[MAXWORD]; root = NULL; while(getword(word,MAXWORD)!=EOF) if(isalpha(word[0])) root=addtree(root,word); treeprint(root); system("PAUSE"); return 0; } struct tnode *talloc(void); //char *strdup(char *s); //addtree函数:在p的位置或者P的下方增加一个W节点 struct tnode *addtree(struct tnode *p,char *w) { int cond; if(p==NULL){ //该单词是一个新单词 p=talloc(); //创建一个新节点 p->word=strdup(w); p->count=1; p->left=p->right=NULL; }else if((cond=strcmp(w,p->word))==0) p->count++; //新单词与节点中的单词匹配 else if(cond<0) //如果小于该节点中的单词,则进入左子树 p->left=addtree(p->left,w); else //如果大于该节点的单词,则进入右子树 p->right=addtree(p->right,w); return p; } //treeprint函数:按序列打印树P void treeprint(struct tnode *p) { if(p!=NULL){ treeprint(p->left); //左子树 printf("%6d %s% /n",p->count,p->word); //本身 treeprint(p->right); //右子树 } } //getword:get next word or character input int getword(char *word,int lim) { int c,getch(void); void ungetch(int); char *w=word; while(isspace(c=getch())) ; if(c!=EOF) *w++=c; if(!isalpha(c)){ *w='/0'; return c; } for(;--lim>0;w++) if(!isalnum(*w=getch())){ ungetch(*w); break; } *w='/0'; return word[0]; } #define BUFSIZE 100 char buf[BUFSIZE]; int bufp=0; int getch(void) { return (bufp>0)?buf[--bufp]:getchar(); } void ungetch(int c) { if(bufp>=BUFSIZE) printf("ungetch:too many charactors/n"); else buf[bufp++]=c; } #include <stdlib.h> //talloc函数:创建一个tnode struct tnode *talloc(void) { return (struct tnode *)malloc(sizeof(struct tnode)); }
相关文章推荐
- 用C语言实现了对英文文章中单词频率的统计,得到出现最多的前十个!
- 遍历Map方法一(通过HashMap类实现统计单词出现次数)
- 搜索二叉树应用---判断单词正确与否,简单字典的实现,出现ip次数的统计
- C语言实现二叉树-利用二叉树统计单词数目
- Java实现 统计单词出现的次数并按照单词频率从高到低输出
- 有关统计单词频率的算法c语言实现
- 有关统计单词频率的算法c语言实现
- 有关统计单词频率的算法c语言实现
- 关于统计ascii码文本中英语单词的出现频率的c++实现
- 一个简单的程序,统计文本文档中的单词和汉字数,逆序排列(出现频率高的排在最前面)。python实现。
- 统计分析文章中英文单词出现次数及频率(C++实现)
- python3 实现统计单词表中每个字母的出现频率
- 计算单词出现的频率C语言实现的
- 实现一个控制台程序,给定一段英文字符串,统计其中各个英文单词(4字符以上含4字符)的出现频率。
- 统计一段文字中出现频率最高的10个单词(c语言)
- C语言输入英文统计单词的出现的频率排序
- 有关统计单词频率的算法c语言实现
- C语言实现的统计一个单词在文章中的频率
- 计算单词出现的频率C语言实现的
- 统计文件中各单词出现的频率(二叉排序树实现)