0

I'm having trouble decompressing this Huffman tree. I can't seem to keep up with everything that's going on and don't know how to approach decompressing it. It's a simple concept but involves a lot of different variables that are throwing me off.

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;
}

Inside of main:

#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;
}

After all, I drew the huffman tree out by hand and noticed that the word is going to be "HELLO"..

Can anyone see what I'm doing wrong. I know the problem is in the decompress function.

0 Answers0