實現哈夫曼樹,首先要了解這些知識:
路徑長度:一個節點到另一個節點的邊數;
二叉樹的路徑長度(PL):每個葉子節點到根節點的路徑長度相加;
帶權路徑長度(WPL):每個葉子結點到根節點的路徑乘權值相加之和;
哈夫曼樹:相同節點個數的二叉樹中,WPL最小的二叉樹;
構建哈夫曼樹的方法:
將權值最小的兩個節點作爲左右孩子,它們的權值之和即爲雙親結點的權值,再從權值集合其他結點中選一個最小的結點,和之前的雙親結點組成左右孩子,依次類推,直至鏈接成一棵每個節點都有權值的二叉樹。 可以使用堆實現哈夫曼樹,可以先取最小堆的堆頂元素,這樣很方便就可以實現哈夫曼樹的構建。
代碼:
Heap.hpp
#include<iostream>
using namespace std;
#include<vector>
#include<assert.h>
template<class T>
struct Less//小堆
{
bool operator()(const T& left,const T& right)
{
return left<right;
}
};
template<class T>
struct Greater//大堆
{
bool operator()(const T& left,const T& right)
{
return left>right;
}
};
template<class T,class Comper=Less<T>>
class Heap
{
public:
Heap()
{}
Heap(const T array[],size_t size)
{
for(int idx=0;idx<size;++idx)
_heap.push_back(array[idx]);
int root=(_heap.size()-2)>>1;//找到倒數第一個非葉子結點·
for(;root>=0;--root)
_AdjustDown(root);
}
size_t Size()const
{
return _heap.size();
}
bool Empty()const
{
return _heap.empty();
}
const T& Top()const
{
return _heap[0];
}
void Insert(const T& data)
{
_heap.push_back(data);//直接插入到最後一個元素
if(_heap.size()>1)//排成最小堆/最大堆
_AdjustUp();
}
void Remove()//刪除堆頂
{
assert(!_heap.empty());
std::swap(_heap[0],_heap[_heap.size()-1]);//將堆頂與最後一個結點交換位置
_heap.pop_back();//最後一個結點出堆,即原來的堆頂
if(_heap.size()>1)
{
int root=(_heap.size()-2)>>1;//找到倒數第一個非葉子結點·
for(;root>=0;--root)
_AdjustDown(root);//將整個樹排爲最小堆/最大堆
}
}
protected:
void _AdjustDown(size_t parent)
{
size_t child=parent*2+1;
size_t size=_heap.size();
while(child<size)
{
Comper com;
if(child+1<size && com(_heap[child+1],_heap[child]))//左孩子>右孩子,將右孩子標記爲孩子結點
child+=1;
if(Comper()(_heap[child],_heap[parent]))//孩子結點<小於雙親結點時,將兩結點交換位置
{
std::swap(_heap[child],_heap[parent]);
parent=child;
child=parent*2+1;
}
else
return;
}
}
void _AdjustUp()//向上排成最小/大堆
{
size_t child=_heap.size()-1;
size_t parent=(child-1)>>1;
while(child!=0)
{
if(Comper()(_heap[child],_heap[parent]))
{
std::swap(_heap[child],_heap[parent]);
child=parent;
parent=(child-1)>>1;
}
else
return;
}
}
private:
std::vector<T> _heap;
};
HuffmanTree.hpp
#include<iostream>
using namespace std;
template<class T>
struct HuffmanTreeNode
{
HuffmanTreeNode(const T& weight)
:_weight(weight)
,_pLeft(NULL)
,_pRight(NULL)
,_pParent(NULL)
{}
T _weight;//權值
HuffmanTreeNode<T>* _pLeft;
HuffmanTreeNode<T>* _pRight;
HuffmanTreeNode<T>* _pParent;
};
template<class T>
class HuffmanTree
{
typedef HuffmanTreeNode<T> Node;
public:
HuffmanTree()
: _pRoot(NULL)
{}
HuffmanTree(const T array[], size_t size, const T& invalid)
{
_Create(array,size,invalid);
}
~HuffmanTree()
{
_Destroy(_pRoot);
}
const Node* Root()const
{
return _pRoot;
}
private:
void _Create(const T array[], size_t size, const T& invalid)
{
struct Compare
{
bool operator()(const Node* pLeft,const Node* pRight)
{
return pLeft->_weight<pRight->_weight;
}
};
Heap<Node*,Compare> hp;
for(size_t idx=0;idx<size;++idx)
{
if(array[idx]!=invalid)
hp.Insert(new Node(array[idx]));
}
while(hp.Size()>1)
{
Node* pLeft=hp.Top();//堆頂的兩個結點一定爲權值最小的結點
hp.Remove();
Node* pRight=hp.Top();
hp.Remove();
Node* pParent=new Node(pLeft->_weight+pRight->_weight);
pParent->_pLeft=pLeft;//將左孩子,右孩子,和雙親結點鏈接起來
pParent->_pRight=pRight;
pLeft->_pParent=pParent;
pRight->_pParent=pParent;
hp.Insert(pParent);
}
_pRoot=hp.Top();//pRoot指向哈弗曼樹
}
void _Destroy(Node* & pRoot)
{
if(pRoot)
{
_Destroy(pRoot->_pLeft);
_Destroy(pRoot->_pRight);
delete pRoot;
pRoot=NULL;
}
}
protected:
Node* _pRoot;
};
測試代碼:
#include"Heap.hpp"
#include"HuffmanTree.hpp"
void Test()
{
int array[]={1,3,5,7};
HuffmanTree<int> hf(array,sizeof(array)/sizeof(array[0]),'0');
}
int main()
{
Test();
return 0;
}