主题:帮帮我吧 各位大姐大哥
[em10] 各位好 这是我照书编的赫夫曼树的构造和求出它的编码 但是有问题 请帮忙看贴的各位哥哥姐姐弟弟妹妹看一下我错在哪里啊? 请指出来 帮我改正一下好吗? 我是学C语言的 用的是 turboc2 来做的 请我改正一下 谢谢各位拉 [em18]
#include<stdio.h>
#include<stdlib.h>
typedef struct
{ unsigned int weight;
unsigned int parent,lchild,rchild; }
HTNode,*HuffmanTree;
typedef char * *HuffmanCode;
void Huffmancoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n)
{ 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={*W,0,0,0};
for(;i<=m;++i,++p)
*p={0,0,0,0};
for(i=n+1;i<=m;++i)
{ Select(HT,i-1,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); }
int main()
{ HTNode *HT,*HC;
HuffmanCoding();}
#include<stdio.h>
#include<stdlib.h>
typedef struct
{ unsigned int weight;
unsigned int parent,lchild,rchild; }
HTNode,*HuffmanTree;
typedef char * *HuffmanCode;
void Huffmancoding(HuffmanTree &HT,HuffmanCode &HC,int *w,int n)
{ 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={*W,0,0,0};
for(;i<=m;++i,++p)
*p={0,0,0,0};
for(i=n+1;i<=m;++i)
{ Select(HT,i-1,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); }
int main()
{ HTNode *HT,*HC;
HuffmanCoding();}