创建一个哈夫曼树

#include<iostream>
#include<cstring>
using namespace std;
struct HuffmanNode
{
    
    
    int weight;
    int Lchild,Rchild;
    int parent;
};
void select(HuffmanNode *HT,int i,int &s1,int &s2)//从HT的1到i中找出parent!=0的最小两个,将下标赋值给s1,s2
{
    
    
    int a,min1=0,min2=0;
    for(a=1;a<=i;a++)
    {
    
    
        if(HT[a].parent==0&&min1==0||HT[a].parent==0&&min2==0)
        {
    
    
            if(HT[a].parent==0&&min1==0)min1=a;
            else if(HT[a].parent==0&&min2==0)min2=a;
        }
        else if(HT[a].parent==0&&HT[a].weight<HT[min1].weight||HT[a].parent==0&&HT[a].weight<HT[min2].weight)
            if(HT[a].weight<HT[min1].weight&&HT[min1].weight>HT[min2].weight)
                min1=a;
                else if(HT[a].weight<HT[min1].weight&&HT[min1].weight<HT[min2].weight)
                {
    
    
                    min2=min1;
                    min1=a;
                }
                else if(HT[a].weight<HT[min2].weight&&HT[min2].weight>HT[min1].weight)
                    min2=a;
                else if(HT[a].weight<HT[min2].weight&&HT[min1].weight>HT[min2].weight)
                {
    
    
                    min1=min2;
                    min2=a;
                }
    }
    s1=min1;s2=min2;
}
void CreateHuffmanTree(HuffmanNode* &HT,int n)//创建一个有N个叶子节点的哈夫曼树
{
    
    
    if(n<=1) return ;
    int m=2*n-1,i,s1,s2;
    HT=new HuffmanNode[m+1];
    for(i=1;i<=m;i++)
    {
    
    
        HT[i].Lchild=0;
        HT[i].parent=0;
        HT[i].Rchild=0;
    }
    cout<<"请输入各节点的权值,以空格间隔。"<<endl;
    for(i=1;i<n+1;i++)
        cin>>HT[i].weight;
    for(i=n+1;i<=m;i++)
    {
    
    
        select(HT,i-1,s1,s2);
        HT[i].Lchild=s1;
        HT[i].Rchild=s2;
        HT[s1].parent=i;
        HT[s2].parent=i;
        HT[i].weight=HT[s1].weight+HT[s2].weight;
    }
}
int main()
{
    
    
    int n;
    cout<<"请输入哈夫曼树的叶子节点个数:"<<endl;
    cin>>n;
    HuffmanNode *HT;
    CreateHuffmanTree(HT,n);
    int i;
    cout<<"哈夫曼树为:"<<endl;
    for(i=1;i<=2*n-1;i++)
        cout<<i<<"  "<<HT[i].weight<<"  "<<HT[i].parent<<"  "<<HT[i].Lchild<<"  "<<HT[i].Rchild<<endl;
}

{
int n;
cout<<“请输入哈夫曼树的叶子节点个数:”<<endl;
cin>>n;
HuffmanNode HT;
CreateHuffmanTree(HT,n);
int i;
cout<<“哈夫曼树为:”<<endl;
for(i=1;i<=2
n-1;i++)
cout<<i<<" “<<HT[i].weight<<” “<<HT[i].parent<<” “<<HT[i].Lchild<<” "<<HT[i].Rchild<<endl;
}
手机上好像看不到main函数内的内容,我把他补在后面

猜你喜欢

转载自blog.csdn.net/m0_45972156/article/details/107781073