I am working on a compression/decompression assignment. I'm trying to write a C++ method to build a tree using the following preorder traversal from the header of my compressed file: 001c1b01a01e1d 0 represents an internal node while 1 represents a leaf. Every time I create a node, I set the code of that node to either 0 or 1, because I will be using another method to decode the tree and it must know how to traverse down different sides by using these codes. Whenever I reach a 1 in the preorder traversal string, I set the node's "symbol" field to the following character in the preorder string. I can't get it to work though. Can anyone help? Here is my code ("bitsToRead" just represents the length of the preorder string so the method knows when to stop) Thanks!
void HCTree::buildFromHeader(ifstream& in, HCNode* n, int codeToUse) {
if( bitsToRead > 0) {
char c = in.get();
bitsToRead--;
if(c == '0') {
n = new HCNode(0, 0, 0, 0, 0, codeToUse);
if(rootSet == 0) {
root = n;
rootSet = true;
}
HCNode* left;
n->c0 = left;
HCNode* right;
n->c1 = right;
buildFromHeader(in, left, 0);
buildFromHeader(in, right, 1);
}
else {
byte symbol = in.get();
n = new HCNode(0, symbol, 0, 0, 0, codeToUse);
bitsToRead--;
n->c0 = n->c1 = NULL;
}
}
}