救急,高分相赠~~怎么用二叉链表结构建立赫夫曼树??谢谢了~~

eagleqdy 2003-08-21 09:17:35
统计了文本中字符出现的次数,
代码如下,已编译通过的
#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);
}
然后怎么建成赫夫曼树 啊,给个简单点的算法吧(可以是代码),谢谢了~~~~
...全文
26 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
made_in_ 2003-08-22
  • 打赏
  • 举报
回复
找本数据结构的书吧,上面说的很详细的。
ZhangYv 2003-08-22
  • 打赏
  • 举报
回复
http://expert.csdn.net/Expert/topic/2059/2059607.xml?temp=.6348841 自己找
eagleqdy 2003-08-22
  • 打赏
  • 举报
回复
数组的不行啊,告诉我大概的算法也可以啊,谢谢了
cdxiaogan 2003-08-21
  • 打赏
  • 举报
回复
链表?
我只有数组的要不
链表我现在忙
懒得给你重写啊

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