统计输入中所有单词出现的次数(使用二叉查找树实现:递归和非递归)

这篇博客通过C语言实现了统计输入中所有单词出现次数的功能,利用二叉查找树(BST)进行数据结构存储。文章包含两个函数:一个是递归方法,另一个是非递归方法将新单词插入到树中,同时更新单词计数。在遍历文件读取单词后,对树进行中序遍历并打印每个单词及其出现次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*@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);
	}
	strcpy(s->word, w);
	s->count = 1;
	s->left = s->right = NULL;
	if(p == NULL) //if tree is empty
	{
		p = s;
		return p;
	}
	temp1 = p;
	while(temp1 != NULL) //find the place to insert
	{
		temp2 = temp1;
		if((cond = strcmp(w, temp1->word)) < 0)
			temp1 = temp1->left;
		else if(cond == 0) //if the node is existed, count increases, and free memory of s
		{
			temp1->count++;
			free(s);
			return p;
		}
		else
			temp1 = temp1->right;
	}
	if((cond = strcmp(w, temp2->word)) < 0)
		temp2->left = s;
	else
		temp2->right = s;
	return p;
}
**/

/* add a node to the tree by using recursive method */
tnode_t *addtree(tnode_t *p, char *w)
{
	int cond;
	tnode_t *s;
	
	if(p == NULL) //基准
	{
	    s = (tnode_t *)malloc(sizeof(tnode_t));
		if(s == NULL)
		{
			printf("error: malloc\n");
			exit(0);
		}
		strcpy(s->word, w);
		s->count = 1;
		s->left = s->right = NULL;
		p = s;
		return p;
	}
	else if((cond = strcmp(w, p->word)) < 0)
		p->left = addtree(p->left, w);//note
	else if(cond == 0)
		p->count++;
	else 
		p->right = addtree(p->right, w);
	return p;
}

/***********************************/
/* inorder print */
void treeprint(tnode_t *p)
{
	if(p != NULL)
	{
		treeprint(p->left);
		printf("%s\t%d\n", p->word, p->count);
		treeprint(p->right);
	}
}

/***********************************/
int getword(char *word, int lim)
{
	int c;
	char *w = word;
	
	while(isspace(c = getchar())) //检查参数c是否为空格字符,即为下一个字母的开始
		;
	if(c != EOF)
		*w++ = c;
	if(!isalpha(c)) //判断字符变量c是否为字母
	{
		*w = '\0';
		return c;
	}
	for(; --lim > 0; w++)
		if(!isalnum(*w = getchar())) //判断字符变量c是否为字母或数字
			break;

	*w = '\0';
	return word[0];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值