C++快速实现STL中的优先队列

数据结构课设被我们做成了造轮子课设。为了堆优化的Dijkstra算法,我把优先队列顺便实现了一下。废话不多说,直接上代码。

我写的是小顶堆,如果你需要使用大顶堆,有两种方法。1. 将程序中的几个<改成> 2. 存入的数据全部取负数,需要拿出来时再取负数

由于STL中的priority_queue是通过vector实现的,所以本处代码中优先队列数据的载体也是vector

先上头文件和类声明:

#include "iostream"
#include "vector"
using namespace std;

template<class DataType>
class priority
{
    
    
    public:
        vector<DataType> a;

        priority();		// 构造函数
        ~priority();	// 析构函数

        int size();		// 获取优先队列中数据的个数
        bool empty();	// 判断优先队列是否为空
        DataType top();	// 获取队首元素(堆顶元素)
        DataType pop();	// 弹出队首元素
        void push(const DataType &value);	// 元素入队
        void adjust(int low, int high);		// 调整堆(这是辅助函数,不会作为功能函数呗用户调用)
};

通过堆的基本操作实现。

插入操作(push)的逻辑为先将需要插入的元素置于队尾,也就是堆对应的二叉树的最后一个父节点那里,然后从该节点开始,自下而上地调整二叉树。调整的逻辑很简单:将该节点与它的父节点比较,如果比它的父节点小,那么说明该节点应该充当当前这个二叉子树的根节点以满足小顶堆的定义,因此,我们会将当前节点与它的父亲节点交换位置,然后递归做下去,直到已经成为根节点(j==0);如果该节点比它的父节点大,那么说明我们草率的放入末尾不会影响当前二叉树还是一个小根堆,此时直接结束即可。

弹出操作的逻辑很简单,做法与插入的相反,由于我们在弹出之前的队列已经是优先队列了,全队列的最小值已经在首元素了,那么我们需要返回和销毁的,就是队首元素。由于vector的实现原理是线性表,线性表直接销毁首地址元素是通过元素的移动实现的,那么我们每pop一下队首元素的时间复杂度都是O(n),这不划算。因此,我们考虑先将队首元素与队尾元素交换一下,然后将队尾元素作为pop()的返回值返回,然后销毁队尾元素,最后调用adjust()函数重新使得剩余的n-1个元素保持小顶堆。

当然,由于return操作会使得当前函数直接结束,因此,实际编写时的顺序应该为:

保存队首元素
交换队首与队尾元素
销毁队尾元素
调整小根堆
返回保存的值

代码如下:

template<class DataType>
priority<DataType>::priority()  {
    
    }

template<class DataType>
priority<DataType>::~priority() {
    
    this->a.clear();}

template<class DataType>
int priority<DataType>::size()
{
    
    
    return this->a.size();
}

template<class DataType>
bool priority<DataType>::empty()
{
    
    
    if (this->size())
        return false;
    else    
        return true;
}

template<class DataType>
DataType priority<DataType>::top()
{
    
    
    return this->a.front();
}

template<class DataType>
DataType priority<DataType>::pop()
{
    
    
    DataType temp = this->a.front();
    swap(this->a.front(), this->a.back());
    this->a.pop_back();
    this->adjust(0, this->size() - 1);
    return temp;
}

template<class DataType>
void priority<DataType>::push(const DataType &value)
{
    
    
    this->a.push_back(value);
    int i = this->a.size() - 1, j = (i - 1) / 2;
    while (j >= 0)
    {
    
    
        if (this->a[i] < this->a[j])
        {
    
    
            swap(this->a[i], this->a[j]);
            i = j, j = (i - 1) / 2;
        }
        else
            break;
    }
}

template<class DataType>
void priority<DataType>::adjust(int low, int high)
{
    
    
    int temp = a[low];
    int i = low, j = 2 * i + 1;

    while (j <= high)
    {
    
    
        if (j + 1 <= high && a[j + 1] < a[j])
            j ++;
        
        if (temp <= a[j])
            break;
        
        a[i] = a[j];
        i = j, j = 2 * i + 1;
    }
    a[i] = temp;
}

template<class DataType>
int arraylen(DataType &a)
{
    
    
    return sizeof(a) / sizeof(a[0]);
}

需要说明的是,我们是从0开始存数据的,如果父节点索引为int i, 左子节点索引为int j,那么ij的关系为 i == 2 * j + 1; j == (i - 1) / 2;

测试一下:

template<class DataType>
int arraylen(DataType &a)	// 返回任意一维数组元素个数的函数
{
    
    
    return sizeof(a) / sizeof(a[0]);
}


int main(int argc, char const *argv[])
{
    
    
    int a[] = {
    
    2,1,3,4,6};
    priority<int> q;
    for (int i = 0; i < arraylen(a); ++ i)
        q.push(a[i]);
    
    while (!q.empty())
    {
    
    
        cout << q.pop() << " ";
    }
    return 0;
}

out:

1 2 3 4 6 

猜你喜欢

转载自blog.csdn.net/weixin_45576923/article/details/111933220