czl的知识点整理1——堆

本文深入讲解了堆数据结构的实现原理及应用,包括大顶堆和小顶堆的构造过程,通过具体代码示例展示了如何使用数组实现堆,并提供了一种高效地插入和删除元素的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

知识点整理——堆

首先,废话不多说,给出代码。

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

int n,k,i,maxn;

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

    void init()
    {
        size=1;
    }

    void push(int x)
    {
//      cout<<x<<" ";
        heap[size]=x;
        if(size!=1)
        {
            n=size/2;
            k=size;
            while(n!=0)
            {
                if(heap[k]>heap[n])
                {
                    swap(heap[k],heap[n]);
                    k=n;
                    n=n/2;
                }
                else break;
            }
        }
        size++;
    }

    int pop()
    {
        n=heap[1];
        k=size;
        heap[1]=heap[k];
        i=1;
        while(i*2+1<=size)
        {
            if(heap[i*2]>=heap[i*2+1])
            {
                if(heap[i]<=heap[i*2])
                {
                swap(heap[i],heap[i*2]);
                i=i*2;
                }
                else break;
            }
            else 
            {
                if(heap[i]<=heap[i*2+1])
                {
                swap(heap[i],heap[i*2+1]);
                i=i*2+1;
                }
                else break;
            }
        }
        size--;
        return n;
    }

    bool empty()
    {
        if(size==0)return true;
        else return false;
    }
};
int main()
{
    freopen("out.txt","w",stdout);
    srand(time(NULL));
    Heap my_heap;
    my_heap.init();
    for(int i=1;i<=100000;i++)
    {
        my_heap.push(rand());
    }
//  cout<<endl;
    while(!my_heap.empty())
    {
        cout<<my_heap.pop()<<endl;
    }
    return 0;
}

附上老师代码(可以比较STL和手打代码的时间):

#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;
}
这是大更堆的手打代码,100000的rand的值,从大到小输出只需要0.7秒左右。

思路:

首先,堆的图形是一个完全二叉树,所以我们可以用一个一位数组去存二叉树的每个节点,并且有个规律,如果数组从1开始,那么第i个节点的孩子节点的数组下标分别i*2和i*2+1。如果是个大更堆,就先把输入的数放在叶子节点,然后和他的父亲节点进行比较,如果大于他的父亲节点,就和他的父亲节点进行交换,直到这个数不能交换或者交换到根节点,就停止。输出时,只需弹出根节点,然后把数组最后一个数放到根节点上,和它最大的孩子节点进行比较,如果小于它,就和它交换,直到不能交换为至。这样重复输出,直到数组变成空为止,这样就能输出从大到小的数据。小更堆也是一样的,只是反一下就行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值