哈夫曼树Huffmantree

实现哈夫曼树,首先要了解这些知识:

路径长度:一个节点到另一个节点的边数;
二叉树的路径长度(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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章