小弟写的huffman函数,运行中却意外中断了,找不出原因,还望帮忙
void Huffmancoding(Huffmantree &HT,Huffmancode &HC,int n){
int s1, s2,f,c;
Huffmantree p;
int m,i,start;
char *cd;
if (n<=1)
printf("ERROR");
m=2*n-1;
HT=(Huffmantree)malloc((m+1)*sizeof(Htnode));
for (p=HT,i=1;i<=n;++i,++p)
{
fflush(stdin);
printf ("请按顺序输入字符信息及对应权重:\n");
scanf ("%c",&(*(HT+i)).letter);
scanf ("%d",&(*(HT+i)).weight);
(*(HT+i)).parent=0;
(*(HT+i)).lchild=0;
(*(HT+i)).rchild=0;
}
for (;i<=m;++i,++p)
{
(*(HT+i)).weight=0;
(*(HT+i)).parent=0;
(*(HT+i)).lchild=0;
(*(HT+i)).rchild=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[c].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);
}
}