/*************************统计各字符出现的频度***********************/
void frequency(){
struct tree *r,*p,*q;
int n,l,j=1;
/*录入第一个字符并创建链表*/
clrscr(); /*清屏*/
head=NULL;
printf("Input the text end of ENTER: ");
n=get);
if(n!= ){
p=(struct tree*)malloc(sizeof(struct tree));
p->data=n;
p->m=1;
p->sign=-1;
p->lchild=NULL;
p->rchild=NULL;
p->parent=NULL;
head=p;
ps[0]=p; /*把字符(后面的叶节点)放进数组备份*/
n=get);
}
/*录入其它字符*/
while(n!= ){
l=0;r=p;
for(q=head;q!=NULL&&l==0;q=q->parent){
if(q->data==n) { /*检查是否和已经录入的字符相同*/
q->m++; /*如果相同则此字符的频度变量加1*/
l=1;
}
r=p;
}
if(l==0){ /*如果不同则录入并加入链表*/
p=(struct tree*)malloc(sizeof(struct tree));
p->data=n;
p->m=1;
p->sign=-1;
p->lchild=NULL;
p->rchild=NULL;
p->parent=NULL;
r->parent=p;
ps[j]=p; /*把字符(后面的叶节点)放进数组备份*/
j++;
}
n=get);
}
/*输出字符的频度*/
p=head;
size=0;
printf(" Frequency as follows: characters frequency ");
while(p!=NULL){
printf("%c %d ",p->data,p->m);
p=p->parent;
size++; /*统计字符的个数*/
}
}
/****************************生成树**********************************/
void ctree(){
struct tree *t,*r,*p,*e,*q;
int n;
/****给链表排序生成频度递增链表*****/
for(p=head;p->parent!=NULL;p=p->parent){
for(q=p->parent;q!=NULL;q=q->parent){
if(p->m>q->m){
n=q->m; /*交换信息*/
q->m=p->m;
p->m=n;
n=q->data;
q->data=p->data;
p->data=n;
}
}
}
/***********生成哈夫曼树***********/
p=head;
while(p!=NULL&&p->parent!=NULL){
/*取递增链表前两个为左右儿子生成哈夫曼树*/
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-35162-2.html
哈哈哈哈
规模不定