模拟实现堆

   堆分为最小堆和最大堆。
   最小堆:堆顶的关键码最小,每个节点的关键码小于该节点的左右孩子结点,并且从根节点到每个节点的路径上,关键码依次递增。
   最大堆:堆顶的关键码最大,每个节点的关键码大于该节点的左右孩子结点,并且从根节点到每个节点的路径上,关键码依次递减。

将一棵二叉树调整为最小堆的方法:
1.比较左右孩子的关键码大小,较小的用child标记
2.比较child和parent,如果child< parent,将它们交换
3.使用循环依次类推,调整至最小堆
同样,将二叉树调整为最大堆方法类似。

所以,我们使用模板参数列表来控制大小堆,代码如下:

#include<ostream>
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;
};

测试代码:

void Test()
{
    int array[]={53,17,78,9,45,65,87,23};
    Heap<int,Greater<int>> hp(array,sizeof(array)/sizeof(array[0]));
    hp.Insert(4);
    hp.Remove();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值