[英]Huffman algorithm from Cormen
I'm trying to write a Huffman coding tree , by using multimap<int, NODE*>
where NODE
is just a classical tree node.我正在尝试使用
multimap<int, NODE*>
编写Huffman coding tree ,其中NODE
只是一个经典的树节点。 So I have written this method:所以我写了这个方法:
this->CreateMapTree();
int n = (int)tree.size();
pair<int, NODE*> x, y, z;
for (int i = 0; i < n-1; ++i)
{
x = *tree.begin(); tree.erase(tree.begin());
y = *tree.begin(); tree.erase(tree.begin());
z.second = new NODE;
z.second->left = y.second;
z.second->right = x.second;
z.first = x.first + y.first;
tree.insert(z);
}//after loop there is only one element in the tree
head = tree.begin()->second;
Method CreateMapTree
work perfect, as the result I have tree
.方法
CreateMapTree
工作完美,结果我有tree
。 I used classical algorithm of сreating Huffman tree (from Cormen).我使用了 сreating Huffman 树的经典算法(来自 Cormen)。 I was checking this code many times, but I couldn't find my mistake.
我多次检查这段代码,但找不到我的错误。 Code three is wrong and I can't create correct dictionary for encode and decode.
代码三是错误的,我无法为编码和解码创建正确的字典。
What is wrong?怎么了?
I suggest you look at what happens to x and y after you delete them from the tree.我建议您查看从树中删除 x 和 y 后会发生什么。 Pointers to them may no longer be valid.
指向它们的指针可能不再有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.