当前位置: 首页 > 编程笔记 >

霍夫曼编码算法

齐阳
2023-03-14
本文向大家介绍霍夫曼编码算法,包括了霍夫曼编码算法的使用技巧和注意事项,需要的朋友参考一下

霍夫曼编码是一种无损数据压缩算法。在该算法中,分配了可变长度代码以输入不同的字符。代码长度与字符使用频率有关。最常见的字符具有最小的代码,较长的代码具有最不频繁的字符。

主要有两个部分。第一个创建霍夫曼树,另一个html" target="_blank">遍历该树以查找代码。

例如,考虑一些字符串“ YYYZXXYYX”,字符Y的频率大于X,字符Z的频率最小。因此,Y的代码长度小于X,X的代码长度将小于Z。

根据每个字符的频率分配代码的复杂度为O(n log n)

输入输出

Input:
A string with different characters, say “ACCEBFFFFAAXXBLKE”
Output:
Code for different characters:
Data: K, Frequency: 1, Code: 0000
Data: L, Frequency: 1, Code: 0001
Data: E, Frequency: 2, Code: 001
Data: F, Frequency: 4, Code: 01
Data: B, Frequency: 2, Code: 100
Data: C, Frequency: 2, Code: 101
Data: X, Frequency: 2, Code: 110
Data: A, Frequency: 3, Code: 111

算法

huffmanCoding (字符串)

输入:不同字符的字符串。

输出:每个单独字符的代码。

Begin
   define a node with character, frequency, left and right child of the node for Huffman tree.
   create a list ‘freq’ to store frequency of each character, initially, all are 0
   for each character c in the string do
      increase the frequency for character ch in freq list.
   done

   for all type of character ch do
      if the frequency of ch is non zero then
         add ch and its frequency as a node of priority queue Q.
   done

   while Q is not empty do
      remove item from Q and assign it to left child of node
      remove item from Q and assign to the right child of node
      traverse the node to find the assigned code
   done
End

traverseNode(n:节点,代码)

输入: 霍夫曼树的节点n,以及上一次调用分配的代码

输出: 为每个字符分配的代码

if a left child of node n ≠φ then
   traverseNode(leftChild(n), code+’0’)     //traverse through the left child
   traverseNode(rightChild(n), code+’1’)    //traverse through the right child
else
   display the character and data of current node.

示例

#include
#include#include
using namespace std;

struct node {
   int freq;
   char data;
   const node *child0, *child1;

   node(char d, int f = -1) { //assign values in the node
      data = d;
      freq = f;
      child0 = NULL;
      child1 = NULL;
   }

   node(const node *c0, const node *c1) {
      data = 0;
      freq = c0->freq + c1->freq;
      child0=c0;
      child1=c1;
   }

   bool operator<( const node &a ) const { //< operator performs to find priority in queue
      return freq >a.freq;
   }

   void traverse(string code = "")const {
      if(child0!=NULL) {
         child0->traverse(code+'0'); //add 0 with the code as left child
         child1->traverse(code+'1'); //add 1 with the code as right child
      }else {
         cout << "Data: " << data<< ", Frequency: "< qu;
   int frequency[256];

   for(int i = 0; i<256; i++)
      frequency[i] = 0; //clear all frequency

   for(int i = 0; i1) {
      node *c0 = new node(qu.top()); //get left child and remove from queue
      qu.pop();
      node *c1 = new node(qu.top()); //get right child and remove from queue
      qu.pop();
      qu.push(node(c0, c1)); //add freq of two child and add again in the queue
   }

   cout <<“霍夫曼密码:” <

输出结果

The Huffman Code:
Data: K, Frequency: 1, Code: 0000
Data: L, Frequency: 1, Code: 0001
Data: E, Frequency: 2, Code: 001
Data: F, Frequency: 4, Code: 01
Data: B, Frequency: 2, Code: 100
Data: C, Frequency: 2, Code: 101
Data: X, Frequency: 2, Code: 110
Data: A, Frequency: 3, Code: 111
 类似资料:
  • 我正在为算法和数据结构课做作业。我很难理解给出的说明。我会尽力解释这个问题。 我得到的输入是一个正整数 n,后跟 n 个正整数,表示有序字符集中符号的频率(或权重)。第一个目标是构造一个树,为有序字符集的每个字符提供近似的顺序保持霍夫曼代码。我们要通过“贪婪地合并权重最小的两棵相邻树”来实现这一点。 在赋值中,我们看到传统的霍夫曼代码树是通过将权重首先插入到优先级队列中来构造的。然后通过使用 de

  • 赫夫曼树 路径和路径长度:表示树从根节点开始到达节点经过的次数,若一颗树根节点为1层,那么第K层的树的路径的长度为K-1 权: 赋予每一个节点上面特定的权重值 带权路径:带权路径等于节点的权与路径长度的乘积,为带权路径 = 权 * 路径长度 树的带权路径长度:为所有叶子节点的带权路径之和记做WPL(weight path length) 赫夫曼树huffman-tree或哈夫曼树,又称最优二叉树,

  • 我用哈夫曼编码压缩了一个二进制文件。现在我试图找到压缩效率。 在我的二进制文件中,我有符号(0的buch 符号:0频率:173 符号:1频率:50 符号:2频率:48 符号:3频率:45 目前,每个符号都在UInt64中编码,所以如果我计算大小的方法正确,文件大小将为(173 50 48 45)*8=2528字节。如果我错了,请纠正我。在调试时,我得到了2536个,还有8个,我不知道为什么? 压缩

  • 我正在尝试在python中生成霍夫曼代码树的有序和预序遍历。不过,我似乎碰壁了。我需要生成遍历并将它们写入相应的文件,然后创建指向每个节点位置的二进制路径并将其输出到文件中。这是我到目前为止所拥有的

  • 我正在尝试创建一个正确的霍夫曼树,并想知道这是否正确。顶部数字是频率/权重,底部数字是 ASCII 代码。字符串是“hhiiiissssss”。如果我将其输入到文本文件中,则只有一个 LF 正确吗?我不确定为什么我的程序是两读的。

  • 我试图压缩一个24位的值。但我之前没有任何压缩方面的经验。所以,我想知道是否有人可以给我一些见解或建议,如何编码和解码的24位值使用verilog或matlab。 问题:我将24位值分成6块4位。每个4位在哈夫曼树中都有一个唯一的路径。我按照这棵树查找压缩值,但我在如何解码这些值上遇到了障碍。由于树是静态的,解码器将知道它。但是,当解码器获得比特流时,它怎么知道如何解码呢。 附上一张图片来阐明我在