用C ++中的二进制数解码霍夫曼树

时间:2017-04-27 03:45:05

标签: c++ binary decoding huffman-code compression

我在解压这个霍夫曼树时遇到了麻烦。我似乎无法跟上所发生的一切,并且不知道如何解压缩它。这是一个简单的概念,但涉及许多不同的变量,这些变量让我失望。

string huffman::Decompress(string bits)
{

int currNode;
string bitCode, word;
bitCode = bits;
string tempCode = "TEST";
int bitCodeSize = sizeof(bitCode);

word = "";

//hmm should I try to run through the size of the bitCode as I update it?
for(int i = bitCodeSize; i >= 0; i--)
{
    currNode = i;

    //I only want to run this until we hit a leaf then go back to the for loop and continue...
    while(tree[currNode].left != NIL && tree[currNode].right != NIL)
    {
            if (tree[currNode].bitCode == bitCode)
            {
                tempCode = tempCode + "1";
            }
            else if(tree[currNode].right == bitCode[i])
            {
                tempCode = tempCode + "0";
            }
    }

    if (tree[currNode].left == NIL && tree[currNode].right == NIL)
    {
        word = word + (char)tree[currNode].ch;
    }
        cout << bitCodeSize << endl;
}

return word;
}

主要内部:

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <bitset>

#include "huffnode.h"
#include "huffman.h"

using namespace std;

int main()
{
string characters;
vector<int> charFreq;
characters = "etaoinsrhldcu";
charFreq = {125,93,80,76,73,71,65,61,55,41,40,31,27};

huffman huff1;

huff1.buildTree(characters, charFreq);

huff1.generateCodes();

huff1.listCodes();

huff1.writeTree();

string charStr, bitCode;

bitCode = "100010101010101000";
//1000-101-0101-0101-000
//---h---e----l----l---o
charStr = huff1.Decompress(bitCode);
cout << "Decompress" << endl;
cout << bitCode << " = " << charStr << endl << endl;

//Keep Console window open until keyboard input
int hold;
cin >> hold;
}

毕竟,我手工拔出了霍夫曼树,注意到这个词将是&#34; HELLO&#34; ..

任何人都可以看到我做错了什么。我知道问题出在解压缩功能中。

0 个答案:

没有答案