霍夫曼编码遍历
Huffman Code encoding traversal
我正在尝试对一个huffman树进行编码。我的树是正确的。我只需要弄清楚如何修复递归函数来正确创建表。谢谢你对我的帮助。
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);
}
}
codeStr.append
修改codeStr
。因此,您正确地将codeStr + "1"
传递给第一个递归调用,但将codeStr + "10"
传递给第二个递归调用。因此,所有出现的"0"都以一个额外的"1"作为前缀。
尝试
createCode(root->getLeftChild(), codeStr + "1",table);
createCode(root->getRightChild(), codeStr + "0",table);
相关文章:
- 有什么方法可以遍历结构吗
- 在循环中按顺序遍历成员变量
- 遍历模板参数
- 在遍历处理程序的向量时注册和注销处理程序
- C++RapidXml-使用first_node()遍历以修改XML文件中节点的值
- 遍历并行数组以确定C++中的最大数字
- 遍历顺序由 std::文件系统directory_iterator给出
- 遍历链表时的无限循环
- 遍历unordered_map向量
- 从预序遍历构造 bst 的 c++ 和 python 解决方案之间的区别
- C++声明双链表,使用两个 for 循环双向遍历列表并打印
- 如何正确地推回然后遍历堆中对象的向量?
- 遍历二维数组的所有子数组
- 如何在可变参数模板函数中遍历可变参数元组?
- 避免在遍历 IShellItemArray 时出现代码重复
- 从特定键开始遍历地图
- 关于链表遍历和调试的困惑
- 将树节点添加到向量向量中的 n 元树遍历的平均和最坏情况时间复杂度是多少?
- 霍夫曼编码遍历
- 需要帮助遍历树;霍夫曼编码