# 最小堆应用---用最小堆实现huffman树

#include"MinHeap.h"

template<class T> class HuffmanTree;
template<class T>
class TreeNode{
friend class HuffmanTree<T>;
private:
T data;
TreeNode<T> *left,*right;
public:
TreeNode(T value){
data = value;
left = right = NULL;
}
TreeNode(){
left = right = NULL;
}
bool operator > (const TreeNode &node){
return data > node.data;
}
bool operator < (const TreeNode &node){
return data < node.data;
}
bool operator == (const TreeNode &node){
return data == node.data;
}
bool operator >= (const TreeNode &node){
return data >= node.data;
}
};

template <class T>
class HuffmanTree{
public:
HuffmanTree();
HuffmanTree(T value[],int n);
protected:
TreeNode<T> *JoinTree(TreeNode<T> &node1,TreeNode<T> &node2);
TreeNode<T> *root;
};

template<class T>
HuffmanTree<T>::HuffmanTree():root(NULL){
}

template<class T>
HuffmanTree<T>::HuffmanTree(T value[],int n):root(NULL){
TreeNode<T> *nodes = new TreeNode<T>[n];
TreeNode<T> leftNode,rightNode;
int i = 0;
for(i = 0; i < n; i++){
nodes[i] = TreeNode<T>(value[i]);
}
MinHeap< TreeNode<T> > *m_heap = new MinHeap< TreeNode<T> >(nodes,n);

for(i = 0; i < n-1; i++){
m_heap->RemoveMin(leftNode);
m_heap->RemoveMin(rightNode);
root = JoinTree(leftNode,rightNode);
m_heap->Insert(*root);
}
}

template<class T>
TreeNode<T> *HuffmanTree<T>::JoinTree(TreeNode<T> &node1,TreeNode<T> &node2){
TreeNode<T> *r = new TreeNode<T>;
r->left = &node1;
r->right = &node2;
r->data = node1.data + node2.data;
return r;
}

#### [C++]数据结构：使用最小堆创建霍夫曼编码树HuffmanTree的过程

2012-12-16 13:01:06

#### 【数据结构】最小堆实现哈夫曼树构造与解码

2015-09-08 11:34:53

#### 最小生成树--Kruskal算法

2018-03-26 21:42:46

#### Kruskal算法详细分析

2017-02-28 12:25:52

#### 最小生成树之Kruskal算法

2015-08-18 08:12:47

#### Kruskal算法（贪心+并查集=最小生成树）

2016-04-16 17:34:35

#### 最小生成树-Kruskal算法（模板）

2016-08-25 10:49:07

#### Kruskal算法的C语言程序

2017-02-06 23:14:24

#### Kruskal算法介绍与实现

2014-04-12 20:21:59

#### 有关最小生成树Kruskal正确性的证明与研究

2017-04-16 16:09:47