哈夫曼编码(备忘)

#include <iostream>
#include <cstdio>
#include <string>
#include <queue>
#include<functional>
#include <vector>
#include <map>

using namespace std;

const int INF=70;
char record[INF];
int weight[INF];
map<char,string>mp;

class NodeTree                  //存储节点信息
{
public:
    NodeTree(char s='#',int w=-1):str(s),weight(w)  //构造函数
    {
        lchild=NULL; rchild=NULL;
    }

    bool LessThan(const NodeTree* temp)const  //比较函数
    {
        if(weight!=temp->weight)
            return weight>temp->weight;
        else
            return str>temp->str;
    }

    char GetStr(){return str;}
    int GetWeight(){return weight;}
    char GetBinary(){return binary;}
    NodeTree* Getlchild(){return lchild;}
    NodeTree* Getrchild(){return rchild;}

    void SetBinary(char s){binary=s;}
    void SetWeight(int s){weight=s;}
    void Setlchild(NodeTree *node){lchild=node;}
    void Setrchild(NodeTree *node){rchild=node;}

private:
    char str;                     //记录字符
    int weight;                  //记录权值
    char binary;                  //记录在哈夫曼树上的编码
    NodeTree *lchild,*rchild;   //记录该节点的左右孩子节点
    friend class Compare;
};
class Compare
{
public:
    bool operator()(const NodeTree* temp1,const NodeTree* temp2)
    {
        return temp1->LessThan(temp2);
    }
};
void Input(int n)    //输入处理
{
    for(int i=0;i<n;i++)
    {
        getchar();
        scanf("%c%*c%d",&record[i],&weight[i]);
    }
}
NodeTree* CreateHaffmanTree(int n)            //构造哈夫曼树
{
    priority_queue<NodeTree*,vector<NodeTree*>,Compare>nodetree;
    for(int i=0;i<n;i++)  //存储信息
    {
        NodeTree *temp=new NodeTree(record[i],weight[i]);
        nodetree.push(temp);
    }
    while(nodetree.size()>1)
    {//cout<<"  hello"<<endl;
        NodeTree *temp1=nodetree.top();
        nodetree.pop();
        NodeTree *temp2=nodetree.top();
        nodetree.pop();

        NodeTree *temp=new NodeTree();

        temp->SetWeight(temp1->GetWeight()+temp2->GetWeight());
        temp1->SetBinary('0'); temp2->SetBinary('1');
        temp->Setlchild(temp1); temp->Setrchild(temp2);
        nodetree.push(temp);
    }
    return nodetree.top();
}

void dfs(char temp[INF],NodeTree* np,int cnt)  //确定编码
{
    if(np->Getlchild()==NULL && np->Getrchild()==NULL)
    {
        temp[cnt-1]=np->GetBinary();
        temp[cnt]='\0';
        string t(temp);
        mp[np->GetStr()]=t;
        return;
    }
    if(cnt) temp[cnt-1]=np->GetBinary();
    dfs(temp,np->Getlchild(),cnt+1);
    dfs(temp,np->Getrchild(),cnt+1);
}

void Output(int n)  //输出处理
{
    for(int i=0;i<n;i++)
    {
        printf("%c:",record[i]);
        cout<<mp[record[i]]<<endl;
    }
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        Input(n);
        NodeTree *root=CreateHaffmanTree(n);
        char temp[INF];
        dfs(temp,root,0);
        Output(n);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值