参考网上代码,其中实现哈弗曼树的地方,使用了贪心准则。
template<typename Type>class BinaryTree; template<typename Type>void Huffman(Type *,int,BinaryTree<Type> &); template<typename Type>class BinTreeNode { public: friend class BinaryTree<Type>; friend void Huffman<Type>(Type *,int,BinaryTree<Type> &); BinTreeNode():m_pleft(NULL),m_pright(NULL){} BinTreeNode(Type item,BinTreeNode<Type> *left=NULL,BinTreeNode<Type> *right=NULL) :m_data(item),m_pleft(left),m_pright(right){} void Destroy(){ //destroy the tree with the root of the node if(this!=NULL) { this->m_pleft->Destroy(); this->m_pright->Destroy(); delete this; } } Type GetData() { return m_data; } BinTreeNode<Type> *Copy(const BinTreeNode<Type> *copy); //copy the node private: BinTreeNode<Type> *m_pleft,*m_pright; Type m_data; }; template<typename Type>BinTreeNode<Type>* BinTreeNode<Type>::Copy(const BinTreeNode<Type> *copy) { if(copy==NULL) { return NULL; } BinTreeNode<Type> *temp=new BinTreeNode<Type>(copy->m_data); temp->m_pleft=Copy(copy->m_pleft); temp->m_pright=Copy(copy->m_pright); return temp; }
#include "BinTreeNode.h" template<typename Type>void Huffman(Type *,int,BinaryTree<Type> &); template<typename Type>class BinaryTree { public: BinaryTree(BinaryTree<Type> &bt1,BinaryTree<Type> &bt2) { m_proot=new BinTreeNode<Type>(bt1.m_proot->m_data+bt2.m_proot->m_data,bt1.m_proot,bt2.m_proot); } BinaryTree(Type item) { m_proot=new BinTreeNode<Type>(item); } BinaryTree(const BinaryTree<Type> ©) { this->m_proot=copy.m_proot; } BinaryTree() { m_proot=NULL; } void Destroy() { m_proot->Destroy(); } ~BinaryTree() { } BinaryTree<Type>& operator=(BinaryTree<Type> copy); friend void Huffman<Type>(Type *,int,BinaryTree<Type> &); friend bool operator < <Type>(BinaryTree<Type> &l, BinaryTree<Type> & r); friend bool operator > <Type>(BinaryTree<Type> &l, BinaryTree<Type> & r); friend bool operator <= <Type>(BinaryTree<Type> &l, BinaryTree<Type> & r); friend ostream& operator<< <Type>(ostream& ,BinaryTree<Type>&); //output the data private: BinTreeNode<Type> *m_proot; void Print(BinTreeNode<Type> *start,int n=0); //print the tree with the root of start }; template<typename Type> bool operator <(BinaryTree<Type> &l, BinaryTree<Type> &r){ return l.m_proot->GetData() < r.m_proot->GetData(); } template<typename Type> bool operator >(BinaryTree<Type> &l, BinaryTree<Type> &r){ return l.m_proot->GetData() > r.m_proot->GetData(); } template<typename Type> bool operator <=(BinaryTree<Type> &l, BinaryTree<Type> &r){ return l.m_proot->GetData() <= r.m_proot->GetData(); } template<typename Type> void BinaryTree<Type>::Print(BinTreeNode<Type> *start, int n){ if(start==NULL){ for(int i=0;i<n;i++){ cout<<" "; } cout<<"NULL"<<endl; return; } Print(start->m_pright,n+1); //print the right subtree for(int i=0;i<n;i++){ //print blanks with the height of the node cout<<" "; } if(n>=0){ cout<<start->m_data<<"--->"<<endl;//print the node } Print(start->m_pleft,n+1); //print the left subtree } template<typename Type> ostream& operator<<(ostream& os,BinaryTree<Type>& out){ out.Print(out.m_proot); return os; } template<typename Type> BinaryTree<Type>& BinaryTree<Type>::operator=(BinaryTree<Type> copy){ m_proot=m_proot->Copy(copy.m_proot); return *this; }
#include "BinaryTree.h" #include "MinHeap.h" template<typename Type> void Huffman(Type *elements, int n, BinaryTree<Type> &tree){ BinaryTree<Type> first, second; BinaryTree<Type> node[20]; for (int i=0; i<n; i++){ node[i].m_proot = new BinTreeNode<Type>(elements[i]); } MinHeap<BinaryTree<Type> > heap(node, n); for (int i=0; i<n-1; i++){ heap.DeleteMin(first); heap.DeleteMin(second); //using the first and the second minimize element create new tree if (first.m_proot->GetData() == second.m_proot->GetData()){ tree = *(new BinaryTree<Type>(second, first)); } else { tree = *(new BinaryTree<Type>(first, second)); } heap.Insert(tree); } }
测试结果图: