谁帮忙改一下这个程序谢了
#include<iostream>using namespace std;
#define maxbit 10
#define maxvalue 1000
#define maxleaf 4
#define maxnode maxleaf*2-1
int n;
typedef struct
{
int weight;
int parent;
int lchild;
int rchild;
}hnodetype;
typedef struct
{
int bit[maxbit];
int start;
}hcodetype;
hnodetype *haffmantree()//建立哈夫曼树
{
hnodetype huffnode[maxnode];
int i,j,m1,m2,x1,x2;
cout<<"请输入叶子结点数:";
cin>>n;
for(i=0;i<2*n-1;i++)
{
huffnode[i].weight=0;
huffnode[i].parent=-1;
huffnode[i].lchild=-1;
huffnode[i].rchild=-1;
}
cout<<"请输入各叶子结点的权重值:";
for(i=0;i<n;i++)
cin>>huffnode[i].weight;
for(i=0;i<n-1;i++)
{
m1=m2=maxvalue;
x1=x2=0;
for(j=0;j<n+i;j++)
{
if(huffnode[j].parent==-1&&huffnode[j].weight<m1)
{
m2=m1;
x2=x1;
m1=huffnode[j].weight;
x1=j;
}
else
if(huffnode[j].parent==-1&&huffnode[j].weight<m2)
{
m2=huffnode[j].weight;
x2=j;
}
}
huffnode[x1].parent=n+1;
huffnode[x2].parent=n+1;
huffnode[n+1].weight=huffnode[x1].weight+huffnode[x2].weight;
huffnode[n+1].lchild=x1;
huffnode[n+2].rchild=x2;
}
return huffnode;
}
void haffmancode()
{
hnodetype *node;
hcodetype huffcode[maxleaf],cd;
int i,j,c,p;
node=haffmantree();
for(i=0;i<n;i++)
{
cd.start=n-1;
c=i;
p=node[c].parent;
while(p!=-1)
{
if(node[p].lchild==c)
cd.bit[cd.start]=0;
else
cd.bit[cd.start]=1;
cd.start--;
c=p;
p=node[c].parent;
}
for(j=cd.start;j<n;j++)
huffcode[i].bit[j]=cd.bit[j];
huffcode[i].start=cd.start;
}
for(i=0;i<n;i++)
{
for(j=huffcode[i].start+1;j<n;j++)
cout<<huffcode[i].bit[j];
cout<<endl;
}
}
int main()
{
haffmancode();
return 0;
}
我就是想实现哈夫曼编码,程序似乎没什么问题,但就是运行不正确……那位高手帮一下……谢谢了