救急,高分相赠~~怎么用二叉链表结构建立赫夫曼树??谢谢了~~
统计了文本中字符出现的次数,
代码如下,已编译通过的
#include<stdio.h>
#include<string.h>
#define MAX 1000
struct node{
char letter;
int weight;
struct node *next;
};
int n;
struct node *head;
main()
{
struct node *counting(void);
head=counting();
}
struct node *counting(void)
{
struct node *p=NULL,*m=NULL;
int j=0,k=0,i=0;
char a[MAX];
printf("please input the text end of enter:");
scanf("%s",a);
m=p=(struct node *)malloc(sizeof(struct node));
for(i=0;a[i]!='\0';++i)
{
k=0;
for(j=0;a[j]!='\0';++j)
{
while(j<i){
if(a[j]!=a[i]) /*判断该字符a[i]是否已经统计过*/
++j;
else
{
if(a[++i]=='\0') /*判断是否越界*/
goto out;
j=0;
}
}
if(a[j]==a[i])
++k;
}
p->next=(struct node *)malloc(sizeof(struct node));
p=p->next;
p->letter=a[i];
p->weight=k;
p->next=NULL;
++n;
}
out:m=m->next; /*输出*/
p=m;
i=0;
while(m!=NULL){
printf("%6c%6d",m->letter,m->weight);
m=m->next;
++i;
if(i==4)
{
printf("\n");
i=0;
}
}
printf("\n");
return(p);
}
然后怎么建成赫夫曼树 啊,给个简单点的算法吧(可以是代码),谢谢了~~~~