2021-01-22

这篇博客介绍了如何使用C++实现小根堆。通过`MinHeap`类,展示了构造函数、插入元素、删除最小元素以及输出堆中元素等方法的详细实现。示例代码包括了创建堆、插入元素、删除最小元素并输出堆内容的过程。
摘要由CSDN通过智能技术生成

小根堆的实现

#include<iostream>

using namespace std;

#define DefaultSize 50

 

template<typename T>

class MinHeap

{

public:

    MinHeap(int sz=DefaultSize)

    {

        maxHeapSize=(DefaultSize<sz)?sz:DefaultSize;

        heap=new T[maxHeapSize];

        currentSize=0;

    }

    MinHeap(T arr[],int n)

    {

        maxHeapSize=(Default<n)?n:DefaultSize;

        heap=new T[maxHeapSize];

        for(int i=0;i<n;i++){

            heap[i]=arr[i];

        }

        currentSize=n;

        int currentpos=(currentSize-2)/2;//从最后一层开始向下调整,直到第一行

        while(currentpos>=0){

            shiftDown(currentpos,currentSize-1);

            currentpos--;

        }

    }

    bool insert(const T& x)

    {

        if(currentSize==maxHeapSize)

        {

            cout<<"Heap Full"<<endl;

            return false;

        }

        heap[currentSize]=x;

        shiftup(currentSize);

        currentSize++;

        return true;

    }

    bool removemin(T& x){

        if(!currentSize)

        {

            cout<<"Heap Empty!"<<endl;

            return false;

        }

        x=heap[0];

        heap[0]=heap[currentSize-1];

        currentSize--;

        shiftdown(0,currentSize-1);

        return true;

    }

    void output(){

        for(int i=0;i<currentSize;i++)

        {

            cout<<heap[i]<<" ";

        }

        cout<<endl;

    }

protected:

    void shiftdown(int start,int end)

    {

        int cur=start;

        int min_child=2*child+1;

        T temp=heap[cur];

        while(cur<=end){

            if(min_child<end&&heap[min_child]>heap[min_child++1])

            min_child++;

            if(temp<=heap[min_child])

            break;

            else

            {

                heap[cur]=heap[min_child];

                cur=min_child;

                min_child=2*min_child+1;

            }

            

        }

        heap[cur]=temp;

    }

    void shiftup(int start){

        int cur=start;

        int parent=(cur-1)/2;

        T temp=heap[cur];

        while(cur>0){

            if(heap[parent]<=temp)

            break;

            else{

                heap[cur]=heap[parent];

                cur=parent;

                parent=(parent-1)/2;

            }

        }

        heap[cur]=temp;

    }

private:

    T* heap;

    int currentSize;

    int maxHeapSize;

};

int main(int argc, char* argv[])

{

    MinHeap<int> h;

    h.Insert(8);

    h.Insert(5);

    h.Insert(7);

    h.Insert(9);

    h.Insert(6);

    h.Insert(12);

    h.Insert(15);

    h.output();

 

    int out;

    cout << static_cast<int> (h.RemoveMin(out)) << endl;

    h.output();

 

    int arr[10] = { 15,19,13,12,18,14,10,17,20,11 };

    MinHeap<int> h1(arr,10);

    h1.output();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值