码迷,mamicode.com
首页 > 其他好文 > 详细

【数据结构】优先级队列的实现

时间:2016-04-27 07:09:59      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:数据结构   优先级队列的实现   堆的应用   

建立PriorityQueue.hpp:

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
#include<assert.h>
#include<vector>


template<class T>
struct Less
{
    bool operator() (const T& l, const T& r)
    {
        return l < r;
    }
};

template<class T>
struct Greater
{
    bool operator() (const T& l, const T& r)
    {
        return l>r;
    }
};


template<class T,template<class> class Compare=Less>
class Heap
{
public:
    Heap()
        :_a(NULL)
    {}


    Heap(const T* a,size_t size)
    {
        for (int i = 0; i < size; i++)
        {
            _a.push_back(a[i]);
        }
        for (int i = (size - 2) / 2; i >= 0; i--)
        {
            _adjustDown(i);
        }
    }


    void _adjustDown(size_t parent)
    {
        Compare<T> com;
        size_t child = 2 * parent + 1;
        size_t size = _a.size();
        while (child<size)
        {
            if (child + 1<size && com(_a[child + 1],_a[child]))
            {
                ++child;
            }
            if (com(_a[child],_a[parent]))
            {
                swap(_a[child], _a[parent]);
                parent = child;
                child = 2 * parent + 1;
            }
            else
            {
                break;
            }
        }
    }


    void Push(const T& x)
    {
        _a.push_back(x);
        _adjustUp(_a.size()-1);
    }


    void _adjustUp(size_t child)
    {
        int parent = (child - 1) / 2;
        Compare<T> com;
        while (child>0)
        {
            if (com(_a[child],_a[parent]))
            {
                swap(_a[parent], _a[child]);
                child = parent;
                parent = (child - 1) / 2;
            }
            else
            {
                break;
            }
        }
    }


    size_t Size()
    {
        size_t size = _a.size();
        return size;
    }


    bool Empty()
    {
        assert(Size() >= 0);
        return Size() == 0;
    }


    void Pop()
    {
        assert(_a.Size() > 0);
        swap(_a[0], _a[Size - 1]);
        _a.pop_back();
        _adjustDown(0);        
    }


    T& GetTop()
    {
        return _a[0];
    }


    void Print()
    {
        cout << "大堆序列:" << endl;
        for (int i = 0; i < _a.size(); i++)
        {
            cout << _a[i] << "  ";
        }
        cout << endl;
    }
public:
    vector<T> _a;
};


template<class T, template<class> class Compare = Less>
class PriorityQueue
{
public:
    void Push(const T& x)
    {
        _hp.Push(x);
    }


    void Pop()
    {
        _hp.Pop();
    }


    size_t Size()
    {
        return _hp.Size();
    }


    bool Empty()
    {
        _hp.Empty();
    }


    T& Top()
    {
        return _hp.GetTop();
    }


    void Print()
    {
        _hp.Print();
    }
private:
    Heap<T,Compare> _hp;
};

建立PriorityQueue.cpp文件:

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include "PriorityQueue.hpp"

void Test()
{
    int a[] = { 10, 11, 13, 12, 16, 18, 15, 17, 14, 19 };
    PriorityQueue<int,Greater> pq;
    pq.Push(10);
    pq.Push(11);
    pq.Push(13);
    pq.Push(12);
    pq.Push(16);
    pq.Push(18);
    pq.Push(15);
    pq.Push(17);
    pq.Push(14);
    pq.Push(19);

    pq.Print();
}


int main()
{
    Test();
    system("pause");
    return 0;
}

本文出自 “Han Jing's Blog” 博客,请务必保留此出处http://10740184.blog.51cto.com/10730184/1768025

【数据结构】优先级队列的实现

标签:数据结构   优先级队列的实现   堆的应用   

原文地址:http://10740184.blog.51cto.com/10730184/1768025

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!