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)