C++ 优先队列实现最大堆和最小堆

C++ 优先队列实现最大堆和最小堆

优先级队列

template <class T, class Container = vector<T>,
  class Compare = less<typename Container::value_type> > class priority_queue;

C++ STL实现的优先级队列是一种容器适配器,类似堆,可以随时插入元素,但只能检索到优先级队列中优先级最高的元素。

Priority queues are a type of container adaptors, specifically designed such that its first element is always the greatest of the elements it contains, according to some strict weak ordering criterion.

This context is similar to a heap, where elements can be inserted at any moment, and only the max heap element can be retrieved (the one at the top in the priority queue).

Priority queues are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are popped from the “back” of the specific container, which is known as the top of the priority queue.

参数

  • T : 元素的类型
  • Container : 存储元素的内部基础容器对象的类型,默认为vector
  • Compare : 元素优先级比较类,类内重载了operator()函数,默认为less

成员函数

(constructor) 构造函数
empty() 优先级队列是否为空
size() 返回优先级队列当前包含的元素个数
top() 返回堆顶元素
push() 插入元素
emplace()C++11 就地构造元素并插入
pop() 弹出堆顶元素
swap()C++11 交换两个优先级队列中的所有元素

C++ STL比较类

priority_queue<int, vector<int>, greater<int> > minHeap;
priority_queue<int, vector<int>, less<int> > maxHeap;

less结构体

template<typename _Tp>
    struct less : public binary_function<_Tp, _Tp, bool>
    {
    
    
      bool
      operator()(const _Tp& __x, const _Tp& __y) const
      {
    
     return __x < __y; }
    };

greater结构体

template<typename _Tp>
    struct greater : public binary_function<_Tp, _Tp, bool>
    {
    
    
      bool
      operator()(const _Tp& __x, const _Tp& __y) const
      {
    
     return __x > __y; }
    };

重载小于运算符

struct MaxHeapNode
{
    
    
    int id;
    int value;
    bool operator < (const MaxHeapNode& x) const {
    
    
        return value < x.value;
    }
};

struct MinHeapNode {
    
    
    int id;
    int value;
    bool operator < (const MinHeapNode& x) const {
    
    
        return value > x.value;
    }
};

priority_queue<MaxHeapNode> maxNodeHeap;
priority_queue<MinHeapNode> minNodeHeap;

实现比较类


class Comparison {
    
    
    bool reverse;
public:
    Comparison(const bool& reverseParameter = false) : reverse(reverseParameter) {
    
    }
    bool operator() (const int& lhs, const int& rhs) const {
    
    
    	// reverse == true : max heap
    	// reverse == false : min heap
        return reverse ? lhs > rhs : lhs < rhs;
    }
};

priority_queue<int, vector<int>, Comparison> myMaxHeap;
priority_queue<int, vector<int>, Comparison> myMinHeap(true);

实现代码

/*
author : eclipse
email  : [email protected]
time   : Sun Jan 31 19:47:13 2021
*/
#include<bits/stdc++.h>
using namespace std;

struct MaxHeapNode
{
    
    
    int id;
    int value;
    bool operator < (const MaxHeapNode& x) const {
    
    
        return value < x.value;
    }
};

struct MinHeapNode {
    
    
    int id;
    int value;
    bool operator < (const MinHeapNode& x) const {
    
    
        return value > x.value;
    }
};

class Comparison {
    
    
    bool reverse;
public:
    Comparison(const bool& reverseParameter = false) : reverse(reverseParameter) {
    
    }
    bool operator() (const int& lhs, const int& rhs) const {
    
    
        return reverse ? lhs > rhs : lhs < rhs;
    }
};

int main(int argc, char const *argv[]) {
    
    
    priority_queue<int, vector<int>, greater<int> > minHeap;
    priority_queue<int, vector<int>, less<int> > maxHeap;
    priority_queue<MaxHeapNode> maxNodeHeap;
    priority_queue<MinHeapNode> minNodeHeap;
    priority_queue<int, vector<int>, Comparison> myMaxHeap;
    priority_queue<int, vector<int>, Comparison> myMinHeap(true);

    for (int i = 0; i < 16; i++) {
    
    
        minHeap.push(i);
        maxHeap.push(i);
        maxNodeHeap.push((MaxHeapNode) {
    
    i, i});
        minNodeHeap.push((MinHeapNode) {
    
    i, i});
        myMaxHeap.push(i);
        myMinHeap.push(i);
    }

    while (!minHeap.empty()) {
    
    
        printf("%d ", minHeap.top());
        minHeap.pop();
    }

    printf("\n");

    while (!maxHeap.empty()) {
    
    
        printf("%d ", maxHeap.top());
        maxHeap.pop();
    }

    printf("\n");

    while (!maxNodeHeap.empty()) {
    
    
        printf("%d ", maxNodeHeap.top().value);
        maxNodeHeap.pop();
    }

    printf("\n");

    while (!minNodeHeap.empty()) {
    
    
        printf("%d ", minNodeHeap.top().value);
        minNodeHeap.pop();
    }

    printf("\n");

    while (!myMaxHeap.empty()) {
    
    
        printf("%d ", myMaxHeap.top());
        myMaxHeap.pop();
    }

    printf("\n");

    while (!myMinHeap.empty()) {
    
    
        printf("%d ", myMinHeap.top());
        myMinHeap.pop();
    }

    return 0;
}

测试结果

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

参考

std::priority_queue

最后

  • 由于博主水平有限,不免有疏漏之处,欢迎读者随时批评指正,以免造成不必要的误解!

猜你喜欢

转载自blog.csdn.net/qq_44486439/article/details/113484234
今日推荐