6.6.2赫夫曼编码
发布日期:2021-06-30 10:49:24 浏览次数:2 分类:技术文章

本文共 829 字,大约阅读时间需要 2 分钟。

首先介绍几个名词。

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

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

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

下面给出一个赫夫曼树。

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

所以编码:

A(0)

B(10)

C(110)

D(111)

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

下面是部分代码:

#include 
#include
#include
#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://it1995.blog.csdn.net/article/details/56293231 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:7.1图的定义和术语
下一篇:6.6.1最优二叉树(赫夫曼树)

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月19日 17时52分30秒