简体   繁体   English

霍夫曼以比特存储代码

[英]Huffman storing code in bits

I have build the huffman tree. 我已经建造了霍夫曼树。 But I have no idea to store the code to bits due to I don't know how 但由于我不知道如何,我不知道将代码存储到位

to handle the variable length. 处理可变长度。

I want to create a table that store the huffman code in bits for print the encoded result. 我想创建一个表,用于存储比特的霍夫曼代码以打印编码结果。

I cannot use the STL containter like bitset. 我不能像bitset一样使用STL包含。

I have try like that 我试过这样的

   void traverse( string code = "")const
   {
        if( frequency == 0 ) return;
        if ( left ) {
             left->traverse( code + '0' );
             right->traverse( code + '1' );
        }
        else {//leaf node
             huffmanTable[ch] = code;
        }
   } 

Can you give me some algorithm to handle it? 你能给我一些算法来处理吗?

I want to store the '0' use 1 bit and "1" use 1 bit. 我想存储'0'使用1位而“1”使用1位。

Thx in advance. Thx提前。

You'll need a buffer, a variable to track the size of the buffer in bytes, and a variable to track the number of valid bits in the buffer. 您需要一个缓冲区,一个用于跟踪缓冲区大小的变量(以字节为单位),以及一个用于跟踪缓冲区中有效位数的变量。

To store a bit: 存储一下:

  1. Check if adding a bit will increase the number of bytes stored. 检查添加一个位是否会增加存储的字节数。 If not, skip to step 4. 如果没有,请跳至步骤4。

  2. Is there room in the buffer to store an additional byte? 缓冲区中是否有空间存储额外的字节? If so, skip to step 4. 如果是,请跳至步骤4。

  3. Reallocate a storage buffer a few bytes larger. 重新分配几个字节的存储缓冲区。 Copy the existing data. 复制现有数据。 Increase the variable holding the size of the buffer. 增加保持缓冲区大小的变量。

  4. Compute the byte position and bit position at which the next bit will be stored. 计算将存储下一位的字节位置和位位置。 Set or clear that bit as appropriate. 根据需要设置或清除该位。

  5. Increment the variable holding the number of bits stored. 增加保存存储位数的变量。

You can use a fixed size structure to store the table and just bits to store encoded input: 您可以使用固定大小的结构来存储表,只使用位来存储编码输入:

struct TableEntry {
  uint8_t size;
  uint8_t code;
};

TableEntry huffmanTable[256];

void traverse(uint8_t size; uint8_t code) const {
        if( frequency == 0 ) return;
        if ( left ) {
             left->traverse(size+1, code << 1 );
             right->traverse(size+1, (code << 1) | 1 );
        }
        else {//leaf node
             huffmanTable[ch].code = code;
             huffmanTable[ch].size = size;
        }
} 

For encoding, you can use the algorithm posted by David . 对于编码,您可以使用David发布的算法。

Basically I'd use one of two different approaches here, based on the maximum key length/depth of the tree: 基本上我会根据树的最大密钥长度/深度使用两种不同的方法之一:

  • If you've got a fixed length and it's shorter than your available integer data types (like long int ), you can use the approach shown by perreal. 如果你有一个固定的长度并且它比可用的整数数据类型(如long int )短,你可以使用perreal显示的方法。

  • If you don't know the maximum depth and think you might be running out of space, I'd use std::vector<bool> as the code value. 如果你不知道最大深度并认为你的空间不足,我会使用std::vector<bool>作为代码值。 This is a special implementation of the vector using a single bit per value (essentially David's approach). 这是使用每个值一位的向量的特殊实现(基本上是David的方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM