我创建的霍夫曼解码功能有点问题。我只是想知道是否有人知道为什么我的程序会产生无限循环。以下是我的功能以及我如何调用它。当计数器达到 8 时,它应该退出函数,因为没有更多位要读取。这里是:
HuffmanNode *rodee = createTree(freqArray2, 256); //holds the huffman tree
HuffmanNode *temporaryNode; //temporary node for traversing
temporaryNode = rodee; //first the temporary node is equal to the root
while(cin.read((char*)&w, sizeof(w))
{
traverseCode(temporaryNode, rodee, bits, count);
count = 0; //reset the count back to 0 for the next time the function is called
} //as I read in a byte of 8 bits (I converted the bytes to bits in another function not shown
void traverseCode(HuffmanNode *temp, HuffmanNode *root, unsigned char *bits, int counter)
{
if(counter >= 7)
{
counter = 0;
return;
}
if(temp->getLeft() == NULL && temp->getRight() == NULL)
{
cout << temp->getLetter();
temp = root;
traverseCode(temp, root, bits, counter);
}
if((int)bits[counter] == 0)
{
traverseCode(temp->getLeft(), root, bits, counter++);
}
if((int)bits[counter] == 1)
{
traverseCode(temp->getRight(), root, bits, counter++);
}
}
可能有人知道为什么我的函数会进入无限循环以及如何解决这个问题?谢谢!