预备知识:优先级队列(二叉堆)

二叉堆属性

在这里插入图片描述

题目描述

在这里插入图片描述

C++代码

//
// Created by YaMiwan on 2020-05-04.
//


#include <iostream>
#include <queue>

using namespace std;

int main() {
    priority_queue<int> big_heap;  // 默认构造是最大堆
    priority_queue<int, vector<int>, greater<int>> small_heap;  // 最小堆构造方法,使用vector和greater实现模板类
    priority_queue<int, vector<int>, less<int>> big_heap2;  // 最大堆构造方法

    if (big_heap.empty()) {
        printf("big_heap is empty!\n");
    }
    int test[] = {6, 10, 1, 7, 99, 4, 33};
    for (int i = 0; i < 7; i++) {
        big_heap.push(test[i]);
    }
    printf("big_heap.top = %d\n", big_heap.top());
    big_heap.push(1000);
    printf("big_heap.top = %d\n", big_heap.top());
    for (int i = 0; i < 3; i++) {
        big_heap.pop();
    }
    printf("big_heap.top = %d\n", big_heap.top());
    printf("big_heap.size = %d\n", big_heap.size());
    return 0;
}

输出

big_heap is empty!
big_heap.top = 99
big_heap.top = 1000
big_heap.top = 10
big_heap.size = 5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值