赫夫曼编码

根据哈夫曼编码的原理,编写一个程序,在用户输入结点权值的基础上求赫夫曼编码,并能把给定的编码进行译码。

1)初始化:从键盘输入一字符串(或读入一文件),统计出现的字符和每个字符出现的频率,将字符出现的频率作为结点的权值,建立哈夫曼树。对各个字符进行哈夫曼编码,最后打印输出字符及每个字符对应的哈夫曼编码。 

2)编码:利用已建好的哈夫曼树对“输入串”进行哈夫曼编码,最后打印输入串对应的哈夫曼编码(写入文件)。   √

3)计算压缩比(选作)   

(4)译码:利用已建好的哈夫曼树对给定的一串代码进行译码,并打印输出得到的字符串。(选作)

测试数据:对字符串{casbcatbsatbat}进行编码;对电文“1101000”译码。字符集D={   },出现频率为w={


//运行环境 VS2015
#include "stdafx.h"
#include "malloc.h"
#include<stdio.h>
#include<string.h>
typedef struct HTNode
{
	unsigned int weight;
	char parent;
	unsigned int lchild,   //count
		         rchild;
}HTNode;
typedef char * *HC;
int Found(char c, HTNode ht[],int index)
{
	for (int i=1;i<index;i++)
	{
		if (c == ht[i].parent)return i;
	}
	return 0;
}
void Select(HTNode ht[], int i, int&s1, int&s2) {
	int j, k = 1;
	while (ht[k].parent != 0)
		k++;
	s1 = k;
	for (j = 1; j <= i; ++j)  // Select the first least of ht[].weight
		if (ht[j].parent == 0 && ht[j].weight<ht[s1].weight)
			s1 = j;
	k = 1;
	while ((ht[k].parent != 0 || k == s1))
		k++;
	s2 = k;
	for (j = 1; j <= i; ++j)  // Select the second  least of ht[].weight
		if (ht[j].parent == 0 && ht[j].weight<ht[s2].weight&&j != s1)
			s2 = j;
}
int Aver(int n)
{
	int i = 0;
	int m = 1;
	for (;m < n;i ++)
	{
		m *= 2;
	}
	return i;
}
#pragma warning(disable:4996)
void main()
{
	char st[101];  //字符种类字符串
	char ch[101];  //输入的所有字符字符串
	int num = 0;
	float haver=0; //赫夫曼编码平均字符编码长度
	HTNode *ht=(HTNode*)malloc(101*sizeof(HTNode));
	ht[0].lchild = 0;ht[0].parent = 1;
	while (char c = getchar())
	{
		if (c == '\n')break;
		ch[num] = c;
		num++;
		int temp = Found(c, ht, ht[0].parent);
		if (!temp)
		{
			ht[ht[0].parent].lchild = 1;
			ht[ht[0].parent].parent = c;
			st[ht[0].parent] = c;
			ht[0].parent++;
		}
		else
		{
			ht[temp].lchild++;
		}
		ht[0].lchild++;
	}
	ch[num] = '\0';
	for (int i = 1;i < ht[0].parent;i++)
	{
		ht[i].weight = (int)(((float)ht[i].lchild / (float)ht[0].lchild)*100);
	}
	int n = ht[0].parent - 1;    //字母种类总数
	int m = 2 * n - 1;
	for (int i = 1;i <= n;i++)ht[i] = { ht[i].weight,0,0,0 };
	for (int i = n + 1;i <= m;i++)ht[i] = { 0,0,0,0 };
	for (int i = n + 1;i <= m;++i)
	{
		int s1, s2;
		Select(ht, i - 1,s1,s2);
		ht[s1].parent = i;ht[s2].parent = i;
		ht[i].lchild = s1;ht[i].rchild = s2;
		ht[i].weight = ht[s1].weight + ht[s2].weight;
	}

	HC hc = (HC)malloc((n + 1) * sizeof(char*));
	char *cd = (char*)malloc(n * sizeof(char));
	cd[n - 1] = '\0';
	for (int i = 1;i <= n;++i)
	{
		int start = n - 1;
		int c;char f;
		for (c = i, f = ht[i].parent;f != 0;c = f, f = ht[f].parent)
		{
			if (ht[f].lchild == c) cd[--start] = '0';
			else cd[--start] = '1';
		}
		hc[i] = (char*)malloc((n - start) * sizeof(char));
		strcpy(hc[i], &cd[start]);
		haver += (float)(ht[i].weight *strlen(hc[i])) / 100;
	}
	free(cd);
	FILE *fp;
	fp = fopen("d:\\code.txt", "w");
	for (num = 0;ch[num] != '\0';num++)   //遍历输入的字符
	{
		for (int j = 1;j <= n;j++)     //遍历编码表
		{
			if (ch[num] == st[j])
			{
				fprintf(fp, "%s",hc[j]);
				printf("%s", hc[j]);
				break;
			}
		}
	}
	fclose(fp);
	putchar('\n');
	printf("********译码表*********\n");
	for (int i = 1;i<=n;i++)
	{
		printf("%c:%s\n", st[i],hc[i]);
	}
	printf("***********************\n");
	printf("赫夫曼编码平均码长=%0.2f\n", haver);
	printf("等长编码平均码长=%d\n", Aver(n));
	printf("赫夫曼编码的压缩率为%%%0.1lf\n",100.00-haver/(float)Aver(n)*100);
	printf("*********译码器********\n");
	printf("请输入要翻译的编码\n");
	char buffer[101]; //缓冲数组
	int index = 0;
	while (char c = getchar())
	{
		if (c == '\n')break;
		buffer[index] = c;      //将c存入缓冲
		index++;
		buffer[index] = '\0';
		for (int i = 1;i<=n;i++)            //遍历密钥表列
		{
			bool found = true;
			for (int j = 0;hc[i][j] != '\0';j++)  //遍历密钥表行				
			{
			    if (buffer[j] != hc[i][j])
			    {
    			    found = false;
				    break;
			    }
		    }
		    if (found)
		    {
			    printf("%c", st[i]);
			    index = 0;
			    break;
		    }
		}
	}
}



  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值