SWUST数据结构--哈夫曼译码

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)
{
	char ch[100];
	int i=0;
	cin>>ch;
	int child=2*n-2;
	while(ch[i]=='0'||ch[i]=='1'){
		if(ch[i]=='0')
			child=hafftree[child].leftchild;
		else
			child=hafftree[child].rightchild;
		if(child<n)
		{
			cout<<hafftree[child].ch;
			child=2*n-2;
		}
		i++;
	}
}
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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
哈夫曼编码译码 包括默认编码 和 自定义编码 数据结构课程设计 一、题目: 哈夫曼编码/译码的设计与实现 二、目的与要求 1、目的: 通过布置具有一定难度的实际程序设计项目,使学生进一步理解和掌握课堂上所学各种基本抽象数据类型的逻辑结构、存储结构和操作实现算法,以及它们在程序中的使用方法;使学生掌握分析问题,求解问题的方法并提高学生设计编程实现的能力。 2、要求: 基本要求: 1. 要求利用C\C++语言来完成系统的设计; 2. 突出C语言的函数特征(以多个函数实现每一个子功能)或者C++语言面向对象的编程思想; 3. 画出功能模块图; 4. 进行简单界面设计,能够实现友好的交互; 5. 具有清晰的程序流程图和数据结构的详细定义; 6. 熟练掌握C语言或者C++语言的各种操作。 创新要求: 在基本要求达到后,可进行创新设计,如系统用户功能控制,改进算法的实现,实现友好的人机交互等等 三、问题描述和求解方法: 首先根据给定的n个权值构造哈夫曼树。通过遍历此二叉树完成哈夫曼编码。 四、解题过程 1. 分析程序的功能要求,划分程序功能模块。 2. 画出系统流程图。 3. 代码的编写。定义数据结构和各个功能子函数。 4. 程序的功能调试。 5. 完成系统总结报告以及使用说明书 五、进度安排 此次课程设计时间为一周,分以下几个阶段完成: 1. 选题与搜集资料:每人选择一题,进行课程设计课题的资料搜集。 2. 分析与概要设计:根据搜集的资料,进行程序功能与数据结构分析,并选择合适的数据结构、并在此基础上进行实现程序功能的算法设计。 3. 程序设计:运用掌握C/C++语言编写程序,实现各个模块功能。 4. 调试与测试:调试程序,并记录测试情况。 5. 完成课程设计报告。 6. 验收与评分:指导教师对每个同学的开发的系统进行综合验收,并由学院考核小组进行随机抽查评分。 六、撰写课程设计报告或课程设计总结 课程设计报告要求: 课程设计报告要求规范书写,应当包括如下6个部分: 1. 问题描述 2. 基本要求 3. 系统分析与设计 4. 测试数据及结果 5. 总结 6. 附录:源程序清单 七、答辩与评分标准: 1 、作业文档: 50 分; 2 、基本功能和要求: 20 分; 2 、设计报告及使用说明书: 10 分; 3 、设置错误或者按照要求改变结果: 10 分; 4 、回答问题: 10 分。 八、参考资料 《数据结构(C语言版)》 网上相关资料(....略) printf("---------------------------------------------\n"); printf(" 哈夫曼编码和译码 \n"); printf(" 1.使用默认初始化 \n"); printf(" 2.使用自定义初始化 \n"); printf(" 3.进行哈夫曼编码 \n"); printf(" 4.进行哈夫曼译码 \n") ; printf(" 5.退出哈夫曼操作 \n"); printf(" 请输入1.2.3.4.5 \n"); printf(" ---------------------------------------------\n");
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值