我正在尝试对霍夫曼树进行编码.我的树是对的.我只需要弄清楚如何修复我的递归函数来正确创建表.感谢您提供的任何帮助.
struct Code
{
char letter;
string code;
};
void createCode(BTree<Data>* root,string codeStr,vector<Code> &table)
{
if (root->getRightChild() == NULL && root->getLeftChild() == NULL)
{
Code code;
code.letter = root->getData().getLetter();
code.code = codeStr;
table.push_back(code);
}
else
{
createCode(root->getLeftChild(), codeStr.append("1"),table);
createCode(root->getRightChild(), codeStr.append("0"),table);
}
}
Run Code Online (Sandbox Code Playgroud)