简体   繁体   中英

issues with huffman decoding using tree

I was able to take the prefixes from a text file and get them into a vector, for example:

So my code:

  • starts with a root node
  • iterate through the vector, create a new node. if we get a 0, take the current node and have it's left pointer point to the new node. if we get a 1, take the current node and have it's right pointer point to the new node. if it's a character, store that character into the current node and start over from the root.

a node is just something that holds a value and has left and right pointers.

Something is going terrible wrong here, but I'm not sure what it is at all. Does anybody see any glaring issues with the implementation?

EDIT: Well I found something interesting. It seems like for every bit it reads, it goes until it finds a leaf. I guess that's kind of how I coded it. For example when '1' is read, it goes right, right again, and couts 'd'. Recursion :(

One problem is - in buildTree , what if the current node already has a child? You'll just create a new child and override the previous one.

You should only create the node if it doesn't already have a child.

So your buildTree function should look something like this:

void Foo:: buildTree(vector<char> v) {
        node* root = new node; 
        vector<char>:: iterator itr;
        node* current = root; 

        for(itr =  v.begin(); itr != v.end(); itr++) {
            cout << *itr << ".";

            if (*itr == '0') {
                if (current->left == NULL)
                   current->left = new node;
                current = current->left;
            }
            else if (*itr == '1') {
                if (current->right == NULL)
                   current->right = new node;
                current = current->right;
            }
            else {  // is a symbol
                current->value = *itr; 
                current = root; 
            }
        }
        nodeVector.push_back(*root); 
}

Another problem is your decode function - you're recursing through the tree, but you're passing the same bit around.

I'd probably scrap the recursive approach altogether and just use a for-loop.

Some pseudo-code:

current = root
for each character c
  if c == 0
    if current.left == NULL
      print current.value
      current = root
    else
      current = current.left
  else
    if current.right == NULL
      print current.value
      current = root
    else
      current = current.right

You need to pass the next bit to the recursion in decode , not the current one again. In fact you can do without recursion, just return the new tempRoot from decode and continue iterating with that. Don't forget to return root when decoding a leaf node.

BTW: Lots of memory leaks in buildTree .

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