haffuman 编码c++实现

来源:互联网 发布:神州泰岳 知乎 编辑:程序博客网 时间:2024/04/25 23:16
为了考研很久没有搞技术了,现在海大考上已经很多天了,无意看到以前考研上机做的考研真题,顺便就粘贴上来冒个泡.
#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和rightHaffumanNode::~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 0