//编码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<fstream>
#include<map>
using namespace std; typedef struct HuffmanNode{
int w;//节点的权值
int ld, rd;//左右孩子节点
int p;//父节点
char ch;//当前节点的字符
HuffmanNode(){
ld = rd = p = -;
}
}huffmanNode, *pHuffmanNode; typedef pair<int, int> pii;//haffuman节点和它的编号 bool operator > (pii x, pii y){
return x.first > y.first;
} struct Huffman{
int cntNode;//总结点的个数
string orgCode;
string huffmanCode;
pHuffmanNode huffman = NULL;
priority_queue<pii, vector<pii>, greater<pii> >qNode;
map<string, char> huffmanMapx;//哈夫曼编码对应字符
map<char, string> huffmanMapy;//字符对应哈夫曼编码
void initHuffman(char *str){
orgCode = str;
cntNode = ;
int cnt[];//统计每一个节点的个数
memset(cnt, , sizeof(cnt));
for(int i=; str[i]; ++i){
if(cnt[str[i]]==) ++cntNode;
++cnt[str[i]];
}
huffman = new HuffmanNode[*(cntNode)];
int index = ;
for(int i=; i<; ++i){
if(cnt[i]!=){
huffman[index].w = cnt[i];
huffman[index].ch = i;
qNode.push(make_pair(huffman[index].w, index));
++index;
}
}
while(qNode.size()>=) {
pii ldPii = qNode.top();
qNode.pop();
pii rdPii = qNode.top();
qNode.pop();
huffman[index].w = ldPii.first + rdPii.first;
huffman[index].ld = ldPii.second;
huffman[index].rd = rdPii.second;
huffman[ldPii.second].p = index;
huffman[rdPii.second].p = index;
qNode.push(make_pair(huffman[index].w, index));
++index;
}
} void huffmanCoding() {
for(int i=; i<cntNode; ++i){//从每一个孩子节点向上寻找
string code = "";
for(int child=i, p=huffman[child].p; ~p; child = p, p = huffman[child].p) {
if(huffman[p].ld == child){//左子树
code += '';
} else if(huffman[p].rd == child){//右子树
code += '';
}
}
reverse(code.begin(), code.end());
huffmanMapx.insert(make_pair(code, huffman[i].ch));
huffmanMapy.insert(make_pair(huffman[i].ch, code));
}
} void outHuffmanTree(fstream &fout, int f){
if(huffman[f].ld==- && huffman[f].rd==-){
fout<<<<" ";
return ;
} else {
fout<<<<" ";
outHuffmanTree(fout, huffman[f].ld);
outHuffmanTree(fout, huffman[f].rd);
}
} void outHuffmanCode(){
huffmanCode = "";//存储字符串的哈夫曼编码之后的内容
fstream fout("out.txt", ios_base::out);
for(int i=; i<orgCode.length(); ++i){
huffmanCode += huffmanMapy[orgCode[i]];
}
cout<<huffmanCode<<endl;
fout<<huffmanCode<<endl;//想文件中输入huffman编码
int f = *cntNode-;//huffman树的父节点
outHuffmanTree(fout, f);
fout<<endl;
for(map<string, char>::iterator it = huffmanMapx.begin(); it!=huffmanMapx.end(); ++it){
cout<<it->first << " -> " << it->second<<endl;
fout<<it->first<<" "<<it->second<<endl; //向文件中输入HuffmanMap
}
}
}; int main(){
char str[];
Huffman huffman;
gets(str);
huffman.initHuffman(str);
huffman.huffmanCoding();
huffman.outHuffmanCode();
return ;
}
//译码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<fstream>
#include<map>
using namespace std; typedef struct HuffmanTreeNode{
HuffmanTreeNode *ld, *rd;
HuffmanTreeNode(){
ld = NULL;
rd = NULL;
}
} *pHuffmanTreeNode; struct Huffman{
pHuffmanTreeNode T;
string code;
map<string, char>huffmanMapx; void buildT(fstream &fin, pHuffmanTreeNode &T){
int w;
fin>>w;
T = new HuffmanTreeNode();
if(w==)
return ;
buildT(fin, T->ld);
buildT(fin, T->rd);
} void outT(pHuffmanTreeNode T){
if(T->ld != NULL){
cout<<<<endl;
outT(T->ld);
}
else return;
if(T->rd != NULL){
cout<<<<endl;
outT(T->rd);
}
} void initHuffmanTree(){
fstream fin("in.txt", ios_base::in);
T = NULL;
fin>>code;
buildT(fin, T);
//outT(T);
string mapContent;
while(getline(fin, mapContent)){
int index = mapContent.find_first_of(' ');
huffmanMapx.insert(make_pair(mapContent.substr(, index), mapContent[index+]));
}
} void outHuffmanEncode(){
string encode = "", cd="";
initHuffmanTree();
pHuffmanTreeNode p = T;
for(int i=; i<code.length(); ++i){
if(p->ld==NULL && p->rd==NULL){
encode+=huffmanMapx[cd];
cd="";
--i;
p=T;
} else {
cd+=code[i];
if(code[i]=='')
p=p->ld;
else if(code[i]=='')
p=p->rd;
}
if(i==code.length()-) encode+=huffmanMapx[cd];
}
cout<<encode<<endl;
}
}; int main(){
Huffman huffman;
huffman.outHuffmanEncode();
return ;
}

