C++构造哈夫曼树，运行时只能输入一个节点的初值，怎么回事，求指点

#include
#define Maxval 10000
using namespace std;
typedef char Datatype;
typedef struct
{
Datatype data;
float weight;
int lchild,rchild,parent;
}HufmanTree;

void ConstHufmanTree(HufmanTree HTree[],int n)
{
int p1,p2,i,j;
float small1,small2;
for(i=0;i<=2*n-1;i++)
{
HTree[i].weight=0.0;
HTree[i].data='0';
HTree[i].parent=0;
HTree[i].lchild=0;
HTree[i].rchild=0;
}//对所有2*n-1个节点进行赋初值；
for(i=0;i {
cin>>HTree[i].data;
cin>>HTree[i].weight;
} //对前n个节点的data和权值进行赋值；

for(i=n;i<2*n-1;i++)
{
p1=p2=0;
small1=small2=Maxval;
for(j=0;j<i;j++)
{
if(HTree[j].parent==0)
{
if(HTree[j].weight<small1)
{
small2=small1;
small1=HTree[j].weigh;
p2=p1;
p1=j;
}
else if(HTree[j].weight<small2)
{
small2=HTree[j].weight;
p2=j;
}
}

`````` }
HTree[p1].parent=i;
HTree[p2].parent=i;
HTree[i].lchild=p1;
HTree[i].rchild=p2;
HTree[i].weight=HTree[p1].weight+HTree[p2].weight;
``````

}
}

int main()
{
int n;
HufmanTree *HTree;
void ConstHufmanTree(HufmanTree HTree[],int n);
cout<<"please input the number of the leaf:"< cin>>n;

HTree=new HufmanTree[2*n-1];

ConstHufmanTree(HTree,n);

return 0;

}

1个回答