huffman编码问题
//我是用最小堆实现的,以下是建立Huffman树的算法struct HuffmanNode
{
int data;
HuffmanNode *leftChild,*rightChild;
HuffmanNode():leftChild(NULL),rightChild(NULL){}
HuffmanNode(int value):data(value),leftChild(NULL),rightChild(NULL){}
bool operator<=(HuffmanNode& R){return data <= R.data;}
bool operator>(HuffmanNode& R){return data > R.data;}
bool operator>=(HuffmanNode& R){return data >= R.data;}
};
void mergeTree(HuffmanNode& bt1,HuffmanNode& bt2,HuffmanNode*& parent)//合并
{
parent = new HuffmanNode;
parent->leftChild = &bt1;
parent->rightChild = &bt2;
parent->data = bt1.data+bt2.data;
}
HuffmanNode*& HuffmanTree(int value[],int n)
{
MinHeap<HuffmanNode> min_heap;
HuffmanNode first,second,*parent;
for(int i=0;i<n;i++){
HuffmanNode temp = HuffmanNode(value[i]);
min_heap.Insert(temp);
}
for(int i=0;i<n-1;i++){
min_heap.Remove(first);
min_heap.Remove(second);
mergeTree(first,second,parent);
min_heap.Insert(*parent);
}
return parent;
}
//运行时程序出错