文本文件单词统计的排序树方式实现

现在提供一个排序树的应用。对文本文件的单词数量统计,源代码如下,在vc6.0环境测试通过

排序树的基础知识请参见《软件技术基础》电子科技大学出版社

#include "stdio.h"
#include "stdLib.h"
#include "string.h"
#include "assert.h"

#define MAXWORDLEN 30   /*单词最大字母数量*/

typedef struct sort_tree
{
 char word[MAXWORDLEN];
 int count;  
 struct sort_tree *left;
 struct sort_tree *right;
}BNODE;

BNODE *sortTree = NULL;
int cnt = 0;

void ins_Bstree(BNODE **proot,char * word);
BNODE * ser_Bstree(BNODE **proot,char *word,char *l_r);
void in_Order(BNODE * proot);

void main()
{
 FILE * pin;
 char temp[MAXWORDLEN] = {'/0'};
 char charIn;
 int i = 0;
 int wordFlag=0;
 
 pin = fopen("word.txt","r");
 if(pin == NULL)
 {
  printf("文件打开错误!!!/n");
  return;
 }
 
 while(!feof(pin))/*未到文件末*/
 {
  charIn = fgetc(pin);
  if((charIn >= 'A' && charIn <= 'Z')||(charIn >= 'a' && charIn <= 'z') )
  {
   if(i>=MAXWORDLEN)
    printf("文件中有超过30个字母的单词!!!");
   assert(!(i >= MAXWORDLEN));
   temp[i++] = charIn;
   wordFlag = 1;
  }
  else
  {
   if(wordFlag == 1)
   {
    ins_Bstree(&sortTree,temp); 
    memset(temp,'/0',MAXWORDLEN);
    i=0;
    wordFlag = 0; 
   }
  }
 }
 printf("   欢迎使用单词统计工具  /n");
       printf("   作者 malamute;   blog:   http://malamute.blog.sohu.com/")
 printf("   word                               count     /n");
 printf("-----------------------------------------------------/n");
 in_Order(sortTree);
}

/*根据ser_Bstree函数的结果来对最近读入单词进行计数或者插入操作*/
void ins_Bstree(BNODE ** proot,char * word)
{
 BNODE *p;
 char l_r = 0;
 BNODE * result;
 
 result = ser_Bstree(proot,word,&l_r);
 
 p = (BNODE *)malloc(sizeof(BNODE));
 if(p == NULL)
 {
  printf("p 分配错误!!!");
  return;
 }
 p->count=1;
 p->left = NULL;
 p->right = NULL;
 
 if(*proot == NULL)
 {
  *proot = p;
  strcpy((*proot)->word,word);
  
  // printf("cnt = %d/n",++cnt);
  return;
 }
 
 if(result != NULL)
 {
  switch(l_r)
  {
  case 0 :
   break;
  case 1 :
   result->left = p;
   strcpy(p->word,word);
   //printf("cnt = %d/n",++cnt);
   break;
  case 2 :
   result->right= p;
   strcpy(p->word,word);
   
   //printf("cnt = %d/n",++cnt);
   break;
  }
 }
 return;
}

/*在排序二叉树中查找是否有相同单词,若找到对应计数值加1,否则判断该放在当前排序二叉树叶子节点的左子树还是右子树*/
/*返回值:找到则返回此单词的节点*/
/*找不到则返回叶子节点,*/
/* l_r 表示需要添加到左子树还是右子树*/
BNODE * ser_Bstree(BNODE **proot,char *word,char *l_r)
{
 BNODE * p;
 //int endFlag = 0;
 
 if(*proot == NULL)
  return NULL;
 
 p = *proot;
 while(1)
 {
  if(strcmp(p->word,word) == 0) /*找到相同单词,计数值加1*/
  {
   p->count++;
   return p;   
  }
  else if(strcmp(p->word,word) > 0)
  {
   if(p->left == NULL)
   {
    *l_r =1;
    return p;
   }
   else p = p->left;
  }
  else if(strcmp(p->word,word) < 0)
  {
   if(p->right== NULL)
   {
    *l_r= 2;
    return p;
   }
   else p = p->right;
  }
 }
}

/*中序遍历排序二叉树*/
void in_Order(BNODE * proot)
{
 int len;

 if(proot == NULL)
  return;
 else
 {
  in_Order(proot->left);
  printf("%s",proot->word);
  len = MAXWORDLEN+10 - strlen(proot->word);
  while(len--)
   printf(" ");
  printf("%d/n",proot->count);
  in_Order(proot->right);
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值