算法基础(九):超详细最优二叉树构建(2)求编码

从叶子到跟逆向求每个字符的赫夫曼编码:

void GetHuffmanCode(HuffmanTree &HT,HuffmanCode &HC,int n)
{	
	char* cd;		
	int c;
	int f;
	int start;
	int i = 0;
	//从叶子到根逆向求每个字符的哈夫曼编码
	printf("\n进入求编码函数...");
	HC = (HuffmanCode)malloc( (n+1) * sizeof(char*));		//分配n个字符编码的头指针向量
	cd = (char*)malloc(n*sizeof(char));		//分配求编码的工作空间,n个,因为有n个叶子节点
	cd[n - 1] = '\0';						//编码结束符
	for(i = 1;i <= n;++i)					//逐个字符求哈弗曼编码
	{
		start = n - 1;						//编码结束符位置
		for(c = i,f = HT[i].parent;f != 0;c = f,f = HT[f].parent)	//f!=0保证一直朝上走,走到根节点
		{
			if(HT[f].lchild == c)	//判断是否为左孩子
				cd[--start] = '0';
			else cd[--start] = '1';	//右孩子			
						
		}
		//退出循环,第i个叶子节点的huffmancode求出,开辟空间,然后赋值,因为HC是char**类型
		HC[i] = (char*)malloc( (n - start) * sizeof(char));
		strcpy(HC[i],cd+start);
		printf("\n编号为:%d,权值为:%d的哈弗曼编码为:",i,HT[i].weight);
		puts(HC[i]);
	}
	free(cd);	//释放工作空间

这段代码加到上一篇文章的头文件里或者main.cpp里面都可以,main.cpp如下:

#include"stdafx.h" 
//#include"BasicGraph2.h"
#include "HuffmanCode.h"
void main()
{ 
	HuffmanTree HT;
	HuffmanCode code;
	int n = 8;
	int m = 15;
	int arr[] = {5,29,7,8,14,23,3,11};
	if(HuffmanCoding(HT,arr,n))	//构建哈弗曼树
		ShowHT(HT,n);			//测试是否正确
	GetHuffmanCode(HT,code,n);	//求哈弗曼编码,结果保存在code中
}

运行结果:


结构图:


注意:8号和11号和我最初画的位置相反。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
文章作者写的matlab源代码,该文章发表在Digital Signal Processing: Ke-Kun Huang , Hui Liu, Chuan-Xian Ren, Yu-Feng Yu and Zhao-Rong Lai. Remote sensing image compression based on binary tree and optimized truncation. Digital Signal Processing, vol. 64, pp. 96-106, 2017. (http://dx.doi.org/10.1016/j.dsp.2017.02.008) 遥感图像数据非常广泛,因此需要通过空间设备上的低复杂度算法进行压缩。具有自适应扫描顺序(BTCA)的二叉树编码是一个的有效算法。然而,对于大规模遥感图像,BTCA需要大量的内存,而且不能随机存取。在本文中,我们提出了一种基于BTCA的新的编码方法。小波图像首先划分为几个块,并由BTCA单独编码的。根据BTCA的属性,仔细选择每个块的有效截断点,以优化速率失真的比例,从而获得更高的压缩比、更低的内存要和随机访问性能。由于没有任何熵编码,所提出的方法简单快速,非常适合于空间设备。对三个遥感图像集进行实验,结果表明它可以显着提高PSNR、SSIM和VIF,以及主观视觉体验。 The remote sensing image data is so vast that it requires compression by low-complexity algorithm on space-borne equipment. Binary tree coding with adaptive scanning order (BTCA) is an effective algorithm for the mission. However, for large-scale remote sensing images, BTCA requires a lot of memory, and does not provide random access property. In this paper, we propose a new coding method based on BTCA and optimize truncation. The wavelet image is first divided into several blocks which are encoded individually by BTCA. According the property of BTCA, we select the valid truncation points for each block carefully to optimize the ratio of rate-distortion, so that a higher compression ratio, lower memory requirement and random access property are attained. Without any entropy coding, the proposed method is simple and fast, which is very suitable for space-borne equipment. Experiments are conducted on three remote sensing image sets, and the results show that it can significantly improve PSNR, SSIM and VIF, as well as subjective visual experience.

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值