求救高手帮忙谢谢
原程序为:
#include<stdio.h>
#include<iostream.h>
#define MAX 21
typedef struct{
char data;
int weight;
int parent,lchild,rchild;
}huffnode;
typedef struct
{
char cd[MAX];
int start;
}huffcode;
void main()
{
huffnode ht[2*MAX];
huffcode hcd[MAX],d;
int i, k,f ,l,r, n,c,m1,m2;
printf("元素个数:");
scanf("%d",n);
for(i=1;i<=n;i++)
{
cout<<"第”<<i<<”个元素=>\n\t结点值:";
cin>>ht[i].data;
cout<<"\t权重:";
cin>>ht[i].weight;
}
for(i=1;i<=2*n-1;i++)
ht[i].parent=ht[i].lchild=ht[i].rchild=0;
for(i=n+1;i<=2*n-1;i++)
{
m1=m2=32767;
l=r=0;
for(k=1;k<=i-1;k++)
if(ht[k].parent==0)
if(ht[k].weight<m1)
{
m2=m1;
r=1;
m1=ht[k].weight;
l=k;
}
else if(ht[k].weight<m2)
{
m2=ht[k].weight;
r=k;
}
ht[l].parent=i;
ht[r].parent=i;
ht[i].weight=ht[l].weight+ht[r].weight;
ht[i].lchild=l;
ht[i].rchild=r;
}
for(i=1;i<=n;i++)
{
d.start=n+1;
c=i;
f=ht[i].parent;
while(f!=0)
{
if(ht[f].lchild==c)
d.cd[--d.start]='0';
else d.cd[--d.start]='1';
c=f;
f=ht[f].parent;
}
hcd[i]=d;
}
cout<<"输出哈夫曼编码:\n";
for(i=1;i<=n;i++)
{
cout<<ht[i].data;
for(k=hcd[i].start;k<=n;k++)
cout<<hcd[i].cd[k];
cout<<endl;
}
}