haffuman 编码c++实现

为了考研很久没有搞技术了,现在海大考上已经很多天了,无意看到以前考研上机做的考研真题,顺便就粘贴上来冒个泡.
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std ;


class HaffumanNode
{
public:
    int wight ; //权值
    char ch ;
    HaffumanNode *left ;
    HaffumanNode *right ;

    HaffumanNode(char _ch,int _wight,HaffumanNode *_left = NULL,HaffumanNode *_right = NULL) ;
    HaffumanNode(const HaffumanNode &haffuman) ;
    ~HaffumanNode() ;
    bool isLeaf() ;

} ;

HaffumanNode::HaffumanNode(const HaffumanNode &haffuman) 
{
    wight = haffuman.wight ;
    ch = haffuman.ch ;
    left = haffuman.left ;
    right = haffuman.right ;
}

HaffumanNode::HaffumanNode(char _ch,int _wight,HaffumanNode *_left,HaffumanNode *_right)
{
    ch = _ch ;
    wight = _wight ;
    left = _left ;
    right = _right ;
}

//处理left和right
HaffumanNode::~HaffumanNode()
{

}

//使用权重作为比较函数的参考值,按降序排
bool HaffumanCompare(const HaffumanNode &left,const HaffumanNode &right)
{
    return (left.wight > right.wight) ;
}

//判断本节点是叶子节点
bool HaffumanNode::isLeaf()
{
    return (left == NULL && right == NULL) ;
}

vector<HaffumanNode> ListHaffuman ;  //haffuman源处理序列

//构建哈夫曼源序列
void GetHaffumanNodeList(string str)
{
    int strLen ;
    int i ;
    vector<HaffumanNode>::iterator it ;
    HaffumanNode *p ;
    strLen = str.length() ;

    for(i = 0 ; i < strLen ;i++)
    {
        for(it = ListHaffuman.begin() ; it != ListHaffuman.end() ; it++)
        {
            if(it->ch == str[i])  //如果存在了这个字符,权值+1
            {
                it->wight++ ;
                break ;
            }
        }
        if(it == ListHaffuman.end())  //如果这个字符没有存在,加入容器
        {
            p = new HaffumanNode(str[i],1) ;
            ListHaffuman.push_back(*p) ;
        }
    }
}

/*显示哈夫曼源字符和权重*/
void ShowListHaffuman(void)
{
    vector<HaffumanNode>::iterator it ;
    cout<<"字符"<<'\t'<<"权重"<<endl ;
    for(it = ListHaffuman.begin() ;it != ListHaffuman.end() ; it++)
    {
        cout<<it->ch<<'\t'<<it->wight<<endl ;
    }
    cout<<endl ;
}

/*打印各叶子节点的编码*/
void PrintLeafCode(HaffumanNode &tree,string bincode)
{
    //中序遍历
    if(tree.left != NULL)
    {
        bincode += '0' ;
        PrintLeafCode(*tree.left,bincode) ;
        bincode = bincode.substr(0,bincode.length() - 1) ;
    }
    if(tree.isLeaf())
    {
        cout<<tree.ch<<":     "<<bincode.c_str()<<endl ;
    }
    if(tree.right != NULL)
    {
        bincode += '1' ;
        PrintLeafCode(*tree.right,bincode) ;
        bincode = bincode.substr(0,bincode.length() - 1) ;
    }

}

/*生成哈夫曼树*/
void CreatHaffumanTree(vector<HaffumanNode> &ListHaffuman)
{
    int vecSize ;
    while((vecSize = ListHaffuman.size()) > 1) //如果源序列存在1个以上的节点,则继续构造哈夫曼树
    {
        sort(ListHaffuman.begin(),ListHaffuman.end(),HaffumanCompare) ;  //按降序排

        HaffumanNode *Leftnode = new HaffumanNode( ListHaffuman[vecSize - 2]) ;  //取出最后两个小的
        HaffumanNode *Rightnode = new HaffumanNode(ListHaffuman[vecSize - 1]) ;

        ListHaffuman.pop_back() ;  //删除取出后的两个小的
        ListHaffuman.pop_back() ;

        if(Leftnode->wight > Rightnode->wight) //使左节点永远比由节点的权重小
        {
            swap(*Leftnode,*Rightnode) ;
        }
        //构造父亲节点
        HaffumanNode *father = new HaffumanNode('*',(Leftnode->wight + Rightnode->wight),Leftnode,Rightnode) ;
        ListHaffuman.push_back(*father) ; //将父节点放入源序列
    }

    //已经完成哈夫曼树的构建

    string bincode ;
    HaffumanNode *tree = new HaffumanNode(ListHaffuman.front()) ; 
    PrintLeafCode(*tree,bincode) ; //遍历哈夫曼树
}



int main()
{
    char str[50] ;

    gets(str) ;
    GetHaffumanNodeList(str) ;  //创建haffuman源序列
    sort(ListHaffuman.begin(),ListHaffuman.end(),HaffumanCompare) ;  //降序排列
    ShowListHaffuman() ;  //显示字符和权重
    cout<<endl ;
    cout<<endl ;

    CreatHaffumanTree(ListHaffuman) ;
    return 0 ;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nicehuai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值