哈夫曼树里面的问题~
时间:2011-12-15
来源:互联网
typedef struct Node
{
int weight;
int parent,LChild,RChild;
}HTNode,* HTree;
typedef char * HuffmanCode;
void CreatHTree(HTree/*代表的是struct Node * 型,而不是struct Node */ ht,HuffmanCode hc,int * w,int n)
{//W存放n个权值,构造哈夫曼树 ht,并求出哈夫曼编码 hc
int start,i,m,s1,s2;
m=2*n-1;
ht=(HTree)malloc((m+1)* sizeof(HTNode));
for(i=1;i<=n;i++)
ht[i]={w[i],0,0,0};
for(i=n+1;i<=m;i++)ht[i]={0,0,0,0};//初始化
for(i=n+1;i<=m;i++)
{
select(ht,i-1,&s1,&s2);
//在 ht[1]~ht[i-1] 的范围内选择两个parent为0且weight最小的结点,其序号分别赋值给s1,s2返回,select函数要求
//在上机调试是补充定义
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 * sizeof(char));
cd[n-1]='\0';
for(i=1;i<=n;i++)
{
start=n-1;
for(c=i,p=ht[i].parent ;p!=0 ;c=p,p=ht[p].parent)
if(ht[p].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 select(HTree Sht,int n,int *S1,int *S2)
{
int i;
*S1=1;*S2=2;
for(i=2;i<=n;i++)
{
if(Sht[i].weight<Sht[*S1].weight)
{
*S2=*S1;*S1=i;
}
else if(Sht[i].weight<Sht[*S2].weight)
*S2=i;
}
}
select函数怎么弄啊,指针那些都乱掉了,,,谁帮我做一个select函数,结构要跟上面这个一样的啊,谢谢~~~
{
int weight;
int parent,LChild,RChild;
}HTNode,* HTree;
typedef char * HuffmanCode;
void CreatHTree(HTree/*代表的是struct Node * 型,而不是struct Node */ ht,HuffmanCode hc,int * w,int n)
{//W存放n个权值,构造哈夫曼树 ht,并求出哈夫曼编码 hc
int start,i,m,s1,s2;
m=2*n-1;
ht=(HTree)malloc((m+1)* sizeof(HTNode));
for(i=1;i<=n;i++)
ht[i]={w[i],0,0,0};
for(i=n+1;i<=m;i++)ht[i]={0,0,0,0};//初始化
for(i=n+1;i<=m;i++)
{
select(ht,i-1,&s1,&s2);
//在 ht[1]~ht[i-1] 的范围内选择两个parent为0且weight最小的结点,其序号分别赋值给s1,s2返回,select函数要求
//在上机调试是补充定义
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 * sizeof(char));
cd[n-1]='\0';
for(i=1;i<=n;i++)
{
start=n-1;
for(c=i,p=ht[i].parent ;p!=0 ;c=p,p=ht[p].parent)
if(ht[p].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 select(HTree Sht,int n,int *S1,int *S2)
{
int i;
*S1=1;*S2=2;
for(i=2;i<=n;i++)
{
if(Sht[i].weight<Sht[*S1].weight)
{
*S2=*S1;*S1=i;
}
else if(Sht[i].weight<Sht[*S2].weight)
*S2=i;
}
}
select函数怎么弄啊,指针那些都乱掉了,,,谁帮我做一个select函数,结构要跟上面这个一样的啊,谢谢~~~
作者: Q3277631 发布时间: 2011-12-15
分太少!
作者: W170532934 发布时间: 2011-12-15
还有啊,ht 不是 struct Node * 型的吗?那就应该是指针啊,为什么不是ht[s1]->parent而是ht[s1].parent?
作者: Q3277631 发布时间: 2011-12-15
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28