堆(优先队列)

21 篇文章 0 订阅
15 篇文章 0 订阅

优先队列

简介

是一个完全二叉树(除最后一行外,其余部分为满二叉树),且父亲永远比孩子大或小。
用一个数组实现i的孩子为i*2和i*2+1,父亲为i/2。
在实际操作中可以用priority_queue<类型>来表示大根堆
用priority_queue<类型,vector<类型>,greater<类型> >来表示小根堆
注意:让结构体如队列时首先要定义它的比较方式

作用

1.迅速输出最大值,用pop输出数组的第一位
输出第一个,并把最后一个提上来,并比较使之合法
2.用——.push()输入
先加到数组尾,再与父亲比较,过大则交换
3.用——.empty()可以判断其中还有没有数。
4.用——.init()初始化

代码(手写堆1)

#include <bits/stdc++.h>
using namespace std;

const int N = 300000;

struct Heap {
    int heap[N + 10];
    int size;

    void init() {
        size = 1;
    }

    void push (int x) {
        int i = size++;
        while (i > 1) {
            int p = (i >> 1);
            if (heap[p] > x) {
                break;
            }
            heap[i] = heap[p];
            i = p;
        }
        heap[i] = x;
    }

    int pop() {
        int ret = heap[1];
        int x = heap[--size];

        int i = 1;
        while ((i << 1) < size) {
            int lson = (i << 1);
            int rson = (i << 1 | 1);
            if (rson < size && heap[rson] > heap[lson]) lson = rson;

            if (heap[lson] < x) {
                break;
            }

            heap[i] = heap[lson];
            i = lson;
        }
        heap[i] = x;
        return ret;
    }

    bool empty() {
        return size == 0;
    }
};

int a[N];

int main () {

    srand(time(NULL));


    Heap my_heap;
    my_heap.init();
    for (int i = 0; i < N; i++) {
        a[i] = rand()*rand();
    }

//start here
    clock_t mystart = clock();
    for (int i = 0; i < N; i++) {
        my_heap.push(a[i]);
    }

    while(!my_heap.empty()) {
        my_heap.pop();
    }

    clock_t myend = clock();
//end here

    priority_queue<int> heap;

    clock_t start = clock();
    for (int i = 0; i < N; i++) {
        heap.push(a[i]);
    }

    while(!heap.empty()) {
       // cout << heap.top() << endl;
        heap.pop();
    }

    clock_t end = clock();
    cout << "Running time of xjoi machine is:" << static_cast<double> (myend-mystart)/CLOCKS_PER_SEC*1000 << "ms" << endl;
    cout << "Running time stl is:" << static_cast<double> (end-start)/CLOCKS_PER_SEC*1000 << "ms" << endl;

    return 0;
}

代码(手写堆2,加入一些补充)

struct Heap
{
    int num[N],size;
    void init()
    {
        int i;
        size=0;
        for(i=0;i<=N-10;i++) num[i]=M;
    }

    void pop()
    {
        int i,j,now=1;
        num[1]=num[size];
        num[size]=M;
        size--;
        while(num[now]>min(num[now*2],num[now*2+1]))
        {
            if(num[now*2]<num[now*2+1])
            {
                swap(num[now*2],num[now]);
                now*=2;
            }
            else
            {
                swap(num[now*2+1],num[now]);
                now=now*2+1;
            }
        }
    }

    void push(int u)
    {
        int now;
        size++;
        num[size]=u;
        now=size;
        if(now!=1)
        while(num[now]<num[now/2])
        {
            swap(num[now],num[now/2]);
            now/=2;
            if(now==1) break;
        }
    }

    int top()
    {
        return num[1];
    }

    int second()
    {
        return min(num[2],num[3]);
    }

    bool empty()
    {
        if(size==0) return 1;
        else return 0;
    }

    void print()
    {
        int i;
        for(i=1;i<=size;i++)
        {
            printf("%d ",num[i]);
        }
    }
};
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值