1、二叉堆的操作

(1)取出元素

(2)插入元素

(3)删除元素

//手写大根堆维护 
#include<bits/stdc++.h>
using namespace std;
int heap[1000];
int up(int x)
{
    if(heap[x]<=heap[x/2])
    return 0;
    else
    {
        swap(heap[x],heap[x/2]);
        up(x/2);
    }
}
int down(int x)
{
    if(heap[x]>=heap[x*2]&&heap[x]>=heap[x*2+1])
    return 0;
    else
    if(heap[x*2]>heap[x*2+1])
    {
        swap(heap[x],heap[x*2]);
        down(x*2);
    }
    else
    {
        swap(heap[x],heap[x*2+1]);
        down(x*2+1);
    }
}
int m,od,x,n;
int main()
{
    scanf("%d",&m);
    while(m--)
    {
        scanf("%d",&od);
        if(od==1)
        {
            printf("%d",heap[0]);
        }
        if(od==2)
        {
            scanf("%d",&x);
            n++;
            heap[n]=x;
            up(n);
        }
        if(od==3)
        {
            heap[0]=heap[n];
            n--;
            down(1);
        }
    }
    return 0;
}
手写大根堆

相关文章:

  • 2021-04-05
  • 2021-05-28
  • 2022-12-23
  • 2021-09-25
  • 2021-10-09
  • 2022-12-23
  • 2021-12-31
  • 2021-07-04
猜你喜欢
  • 2021-07-15
  • 2022-12-23
  • 2021-05-11
  • 2021-06-25
  • 2021-08-01
  • 2021-08-06
相关资源
相似解决方案