西南科技大学OJ题 哈夫曼译码0986

本文介绍了一种基于哈夫曼树的译码方法,通过构建哈夫曼树并对密文进行解码,实现数据压缩与高效传输。文章提供了完整的哈夫曼树构建及译码流程,并通过示例展示了如何对编码后的数据进行解码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

哈夫曼译码

 1000(ms)

 10000(kb)

 1974 / 4142

通常要求根据给定的编码本对密文进行解码。现已给定相应字符的哈夫曼编码,要求根据编码对密文进行解码。(建立哈夫曼树以及编码、主函数等都已经给出,你只需要填写译码函数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

 

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
const int maxvalue=100;
const int maxbit=100;
const int maxn=100;
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)//将0到n-1依次储存相应的权重和数字 
		{ 
			hafftree[i].weight=weight[i];
			hafftree[i].ch=text[i];
 		} 
		else//将到2n-2依次设置权重为0,字符为# 
 		{ 
			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++)//循环找出未最小权重的2个结点,x1储存最小结点的序号,x2储存第2小结点的序号,m1储存最小结点的权重,m2储存第2小结点的权重 
		{ 
			if(hafftree[j].weight< m1&&hafftree[j].flag==0)//如果该结点的权重小于m1,并且未标记使用过,则将其权重以及序号赋值给m1,x1,原x1变x2,原m1变m2 
			{ 
				m2=m1;
				x2=x1;
 				m1=hafftree[j].weight;
 				x1=j; 
			} 
			else if(hafftree[j].weight< m2&&hafftree[j].flag==0)//如果该结点的权重小于m2,并且未标记使用过,则将其权重以及序号赋值给m2,x2
			{ 
				m2=hafftree[j].weight; 
				x2=j;
 			} 
		} 
		hafftree[x1].parent=n+i;//最小结点的双亲赋值 
		hafftree[x2].parent=n+i;//第2小结点的双亲赋值 
 		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)//翻译解码 
{ 
	char data[100];
	int t=2*n-2;//t为树根结点的序号 
	scanf("%s",data);//输入需要翻译的字符串 
	for(int i=0;data[i]!='\n';i++)
	{
		if(data[i]=='0') t=hafftree[t].leftchild;//如果为0,则向左孩子搜索 
		else if(data[i]=='1') t=hafftree[t].rightchild;//如果为1,则向右孩子搜索 
		if(hafftree[t].leftchild==-1)//如果左孩子为-1,则表示已经到了树的底部,则输出其对应的字符,然后重新从根结点继续寻找下一个字符 
		{
			printf("%c",hafftree[t].ch);
			t=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]; 
	code myhaffcode[maxvalue];
	haffman(weight,text,n,myhafftree);
	haffmancode(myhafftree,n,myhaffcode); 
	ccode(myhafftree,n); 
	return 0;
}

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值