操作流程:

文本内容:aaaaaaabbbbbccdddd, and I am a student, my name is hjzgg!

1.首先利用''编码"工具将文本编码,会输出一个out.txt的文本,将out.txt文本中的内容发送给你的好友。

2.接受到out.txt文本的内容后,将内容复制到文本名为in.txt的文件中,利用"译码"工具(保证in.txt和译码工具在同一目录下)可以查看文本内容。

3.其中out.txt文本的格式如下:

Huffman树进行编码和译码的更多相关文章

  1. Huffman树的编码译码

    上个学期做的课程设计,关于Huffman树的编码译码. 要求: 输入Huffman树各个叶结点的字符和权值,建立Huffman树并执行编码操作 输入一行仅由01组成的电文字符串,根据建立的Huffma ...

  2. Huffman树与编码

    带权路径最小的二叉树称为最优二叉树或Huffman(哈夫曼树). Huffman树的构造 将节点的权值存入数组中,由数组开始构造Huffman树.初始化指针数组,指针指向含有权值的孤立节点. b = ...

  3. Huffman树与编码的简单实现

    好久没写代码了,这个是一个朋友问的要C实现,由于不会C,就用JAVA写了个简单的.注释掉的代码属性按照原来朋友发的题里带的参数,发现没什么用就给注释掉了. package other; import ...

  4. Huffman树及其编码(STL array实现)

    这篇随笔主要是Huffman编码,构建哈夫曼树有各种各样的实现方法,如优先队列,数组构成的树等,但本质都是堆. 这里我用数组来存储数据,以堆的思想来构建一个哈弗曼树,并存入vector中,进而实现哈夫 ...

  5. Huffman树及其编解码

    Huffman树--编解码 介绍:   Huffman树可以根据输入的字符串中某个字符出现的次数来给某个字符设定一个权值,然后可以根据权值的大小给一个给定的字符串编码,或者对一串编码进行解码,可以用于 ...

  6. 构造数列Huffman树总耗费_蓝桥杯

    快排! /** 问题描述 Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, …, pn-1},用这列数构造Huffman树的 ...

  7. Java蓝桥杯练习题——Huffman树

    Huffman树在编码中有着广泛的应用.在这里,我们只关心Huffman树的构造过程. 给出一列数{pi}={p0, p1, -, pn-1},用这列数构造Huffman树的过程如下: 找到{pi}中 ...

  8. Huffman树的构造及编码与译码的实现

    哈夫曼树介绍 哈夫曼树又称最优二叉树,是一种带权路径长度最短的二叉树.所谓树的带权路径长度,就是树中所有的叶结点的权值乘上其到根结点的路径长度(若根结点为0层,叶结点到根结点的路径长度为叶结点的层数) ...

  9. C++哈夫曼树编码和译码的实现

    一.背景介绍: 给定n个权值作为n个叶子结点,构造一棵二叉树,若带权路径长度达到最小,称这样的二叉树为最优二叉树,也称为哈夫曼树(Huffman Tree).哈夫曼树是带权路径长度最短的树,权值较大的 ...

随机推荐

  1. IE11 上的3个bug

    1.IE 11在popstate上无法正常使用,所以,需要使用老方法hashchange.有一个叫History.js的library,是可以解决这个问题.但如果url在"#"后跟 ...

  2. eclipse常见问题

    使用eclipse进入断点,当弹出"Confir Perspective Switch"视图时,选择"Yes".之后每次进入断点都会自动切换到debug视图. ...

  3. weex append

    append有两个值:其中的一个是tree, 另外一个是node. 不会像数据绑定一样对最后的渲染结果有影响.但它决定是否会影响整个节点的重绘还是只是某一个地方的内容会重绘. append=" ...

  4. VPB和OSGGIS安装

    VPB和OSGGIS安装 转自:http://blog.sina.com.cn/s/blog_668aae780101k6pr.html 第一部分VPB安装 VirtualPlanetBuilder是 ...

  5. 关于C#的微信开发的入门记录二

    在准备了空间和域名之后,现在来讲讲我们接下来的编码过程: 今天就先到这里了!没有服务器那些的请看我之前的博客:http://www.cnblogs.com/zhankui/p/4515905.html ...

  6. Python之路【第六篇】python基础 之面向对象进阶

    一 isinstance(obj,cls)和issubclass(sub,super) isinstance(obj,cls)检查是否obj是否是类 cls 的对象  和  issubclass(su ...

  7. 纯css3 Star

    <style><!--* { box-sizing: border-box; padding: 0px; margin: 0px; } body, html { height: 10 ...

  8. redis配置文件英译汉

    # By default Redis asynchronously dumps the dataset on disk. This mode is # good enough in many appl ...

  9. Map Network Driver

    K: \\10.11.80.5\deptfiling-pubgz$ O: \\10.11.80.5\deptfiling-itdgz$

  10. ABP理论学习之验证DTO

    返回总目录 本篇目录 验证介绍 使用数据注解 自定义验证 标准化 验证介绍 首先应该验证应用的输入.用户或者其它应用都可以向该应用发送输入.在一个web应用中,验证通常要实现两次:在客户端和服务器端. ...