/*@function:统计输入中的所有单词出现的次数
* @method: 使用二叉查找树。对输入的单词建立一颗二叉查找树,新输入的单词若存在于树中,则增加该节点的计数值;否则,新增一个节点
*/
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<string.h>
#define MAXWORD 100
typedef struct tnode
{
char word[MAXWORD];
int count;
struct tnode *left;
struct tnode *right;
}tnode_t;
tnode_t *addtree(tnode_t *, char *);
void treeprint(tnode_t *);
int getword(char *, int);
main(void)
{
tnode_t *root;
char word[MAXWORD];
root = NULL;
while(getword(word, MAXWORD) != EOF)
if(isalpha(word[0]))
root = addtree(root, word);
treeprint(root);
return 0;
}
/* add a node to the tree by using non-recursive method */
/*
tnode_t *addtree(tnode_t *p, char *w)
{
int cond;
tnode_t *s, *temp1, *temp2;//s为待插入的node,temp2为temp1的父节点
s = (tnode_t *)malloc(sizeof(tnode_t));
if(s == NULL)
{
printf("error: malloc\n");
exit(0
统计输入中所有单词出现的次数(使用二叉查找树实现:递归和非递归)
最新推荐文章于 2021-11-22 18:18:27 发布