986: 哈夫曼译码
题目描述
通常要求根据给定的编码本对密文进行解码。现已给定相应字符的哈夫曼编码,要求根据编码对密文进行解码。(建立哈夫曼树以及编码、主函数等都已经给出,你只需要填写译码函数void ccode(haffnode hafftree[],int n)即可。
const int maxvalue=100;
const int maxbit=100;
const int maxn=100;
#include “iostream”
#include “stdio.h”
#include “stdlib.h”
using namespace std;
struct haffnode
{
char ch;
int weight;
int flag;
int parent;
int leftchild;
int rightchild;
};
struct code
{
int bit[maxn];
int start;
int weight;
char ch;
};
void haffman(int weight[],char text[],int n,haffnode hafftree[])
{
int j,m1,m2,x1,x2,i;
for(i=0;i< 2*n-1;i++)
{
if(i < n)
{
hafftree[i].weight=weight[i];
hafftree[i].
ch=text[i];
}
else
{
hafftree[i].weight=0;
hafftree[i].ch=’#’;
}
hafftree[i].parent=0;
hafftree[i].flag=0;
hafftree[i].leftchild=-1;
hafftree[i].rightchild=-1;
}
for(i=0;i< n-1;i++)
{
m1=m2=maxvalue;
x1=x2=0;
for(j=0;j< n+i;j++)
{
if(hafftree[j].weight< m1&&hafftree[j].flag==0)
{
m2=m1;
x2=x1;
m1=hafftree[j].weight;
x1=j;
}
else if(hafftree[j].weight< m2&&hafftree[j].flag==0)
{
m2=hafftree[j].weight; x2=j;
}
}
hafftree[x1].parent=n+i;
hafftree[x2].parent=n+i;
hafftree[x1].flag=1;
hafftree[x2].flag=1;
hafftree[n+i].weight=hafftree[x1].weight+hafftree[x2].weight;
hafftree[n+i].leftchild=x1; hafftree[n+i].rightchild=x2;
}
}
void haffmancode(haffnode hafftree[],int n,code haffcode[])
{
code cd; int i,j; int child,parent;
for( i=0;i< n;i++)
{
cd.start=n-1;
cd.weight=hafftree[i].weight;
cd.ch=hafftree[i].ch;
child=i;
parent=hafftree[child].parent;
while(parent!=0)
{
if(hafftree[parent].leftchild==child)
cd.bit[cd.start]=0;
else cd.bit[cd.start]=1;
cd.start–;
child=parent;
parent=hafftree[child].parent;
}
for(j=cd.start+1;j< n;j++)
haffcode[i].bit[j]=cd.bit[j];
haffcode[i].start=cd.start;
haffcode[i].weight=cd.weight;
haffcode[i].ch=cd.ch;
}
}
void ccode(haffnode hafftree[],int n)
{ }
int main( )
{
int n=8;
int weight[]={5,29,7,8,14,23,3,11};
char text[]={‘a’,‘b’,‘c’,‘d’,‘e’,‘f’,‘g’,‘h’};
haffnode myhafftree[maxvalue];
code myhaffcode[maxvalue];
haffman(weight,text,n,myhafftree);
haffmancode(myhafftree,n,myhaffcode);
ccode(myhafftree,n);
return 0;
}
输入
根据哈夫曼树编码表,针对字符串做好的编码结果。
输出
对每一行需要解码的串,进行解码,并输出解码后的结果。
样例输入
000100011011101110
样例输出
aabcc
思路:
- 我对哈夫曼的理解是,把哈夫曼比作是一个密码小本本,我们只有按照小本本上面的东西来翻译才能把特殊密码翻译出来。这道题相当于把小本本给了我们,我们只需要读懂小本本的编码方式按部就班的解码就能得出答案,总的来说其实并不难。
由于题干中涉及到的只有哈夫曼解码,下面的内容中关于哈夫曼树的基本概念和构造方法只做简略介绍,重点讲解哈夫曼的解码过程:
- 解释题干中的代码(电脑喜欢的小本本)
const int maxvalue=100;
const int maxbit=100;
const int maxn=100;
#include "iostream"
#include "stdio.h"
#include "stdlib.h"
#include<string.h>
using namespace std;
struct haffnode
{
char ch;
int weight;
int flag;
int parent;
int leftchild;
int rightchild;
};
struct code
{
int bit[maxn];
int start;
int weight;
char ch;
};
//种哈夫曼树
void haffman(int weight[],char text[],int n,haffnode hafftree[])
{
int j,m1,m2,x1,x2,i;
//i、j是下标
//m1,m2是两个最小值,更小的为左子树的值
//较大的是右子树的值
//x1是左子树,x2是右子树
for(i=0;i<2*n-1;i++)
{
if(i < n)//前n个是叶子结点,有权值和结点值
{
hafftree[i].weight=weight[i];
hafftree[i].ch=text[i];
}
else// [n,2*n-2] 是双亲结点,初始时没有权值,结点值置为‘#’号
{
hafftree[i].weight=0;
hafftree[i].ch='#';
}
//每个结点的其余数据都置初值
hafftree[i].parent=0;
hafftree[i].flag=0;
hafftree[i].leftchild=-1;
hafftree[i].rightchild=-1;
}
for(i=0;i<n-1;i++)
{
m1=m2=maxvalue;
//两个最小值置为最大值,待会好比较
x1=x2=0;
//左右子树
for(j=0;j<n+i;j++)//从中取出两个权值最小的点
{
if(hafftree[j].weight< m1&&hafftree[j].flag==0)//并且未被访问
{
//遇到更小的m1(左子树权值)
//则把前一个m1的值给m2,因为要满足左子树比右子树小
m2=m1;
x2=x1;
m1=hafftree[j].weight;
x1=j;
}
else if(hafftree[j].weight< m2&&hafftree[j].flag==0)
{
m2=hafftree[j].weight; x2=j;
}
}
//选出两个子结点,构成一个父结点
hafftree[x1].parent=n+i;
hafftree[x2].parent=n+i;
hafftree[x1].flag=1;//标志已访问
hafftree[x2].flag=1;//标志已访问
hafftree[n+i].weight=hafftree[x1].weight+hafftree[x2].weight;
//父节点权重
hafftree[n+i].leftchild=x1; hafftree[n+i].rightchild=x2;
//父节点的左右子树
}
}
//放哈夫曼编码
void haffmancode(haffnode hafftree[],int n,code haffcode[])
{
code cd;//临时哈夫曼编码
int i,j;
int child,parent;
for(i=0;i<n;i++)//一共n个叶子节点
{
cd.start=n-1;//从该叶子结点开始,从下往上遍历树
cd.weight=hafftree[i].weight;
cd.ch=hafftree[i].ch;
child=i;
parent=hafftree[child].parent;
while(parent!=0)//从下往上,到根节点为止
{
//哈夫曼树转编码时,左子树为0,右子树为1
if(hafftree[parent].leftchild==child)//当前子树是左子树
cd.bit[cd.start]=0;
else cd.bit[cd.start]=1;
cd.start--;
child=parent;//往上
parent=hafftree[child].parent;
}
for(j=cd.start+1;j<n;j++)//从头开始设置该根节点的哈夫曼编码的值
haffcode[i].bit[j]=cd.bit[j];
haffcode[i].start=cd.start;
haffcode[i].weight=cd.weight;
haffcode[i].ch=cd.ch;
}
}
//编译哈夫曼编码即我们要做的工作
void ccode(haffnode hafftree[],int n)
{
}
int main( )
{
int n=8;
int weight[]={5,29,7,8,14,23,3,11};
char text[]={'a','b','c','d','e','f','g','h'};
haffnode myhafftree[maxvalue];
//最多maxvalue个哈夫曼结点构成一棵哈夫曼树
code myhaffcode[maxvalue];
haffman(weight,text,n,myhafftree);
//先种哈夫曼树
haffmancode(myhafftree,n,myhaffcode);
//根据哈夫曼树来放哈夫曼编码
ccode(myhafftree,n);
//编译哈夫曼编码
return 0;
}
- 根据题干画出哈夫曼树的逻辑结构(人类喜欢的小本本)
- 哈夫曼解码:从根节点开始,左走零,右走一,走到叶子节点就输出,重复以上步骤至编码解完。
- 完整答案:
const int maxvalue=100;
const int maxbit=100;
const int maxn=100;
#include "iostream"
#include "stdio.h"
#include "stdlib.h"
#include<string.h>
using namespace std;
struct haffnode
{
char ch;
int weight;
int flag;
int parent;
int leftchild;
int rightchild;
};
struct code
{
int bit[maxn];
int start;
int weight;
char ch;
};
void haffman(int weight[],char text[],int n,haffnode hafftree[])
{
int j,m1,m2,x1,x2,i;
//i、j是下标
//m1,m2是两个最小值,更小的为左子树的值
//较大的是右子树的值
//x1是左子树,x2是右子树
for(i=0;i<2*n-1;i++)
{
if(i < n)//前n个是叶子结点,有权值和结点值
{
hafftree[i].weight=weight[i];
hafftree[i].ch=text[i];
}
else// [n,2*n-2] 是双亲结点,初始时没有权值,结点值置为‘#’号
{
hafftree[i].weight=0;
hafftree[i].ch='#';
}
//每个结点的其余数据都置初值
hafftree[i].parent=0;
hafftree[i].flag=0;
hafftree[i].leftchild=-1;
hafftree[i].rightchild=-1;
}
for(i=0;i<n-1;i++)
{
m1=m2=maxvalue;
//两个最小值置为最大值,待会好比较
x1=x2=0;
//左右子树
for(j=0;j<n+i;j++)//从中取出两个权值最小的点
{
if(hafftree[j].weight< m1&&hafftree[j].flag==0)//并且未被访问
{
//遇到更小的m1(左子树权值)
//则把前一个m1的值给m2,因为要满足左子树比右子树小
m2=m1;
x2=x1;
m1=hafftree[j].weight;
x1=j;
}
else if(hafftree[j].weight< m2&&hafftree[j].flag==0)
{
m2=hafftree[j].weight; x2=j;
}
}
//选出两个子结点,构成一个父结点
hafftree[x1].parent=n+i;
hafftree[x2].parent=n+i;
hafftree[x1].flag=1;//标志已访问
hafftree[x2].flag=1;//标志已访问
hafftree[n+i].weight=hafftree[x1].weight+hafftree[x2].weight;
//父节点权重
hafftree[n+i].leftchild=x1; hafftree[n+i].rightchild=x2;
//父节点的左右子树
}
}
void haffmancode(haffnode hafftree[],int n,code haffcode[])
{
code cd;//临时哈夫曼编码
int i,j;
int child,parent;
for(i=0;i<n;i++)//一共n个叶子节点
{
cd.start=n-1;//从该叶子结点开始,从下往上遍历树
cd.weight=hafftree[i].weight;
cd.ch=hafftree[i].ch;
child=i;
parent=hafftree[child].parent;
while(parent!=0)//从下往上,到根节点为止
{
//哈夫曼树转编码时,左子树为0,右子树为1
if(hafftree[parent].leftchild==child)//当前子树是左子树
cd.bit[cd.start]=0;
else cd.bit[cd.start]=1;
cd.start--;
child=parent;//往上
parent=hafftree[child].parent;
}
for(j=cd.start+1;j<n;j++)//从头开始设置该根节点的哈夫曼编码的值
haffcode[i].bit[j]=cd.bit[j];
haffcode[i].start=cd.start;
haffcode[i].weight=cd.weight;
haffcode[i].ch=cd.ch;
}
}
void ccode(haffnode hafftree[],int n)
{
int Treenode;
char str[maxvalue];
scanf("%s",str);
int len=strlen(str);
//此处长度不为n,长度为输入字符串的长度
Treenode=2*n-2;
for(int j=0;j<len;j++)
{
if(str[j]=='0')//为0,朝左分支走
{
Treenode=hafftree[Treenode].leftchild;
}
else if(str[j]=='1')//为1,朝右分支走
{
Treenode=hafftree[Treenode].rightchild;
}
if(hafftree[Treenode].leftchild==-1||hafftree[Treenode].rightchild==-1)
//走到叶子结点即可输出符号
{
printf("%c",hafftree[Treenode].ch);
Treenode=2*n-2;
}
}
}
int main( )
{
int n=8;
int weight[]={5,29,7,8,14,23,3,11};
char text[]={'a','b','c','d','e','f','g','h'};
haffnode myhafftree[maxvalue];
//maxvalue个哈夫曼结点构成一棵哈夫曼树
code myhaffcode[maxvalue];
haffman(weight,text,n,myhafftree);
//先得出哈夫曼树
haffmancode(myhafftree,n,myhaffcode);
//根据哈夫曼树来创建哈夫曼编码
ccode(myhafftree,n);
//打表
return 0;
}
感谢西南科技大学->拉图的分享
以上方法仅供参考,欢迎互联网广大朋友们提出指正。