合并果子——哈夫曼树的运用

合并果子
描述
在一个果园里,多多已经将所有的果子打了下来,而且按果子的不同种类分成了不同的堆。多多决定把所有的果子合成一堆。
每一次合并,多多可以把两堆果子合并到一起,消耗的体力等于两堆果子的重量之和。可以看出,所有的果子经过n-1次合并之后,就只剩下一堆了。多多在合并果子时总共消耗的体力等于每次合并所耗体力之和。
因为还要花大力气把这些果子搬回家,所以多多在合并果子时要尽可能地节省体力。假定每个果子重量都为1,并且已知果子的种类数和每种果子的数目,你的任务是设计出合并的次序方案,使多多耗费的体力最少,并输出这个最小的体力耗费值。
例如有3种果子,数目依次为1,2,9。可以先将1、2堆合并,新堆数目为3,耗费体力为3。接着,将新堆与原先的第三堆合并,又得到新的堆,数目为12,耗费体力为12。所以多多总共耗费体力=3+12=15。可以证明15为最小的体力耗费值。

//在熟悉哈夫曼树的前提下,我们很容易就能根据题意来得出,本题所用的方法为哈夫曼树。

直接上代码:

#include <iostream>
#include <string.h>
using namespace std;
#define max 100
//define node hufman tree
struct HTNode
{
    int m_parent;
    int m_lchild;
    int m_rchild;
    int m_weight;
    
};
//define huffman tree
struct HTree{
    HTNode* m_Htree;
    int m_length;//长度 
}; 

//init function初始化 
void initHTree(HTree &HT,int num)//导入对象和叶子结点
{
    int totalNode=2*num-1;
    HT.m_Htree=new HTNode[totalNode];
    if(!HT.m_Htree)    {
        cout<<"fial"<<endl;
    }    
    HT.m_length=0;
    int i;
    for(i=0;i<num;i++)
    {
        HT.m_Htree[i].m_parent=-1;
        HT.m_Htree[i].m_lchild=-1;
        HT.m_Htree[i].m_rchild=-1;
        //cout<<"pls input the weight of "<<i<<endl;
        int weight;
        cin>>weight;
        HT.m_Htree[i].m_weight=weight;
        HT.m_length++;
    }
    for(;i<totalNode;i++)
    {
        HT.m_Htree[i].m_parent=-1;
        HT.m_Htree[i].m_lchild=-1;
        HT.m_Htree[i].m_rchild=-1;
        HT.m_Htree[i].m_weight=9999;
        HT.m_length++;    
    }

//fine two min node function
void findTwoMinNodeofTree(HTree &HT,int pos,int &min1,int &min2)
{
    int i=0;
    int m1,m2;
    int minweight;
    //find min1
    while(HT.m_Htree[i].m_parent!=-1)
    {
        i++;
    }
    minweight=HT.m_Htree[i].m_weight;
    m1=i;
    for(;i<pos;i++)
    {
        if(HT.m_Htree[i].m_weight<minweight&&HT.m_Htree[i].m_parent==-1)
        {
            minweight=HT.m_Htree[i].m_weight;
            m1=i;
        }
    }
    HT.m_Htree[m1].m_parent=1;/ange the node state to not root
    min1=m1;
    //find min2
    i=0;
    while(HT.m_Htree[i].m_parent!=-1)
    {
        i++;
    }
    minweight=HT.m_Htree[i].m_weight;
    m2=i;
    for(;i<pos;i++)
    {
        if(HT.m_Htree[i].m_weight<minweight&&HT.m_Htree[i].m_parent==-1)
        {
            minweight=HT.m_Htree[i].m_weight;
            m2=i;
        }
    }
    HT.m_Htree[m2].m_parent=1;/ange the node state to not root
    min2=m2;
}


//fuction of create huffman
void creatHuffmanTree(HTree &HT,int num)
{
    if(!HT.m_Htree)
    {
        cout<<"the tree isnot exist"<<endl;
        
    }
    int i,min1,min2,all=0;
    for(i=num;i<HT.m_length;i++)
    {
        findTwoMinNodeofTree(HT,i,min1,min2);
        HT.m_Htree[min1].m_parent=i;
        HT.m_Htree[min2].m_parent=i;
        HT.m_Htree[i].m_lchild=min1;
        HT.m_Htree[i].m_rchild=min2;
        HT.m_Htree[i].m_weight=HT.m_Htree[min1].m_weight+HT.m_Htree[min2].m_weight;
        all+=HT.m_Htree[i].m_weight;
    }
        cout<<all<<endl;
}

/Pst the huffmantree
void showTree(HTree &HT)
{
    if(HT.m_Htree!=NULL)
    {
        //cout<<"show the node of huffmantree"<<endl;
        for(int i=0;i<HT.m_length;i++)
        {
        //    cout<<HT.m_Htree[i].m_parent<<endl;
        //    cout<<HT.m_Htree[i].m_lchild<<endl;
        //    cout<<HT.m_Htree[i].m_rchild<<endl;
            cout<<HT.m_Htree[i].m_weight<<endl;
        }
    }
}
void test01()
{
    //cout<<"pls input the num of the leaf node"<<endl;//输入叶节点数
    int num;
    cin>>num;
    HTree mytree;
    initHTree(mytree,num);//初始化 
    creatHuffmanTree(mytree,num);
}
int main()
{
    test01();
    return 0;
        
}

//代码来源于网络,稍作了修改,仅供参考。

由于最后的结果需要的是非叶子结点的和,所以并没有输出结点的其他属性,如需输出请自行修改。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值