简体   繁体   中英

Decoding a Huffman Tree

I'm trying to decode a Huffman tree of the form:

001A1C01E01B1D

I'm using an implementation found here: Efficient way of storing Huffman tree to encode the tree in the form above and decode it as well.

This is my implementation of it:

HuffmanNode* HuffmanTree::decodeTree(string tree, int idx) {
    cout << idx << endl;
    if (tree[idx] == '1') {
        idx += 2;
        return new HuffmanNode(tree[idx - 1]);
    }
    else {
        if (idx != tree.length() - 1) {
            idx++;
            HuffmanNode* leftChild = decodeTree(tree, idx);
            idx++;
            HuffmanNode* rightChild = decodeTree(tree, idx);
            return new HuffmanNode(leftChild, rightChild);
        }
        else
            return new HuffmanNode(tree[idx]);
    }
}

I'm getting an access violation writing a location when the function unwinds (On "return new HuffmanNode(tree[idx - 1]);" ), and I'm hoping that the final return would be the root of the tree, but upon further inspection this doesn't seem to be the case. Can anyone give me some pointers? (No pun intended)

The problem with your code is that idx is not modified inside recursive runs. Pass it into the function as int & : HuffmanNode* HuffmanTree::decodeTree(string tree, int &idx)

There is also one more bug in your code, which makes it segfault: instead of

if (tree[idx] == '1') {
    idx += 2;
    return new HuffmanNode(tree[idx - 1]);
}

you should have

if (tree[idx] == '1') {
    ++idx;
    return new HuffmanNode(tree[idx]);
}

Another 1 is being added to index within the second block:

idx++;
HuffmanNode* leftChild = decodeTree(tree, idx);
idx++;
HuffmanNode* rightChild = decodeTree(tree, idx);

Also, consider doing a thing, similar to the example you have linked to: pass reference to a string iterator, (or an istringstream , or some other stream) and don't pass an index: HuffmanNode* HuffmanTree::decodeTree(std::string::const_iterator &tree) .

Also, you don't have to do checks like if (idx != tree.length() - 1) if the tree is well-formed. You may still do that at the start of the function to check for errors in your input, along with checks that current symbol is either '0' or '1' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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