C++语言:用最大堆实现优先队列
Vivian 2018-06-04 来源 : 阅读 917 评论 0

摘要:本文主要向大家介绍了C++语言的用最大堆实现优先队列,通过具体的代码向大家展示,希望对大家学习C++语言有所帮助。

    本文主要向大家介绍了C++语言的用最大堆实现优先队列,通过具体的代码向大家展示,希望对大家学习C++语言有所帮助。

#include <iostream>
#include <vector>
using namespace std;
 
template<typename t="">
class PriorityQueue {
    private:
        vector<t> v;
        int size;
        const static int initCap = 100;
    private:
        int parent(int pos) {
            return pos/2;
        }
 
        int left(int pos) {
            return pos*2;
        }
 
        int right(int pos) {
            return pos*2 + 1;
        }
    public:
        PriorityQueue():
            size(0) {
                v = vector<t>(initCap);
            }
 
        bool enQueue(const T& t) {
            size++;
            v[size] = t;
            int i = size;
            //插入到尾部,一直上提到使当前堆符合最大堆性质为止
            while(i > 1 && v[i] > v[parent(i)]) {
                T tmp = v[i];
                v[i] = v[parent(i)];
                v[parent(i)] = tmp;
                i = parent(i);
            }
            return true;
        }
 
        bool deQueue(T& max) {
            //取出最大值,并把尾部元素和最大值交换,然后维护当前堆到符合堆性质
            if(size < 1) {
                return false;
            }
            max = v[1];
            v[1] = v[size];
            v[size] = max;
            size--;
            maxHeapIfy(1);
            return true;
        }
 
        void maxHeapIfy(int pos) {
            int largestPos = pos;
            if(left(pos) <= size && v[pos] < v[left(pos)]) {
                largestPos = left(pos);
            }
            if(right(pos) <= size && v[largestPos] < v[right(pos)]) {
                largestPos = right(pos);
            }
            if(largestPos != pos) {
                T tmp = v[pos];
                v[pos] = v[largestPos];
                v[largestPos] = tmp;
                maxHeapIfy(largestPos);
            }
        }
 
        int getSize() {
            return size;
        }
 
        bool top(T& t) {
            if(size < 1) {
                return false;
            }
            t = v[1];
            return true;
        }
};
 
int main() {
    PriorityQueue<int> pQ;
    pQ.enQueue(8);
    pQ.enQueue(1);
    pQ.enQueue(3);
    pQ.enQueue(6);
    pQ.enQueue(5);
    pQ.enQueue(3);
    pQ.enQueue(10);
    pQ.enQueue(9);
    pQ.enQueue(7);
    int a = -1;
    while(pQ.deQueue(a)) {
        cout<</a<<"></int></t></t></typename></vector></iostream>

本文由职坐标整理并发布,了解更多内容,请关注职坐标编程语言C/C+频道!

本文由 @Vivian 发布于职坐标。未经许可,禁止转载。
喜欢 | 1 不喜欢 | 0
看完这篇文章有何感觉?已经有1人表态,100%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程