优先级队列

来源:互联网 发布:电视用什么软件看电影 编辑:程序博客网 时间:2024/06/07 13:40

模板函数实现大小堆:

#include <vector>#include <iostream>using namespace std;#include <assert.h>template<class T>struct Less{    bool operator()(const T& left, const T& right)//仿函数    {        return left<right;    }};template<class T>struct Greater{    bool operator()(const T& left, const T& right)//仿函数    {        return left>right;    }};//模板参数template<class T, class Compare = Less<T>>class Heap{public:    // 创建一个空堆    Heap()    {}    Heap(const T array[], size_t size)    {        _heap.resize(size);  //一次性开辟足够的空间        for(size_t idx=0; idx<size; ++idx)        {            //_heap.push_back(array[idx]);  //先开辟有限个,若不够,再开辟            _heap[idx] = array[idx]; //如果没有扩容,就是没有开辟空间,导致程序崩溃        }        int root = (_heap.size() - 2)>>1;  //找到最后一个非叶子节点        for(; root>=0; root--)        {            _AdjustDown(root);        }    }    size_t Size()const    {        return _heap.size();    }    bool Empty()const    {        return _heap.empty();    }    void Insert(const T& data)    {        _heap.push_back(data);          if(_heap.size()>1)            _AdjustUp();    }    T& Top()    {        assert(!Empty());        return _heap[0];    }    void Remove()    {        assert(!_heap.empty());        size_t last = _heap.size()-1;        swap(_heap[last], _heap[0]);        _heap.pop_back();        _AdjustDown(0);    }protected:    void _AdjustDown(size_t parent)    {        size_t size = _heap.size();        size_t child = 2*parent+1;   //左孩子结点        while(child<size)        {            child = 2*parent+1;             //右孩子存在,找出左右孩子中小的节点            Compare com;            if(child+1 < size && com(_heap[child+1], _heap[child]))            {                child+= 1; //child指向右孩子            }            if(com(_heap[child], _heap[parent]))            {                swap(_heap[child], _heap[parent]);                parent = child;                child = parent*2+1;            }            else            {                break;            }        }    }    void _AdjustUp()    {        size_t child = _heap.size()-1;        size_t parent = (child-1)>>1;        while(child != 0)        {            Compare com;            if(com(_heap[child], _heap[parent]))            {                swap(_heap[child], _heap[parent]);                child = parent;                parent = (child-1)>>1;            }            else                return;        }    }protected:    std::vector<T> _heap;};int main(){    int array[] = {53, 17, 78, 9, 45, 65, 87, 23};    Heap<int> h(array, sizeof(array)/sizeof(array[0]));//小堆    Heap<int,Greater<int>> hp(array, sizeof(array)/sizeof(array[0]));//大堆    h.Insert(8);    hp.Insert(100);    h.Remove();    hp.Remove();    system("pause");    return 0;}

模板的模板函数实现大小堆:

template<class T>struct Less{    bool operator()(const T& left, const T& right)//仿函数    {        return left<right;    }};template<class T>struct Greater{    bool operator()(const T& left, const T& right)//仿函数    {        return left>right;    }};//模板的模板参数template<class T, template<class> class Compare = Less>// template<class> class Compare定义类模板依赖,调用处不用传类型class Heap{public:    // 创建一个空堆    Heap()    {}    Heap(const T array[], size_t size)    {        _heap.resize(size);  //一次性开辟足够的空间        for(size_t idx=0; idx<size; ++idx)        {            //_heap.push_back(array[idx]);  //先开辟有限个,若不够,再开辟            _heap[idx] = array[idx]; //如果没有扩容,就是没有开辟空间,导致程序崩溃        }        int root = (_heap.size() - 2)>>1;  //找到最后一个非叶子节点        for(; root>=0; root--)        {            _AdjustDown(root);        }    }    size_t Size()const    {        return _heap.size();    }    bool Empty()const    {        return _heap.empty();    }    void Insert(const T& data)    {        _heap.push_back(data);          if(_heap.size()>1)            _AdjustUp();    }    T& Top()    {        assert(!Empty());        return _heap[0];    }    void Remove()    {        assert(!_heap.empty());        size_t last = _heap.size()-1;        swap(_heap[last], _heap[0]);        _heap.pop_back();        _AdjustDown(0);    }protected:    void _AdjustDown(size_t parent)    {        size_t size = _heap.size();        size_t child = 2*parent+1;   //左孩子结点        while(child<size)        {            child = 2*parent+1;             //右孩子存在,找出左右孩子中小的节点            if(child+1 < size && Compare<T>()(_heap[child+1], _heap[child]))            {                child+= 1; //child指向右孩子            }            if(Compare<T>()(_heap[child], _heap[parent]))            {                swap(_heap[child], _heap[parent]);                parent = child;                child = parent*2+1;            }            else            {                break;            }        }    }    void _AdjustUp()    {        size_t child = _heap.size()-1;        size_t parent = (child-1)>>1;        while(child != 0)        {            if(Compare<T>()(_heap[child], _heap[parent]))            {                swap(_heap[child], _heap[parent]);                child = parent;                parent = (child-1)>>1;            }            else                return;        }    }protected:    std::vector<T> _heap;};int main(){    int array[] = {53, 17, 78, 9, 45, 65, 87, 23};    Heap<int> h(array, sizeof(array)/sizeof(array[0]));//小堆    Heap<int,Greater> hp(array, sizeof(array)/sizeof(array[0]));//大堆    h.Insert(8);    hp.Insert(100);    h.Remove();    hp.Remove();    system("pause");    return 0;}

优先级队列的实现:

template<class T, class Compare = Less<T>>class PriorityQueue{public:    PriorityQueue()    {}    void Push(const T& data)    {        _hp.Insert(data);    }    void Pop()    {        _hp.Remove();    }    const T& Top()const    {        return _hp.Top();     }    size_t Size()const    {        return _hp.Size();    }    bool Empty()const    {        return _hp.Empty();    }protected:    Heap<T, Compare> _hp;};int main(){    PriorityQueue<int> elem;    elem.Push(53);    elem.Push(17);    elem.Push(78);    elem.Push(9);    elem.Push(45);    elem.Push(65);    elem.Push(87);    elem.Push(23);    elem.Pop();    int array[] = {53, 17, 78, 9, 45, 65, 87, 23};    Heap<int, Less> h(array, sizeof(array)/sizeof(array[0]));    Heap<int,Greater> hp(array, sizeof(array)/sizeof(array[0]));    h.Insert(8);    hp.Insert(100);    h.Remove();    hp.Remove();    system("pause");    return 0;}
0 0
原创粉丝点击