c/c++ | 优先队列 | 大根堆、小根堆

什么是大根堆、小根堆呢?

大根堆,就是一个节点个数为k的二叉树结构,节点元素的val 按照根左右的顺序,所以根节点上的val是最大的值,而 最后的孩子节点中最右边的节点的val 是最小的值。

小根堆,就是元素的值排列相反,根节点上的val 是最小的值,最后孩子节点中的最右边的节点的val 是最大的值

有什么应用吗?常常在找前 k 个最大值/最小值 数据存储等场景

注意遍历 大根堆/小根堆 的元素 需要 top、pop两个函数结合使用

c++ 复制代码
/*
大根堆 参考
*/
#include <iostream>
#include <vector>
#include <algorithm>  // 包含 std::make_heap, std::push_heap, std::pop_heap 等函数

template <typename T, typename Container = std::vector<T>, typename Compare = std::less<typename Container::value_type>>
class priority_queue {
public:
    // 构造函数
    priority_queue(const Compare& comp = Compare(), const Container& ctnr = Container())
        : c(ctnr), comp(comp) {
        std::make_heap(c.begin(), c.end(), comp);
    }

    // 插入元素
    void push(const T& value) {
        c.push_back(value);
        std::push_heap(c.begin(), c.end(), comp);
    }

    // 移除堆顶元素
    void pop() {
        std::pop_heap(c.begin(), c.end(), comp);
        c.pop_back();
    }

    // 访问堆顶元素
    const T& top() const {
        return c.front();
    }

    // 判断堆是否为空
    bool empty() const {
        return c.empty();
    }

    // 获取堆中元素个数
    std::size_t size() const {
        return c.size();
    }

private:
    Container c;       // 存储元素的容器
    Compare comp;      // 比较函数对象
};

int main() {
    priority_queue<int> maxHeap;  // 默认是大根堆

    maxHeap.push(3);
    maxHeap.push(1);
    maxHeap.push(4);
    maxHeap.push(1);
    maxHeap.push(5);

    std::cout << "Max Heap elements: ";
    while (!maxHeap.empty()) {
        std::cout << maxHeap.top() << " ";
        maxHeap.pop();
    }
    std::cout << std::endl;

    return 0;
}
c++ 复制代码
/*
小根堆  参考
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>  // std::greater

template <typename T, typename Container = std::vector<T>, typename Compare = std::less<typename Container::value_type>>
class priority_queue {
public:
    // 构造函数
    priority_queue(const Compare& comp = Compare(), const Container& ctnr = Container())
        : c(ctnr), comp(comp) {
        std::make_heap(c.begin(), c.end(), comp);
    }

    // 插入元素
    void push(const T& value) {
        c.push_back(value);
        std::push_heap(c.begin(), c.end(), comp);
    }

    // 移除堆顶元素
    void pop() {
        std::pop_heap(c.begin(), c.end(), comp);
        c.pop_back();
    }

    // 访问堆顶元素
    const T& top() const {
        return c.front();
    }

    // 判断堆是否为空
    bool empty() const {
        return c.empty();
    }

    // 获取堆中元素个数
    std::size_t size() const {
        return c.size();
    }

private:
    Container c;       // 存储元素的容器
    Compare comp;      // 比较函数对象
};

int main() {
    // 使用 std::greater 作为比较函数,构造小根堆
    priority_queue<int, std::vector<int>, std::greater<int>> minHeap;

    minHeap.push(3);
    minHeap.push(1);
    minHeap.push(4);
    minHeap.push(1);
    minHeap.push(5);

    std::cout << "Min Heap elements: ";
    while (!minHeap.empty()) {
        std::cout << minHeap.top() << " ";
        minHeap.pop();
    }
    std::cout << std::endl;

    return 0;
}
c++ 复制代码
#include <iostream>
#include <queue>
using namespace std;
int my_array[10] = {3,5,6,2,1,-8,10,4,-7,-6};
int main()
{
    //priority_queue<int, vector<int>, greater<int>> q;     //小根堆  3 2 1
    priority_queue<int> q;          //  大根堆  1 2 3
 
    for (int i=0;i<10;i++)
    {
        q.push(my_array[i]);
    }
 
    for (int i = 0; i < 10; i++)
    {
        cout << "order: " << q.top() << endl;
        q.pop();
    }
    
    return 0;
}
相关推荐
xlp666hub2 小时前
Leetcode第七题:用C++解决接雨水问题
c++·leetcode
肆忆_3 小时前
实战复盘:手写 C++ 虚拟机的高性能并行 GC (Thread Pool + Work Stealing)
c++
肆忆_3 小时前
虚函数进阶答疑:把上一篇博客评论区里最容易卡住的问题,一次追到底
c++
saltymilk20 小时前
使用 C++ 模拟 ShaderLanguage 的 swizzle
c++·模板元编程
xlp666hub1 天前
Leetcode第五题:用C++解决盛最多水的容器问题
linux·c++·leetcode
得物技术1 天前
搜索 C++ 引擎回归能力建设:从自测到工程化准出|得物技术
c++·后端·测试
xlp666hub2 天前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
会员源码网2 天前
构造函数抛出异常:C++对象部分初始化的陷阱与应对策略
c++
xlp666hub2 天前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
不想写代码的星星2 天前
static 关键字:从 C 到 C++,一篇文章彻底搞懂它的“七十二变”
c++