24小时热门版块排行榜    

北京石油化工学院2026年研究生招生接收调剂公告
查看: 430  |  回复: 2

-谁主沉浮

新虫 (小有名气)

[求助] 哈夫曼树纠错!急 哪里运行出问题。。。

哈夫曼树纠错!急 哪里运行出问题。。。
CODE:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define MAX 100
typedef struct
{
int weight;
int parent,lchild,rchild;
}HTNode,*HuffmanTree;
typedef char **HuffmanCode;
void Select(HuffmanTree HT,int n,int &s1,int &s2)
{
    int small1,small2;
    s1=s2=0;small1=small2=MAX;
for(int i=1;i<=n;++i)
{
  if(HT[i].parent==0)
  {
   if(HT[i].weight<small1)
   {
    small2=small1;
    small1=HT[i].weight;
    s2=s1; s1=i;
   }
   else
    if(HT[i].weight<small2)
    {
     small2=HT[i].weight;
     s2=i;
    }
  }
}
}
void HuffmanCoding(HuffmanTree &HT,HuffmanCode &HC,int*w,int n)
{
int m,s1,s2,i,c,start,f;
char *cd;
HuffmanTree p;
if(n<=1)
  return;
m=2*n-1;
HT=(HuffmanTree)malloc((m+1)*sizeof(HTNode));
for(p=HT+1,i=1;i<=n;++i,++p,++w)
{
  p->weight=*w;
  p->lchild=0;
  p->parent=0;
  p->rchild=0;
}
for(;i<=m;++i,++p)
{
  p->weight=0;
  p->lchild=0;
  p->parent=0;
  p->rchild=0;
}
for(i=n+1;i<=m;++i)
{
  Select(HT,i-1,s1,s2);
  printf("%d  %d\n",s1,s2);
  HT[s1].parent=i;
  HT[s2].parent=i;
  HT[i].lchild=s1;
  HT[i].rchild=s2;
  HT[i].weight=HT[s1].weight+HT[s2].weight;
}
HC=(HuffmanCode)malloc((n+1)*sizeof(char*));
cd=(char*)malloc(n*sizeof(char));
cd[n-1]='\0';
for(i=1;i<=n;++i)
{
  start=n-1;
  for(c=i,f=HT[i].parent;f!=0;c=f,f=HT[f].parent)
  {
   if(HT[f].lchild==c)
    cd[--start]='0';
   else
    cd[--start]='1';
   HC[i]=(char*)malloc((n-start)*sizeof(char));
   strcpy(HC[i],&cd[start]);
  }
  free(cd);
}
}
void PrintCode(HuffmanCode HC,int n,char *ch)
{
for(int i=1;i<=n;i++)
{
  printf("字符%c的霍夫曼编码:",ch[i-1]);
  printf("%s\n",HC[i]);
}
}
void main()
{
HuffmanTree HT;
HuffmanCode HC;
int w[MAX],i,n;
char ch[MAX];
printf("请输入字符个数:\n");
scanf("%d",&n);
printf("请输入字符:\n");
for(i=0;i<n;i++)
  scanf("%c",&ch[i]);
printf("请输入字符对应权值:\n");
for(i=0;i<n;i++)
  scanf("%d",&w[i]);
HuffmanCoding(HT,HC,w,n);
PrintCode(HC,n,ch);
}

[ Last edited by jjdg on 2013-5-11 at 01:18 ]
回复此楼

» 猜你喜欢

已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

shyboylpf

新虫 (初入文坛)

jjdg: 编辑内容 2013-05-11 01:19
内容已删除
2楼2013-05-09 20:18:44
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

-谁主沉浮

新虫 (小有名气)

引用回帖:
2楼: Originally posted by shyboylpf at 2013-05-09 20:18:44
为毛还能加表情。。。

不是的啊,明明是括号,系统自动换成表情
3楼2013-05-10 12:15:48
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
相关版块跳转 我要订阅楼主 -谁主沉浮 的主题更新
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[考研] 一志愿华南师范大学-22408计算机-292分-求华南师范大学调剂 +4 爱读书的小鳄鱼 2026-04-02 4/200 2026-04-02 18:35 by 求调剂zz
[考研] 312 化工或制药调剂 +4 小小墨123 2026-04-02 5/250 2026-04-02 16:04 by 小touming
[考研] 材料调剂 +6 一样YWY 2026-04-02 6/300 2026-04-02 14:56 by liu823948201
[考研] 283求调剂 +3 jiouuu 2026-04-02 4/200 2026-04-02 14:08 by 哒哒哒呱呱呱
[考研] 262求调剂 +5 励志一定发文章 2026-04-02 6/300 2026-04-02 12:51 by yulian1987
[考研] 一志愿北京理工大学本科211材料工程294求调剂 +8 mikasa的围巾 2026-03-28 9/450 2026-04-02 12:09 by ms629
[考研] 302求调剂一志愿北航070300,本科郑大化学 +8 圣日耳曼条 2026-04-01 11/550 2026-04-02 07:40 by chemdavid
[考研] 304求调剂 +12 素年祭语 2026-03-31 15/750 2026-04-01 22:41 by peike
[考研] 求调剂推荐 材料 304 +21 荷包蛋hyj 2026-03-26 21/1050 2026-04-01 21:09 by lijunpoly
[考研] 一志愿西安交大材料学硕(英一数二)347,求调剂到高分子/材料相关专业 +7 zju51 2026-03-31 9/450 2026-04-01 19:35 by CFQZAFU
[考研] 332求调剂 +8 Lyy930824@ 2026-03-29 8/400 2026-04-01 18:40 by 千钧澄玉宇啊
[考研] 英一数二生物信息学287分,本科生物科学,求调剂 +3 碧水xyz 2026-03-29 4/200 2026-04-01 15:35 by plum
[考研] 291求调剂 +3 迷蒙木木 2026-04-01 4/200 2026-04-01 11:07 by 逆水乘风
[考研] 263求调剂 +3 DDDDuu 2026-03-27 3/150 2026-03-31 16:21 by 土木硕士招生
[考研] 286求调剂 +6 Faune 2026-03-30 6/300 2026-03-31 14:37 by jp9609
[考研] 08工科,295,接受跨专业调剂 +6 lmnlzy 2026-03-30 6/300 2026-03-31 10:04 by cal0306
[考研] 085601一志愿西北工业大学初试346 +4 085601初试346 2026-03-30 4/200 2026-03-31 07:47 by jp9609
[有机交流] 考研调剂 +8 watb 2026-03-26 8/400 2026-03-30 18:40 by 544594351
[考研] 求调剂,一志愿 南京航空航天大学大学 ,080500材料科学与工程学硕 +6 @taotao 2026-03-26 7/350 2026-03-30 10:43 by 我是小康
[考研] 265求调剂 +8 小木虫085600 2026-03-27 8/400 2026-03-27 22:16 by 无际的草原
信息提示
请填处理意见