哈夫曼编码和译码

试题描述:输入为:一段英文或中文的文章(原文),对输入的文章构造哈夫曼树,生成对应的编码,输出为:原文所对应的编码(译文),根据已经生成的编码表,输入任意的译文可以得到对应的原文。要求有运行结果截图。

#include <bits/stdc++.h>
using namespace std;
typedef struct point
{
    int count;
    char c;
    int left;        //方便确定01编码
    int right;    
    int parent;    //方便回溯
    int loc;     //当前在队列中位置
}Huf;
typedef struct node{
    Huf array[200];
    int capacity;
    int size;
}tree;
tree createHeap(){    //创建最小堆
    tree tmp;
    tmp.array[0].count=-0xfff;   //哨兵
    tmp.capacity=199;
    tmp.size=1;
    return tmp;
}
tree insertHeap(tree tmp,Huf node){  //插入
    int t=tmp.size++;
    if (t>tmp.capacity)  return tmp; //判断堆是否还有空余
    while(tmp.array[t/2].count>node.count){     //向上调整
        tmp.array[t]=tmp.array[t/2];
        t=t/2;
    }
    tmp.array[t]=node;
    return tmp;
}
Huf getMin(tree tmp){
    return tmp.array[1];
}
tree deleteHeap(tree tmp){       //删除最小值
    if(tmp.size==1) return tmp;   //判断是否空堆
    Huf element=tmp.array[--tmp.size];
    int i,Child;
    for(i=1;i*2<tmp.size;i=Child){
        Child=i*2;
        if(Child!=tmp.size&&tmp.array[Child].count>tmp.array[Child+1].count) Child++;
        if(element.count>tmp.array[Child].count) tmp.array[i]=tmp.array[Child];
        else break;
    }
    tmp.array[i]=element;
    return tmp;
}
void EnCode(vector<Huf> HuffMan,char c){
    Huf tmp;
    stack<int> Q;
    for (int i=0;i<HuffMan.size();i++)
    {
        if (HuffMan[i].c==c)
        {
            tmp=HuffMan[i];
            break;
        }
    }
    while(tmp.parent!=-1) {
        if(HuffMan[tmp.parent].left==tmp.loc)  Q.push(1);
        else Q.push(0);
        tmp=HuffMan[tmp.parent];
    }
    while(!Q.empty()) {
            cout<<Q.top();
            Q.pop();
        }
}
void DeCode(vector<Huf> HuffMan,string c){
    Huf tmp=HuffMan.back();
    for(int i=0;i<c.length();i++){
        if (c[i]=='1')
        {
            tmp=HuffMan[tmp.left];
        }else if(c[i]=='0'){
            tmp=HuffMan[tmp.right];
        }
        if(tmp.left==-1) {
            cout<<tmp.c;
            tmp=HuffMan.back();
        }
    }
}
int main(int argc, char const *argv[])
{
    Huf tmp1,tmp2;
    cout<<endl;
    tree MinHeap=createHeap();
    string article;
    cout<<"请输入文本:";
    getline(cin,article);
    vector<Huf> HuffMan;
    map<char,int> Map;
    for (int i = 0; i < article.length(); ++i)    Map[article[i]]++; //计数
    for(map<char,int>::iterator iter=Map.begin();iter!=Map.end();iter++){
        Huf tmp;
        tmp.c=iter->first;
        tmp.count=iter->second;
        tmp.parent=-1;
        tmp.left=-1;
        tmp.right=-1;
        tmp.loc=HuffMan.size();
        MinHeap=insertHeap(MinHeap,tmp);
        HuffMan.push_back(tmp);
    }
    while(MinHeap.size!=2){
        tmp1=getMin(MinHeap);
        MinHeap=deleteHeap(MinHeap);
        tmp2=getMin(MinHeap);
        MinHeap=deleteHeap(MinHeap);
        Huf tmp;
        tmp.count=tmp1.count+tmp2.count;
        tmp.left=tmp1.loc;
        tmp.right=tmp2.loc;
        tmp.parent=-1;
        tmp.loc=HuffMan.size();
        HuffMan[tmp1.loc].parent=tmp.loc;
        HuffMan[tmp2.loc].parent=tmp.loc;
        MinHeap=insertHeap(MinHeap,tmp);
        HuffMan.push_back(tmp);
    }

    cout<<endl<<"编码已完成!"<<endl;
    for (int i = 0; i < article.length(); ++i)
    {
        EnCode(HuffMan,article[i]);
    }
    cout<<endl<<"请输入需要解码的文本"<<endl;
    getline(cin,article);
    cout<<"解码完成!"<<endl;
    DeCode(HuffMan,article);
    return 0;
}
  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值