首页 > 其他 > 详细

二叉堆模板

时间:2021-04-20 23:14:24      阅读:32      评论:0      收藏:0      [点我收藏+]

二叉堆模板

代码:

#include<bits/stdc++.h>

template<typename T> T max(T a, T b){return (a>b)?a:b;}
template<typename T> T min(T a, T b){return (a<b)?a:b;}

const char endl=‘\n‘;
const int MAXN=1e6+5;
const int inf=0x3f3f3f3f;

#define RE register

template<typename T> void swap(T &a, T &b){T t=a; a=b; b=t;}

struct Min_Heap
{
    int heap[MAXN];
    int heap_size=0;
    int top(){return heap[1];}
    int size(){return heap_size;}
    bool empty(){return (heap_size>0)?true:false;}
    void push(int v)
    {
        heap_size++;
        int p=heap_size;
        heap[p]=v;
        while(p>>1&&heap[p]<heap[p>>1])
        {
            swap(heap[p], heap[p>>1]);
            p>>=1;
        }
    }
    void pop()
    {
        heap[1]=heap[heap_size];
        heap_size--;
        int x=1;
        while((x<<1)<=heap_size)
        {
            int y=x<<1;
            if(y|1<=heap_size&&heap[y]>heap[y|1])
                y|=1;
            if(heap[x]>heap[y])
            {
                swap(heap[x], heap[y]);
                x=y;
            }
            else
                break;
        }
    }
};

使用方法:
待更新

二叉堆模板

原文:https://www.cnblogs.com/vectorli2021/p/14683001.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!