建大堆实现优先级队列

来源:互联网 发布:北医李睿医考 知乎 编辑:程序博客网 时间:2024/06/05 22:37


//******************************大堆****************************
template<typenameT>
classHeap
{
public:
                Heap()
                { }
                Heap(constT*a,size_tsize)
                {
                                for(size_ti = 0; i <size; ++i)
                                {
                                                _a.push_back(a[i]);
                                }
                                for(inti = (_a.size() - 2) / 2; i >= 0; --i)
                                {
                                                _AdjustDown(i);
                                }
                }
                

//向下调整,每次只调节一个父亲节点下面的所有分支
                void_AdjustDown(int parent)
                {
                                intchild =parent* 2 + 1;
                                while(child < _a.size())
                                {
                                                if(child + 1 < _a.size() && _a[child + 1] > _a[child])
                                                {
                                                                ++child;
                                                }
                                                if(_a[child]>_a[parent])
                                                {
                                                                swap(_a[child], _a[parent]);
                                                                parent= child;
                                                                child = 2 *parent+ 1;
                                                }
                                                else
                                                                break;
                                }
                }
                

//向上调整,每次向上调整一条线上的数据
                void_AdjustUp(intchild)
                {
                                intparent = (child- 1) / 2;
                                while(child> 0)
                                {
                                                if(_a[parent] < _a[child])
                                                {
                                                                swap(_a[parent], _a[child]);
                                                                child= parent;
                                                                parent = (child- 1) / 2;
                                                }
                                                else
                                                {
                                                                break;
                                                }
                                }
                }

                
                //插入一个数据到最小堆中
                voidPush(constT&x)
                {
                                _a.push_back(x);
                                _AdjustUp(_a.size() - 1);
                }

                //删除堆顶元素
                voidPop()
                {
                                assert(_a.size() > 0);
                                swap(_a[0], _a[_a.size() - 1]);
                                _a.pop_back();
                                _AdjustDown(0);
                }
                
                T& GetTop()
                {
                                assert(_a.size() > 0);
                                return_a[0];
                }
                

                //判断是否为空
                boolEmpty()
                {
                                if(_a.size() == 0)
                                {
                                                returntrue;
                                }
                                returnfalse;
                }
                size_tSize()
                {
                                return_a.size();
                }
                voidPrint()
                {
                                for(inti = 0; i < _a.size(); i++)
                                {
                                                cout << _a[i] <<" ";
                                }
                                cout << endl;
                }
private:
                vector<T> _a;
};

//int main()
//{
//             int array[10] = { 10, 16, 18, 12, 11, 13, 15, 17, 14, 19 };
//             Heap<int> a1(array, sizeof(array) / sizeof(array[0]));
//             /*a1.Print();
//             a1.Push(20);
//             a1.Print();
//             a1._AdjustUp(a1.Size() - 1);
//             a1.Print();
//             a1.GetTop();
//             a1.Size();*/
//             return 0;
//}

//*************************优先级队列****************************

template<typenameT>
classPriorityQueue
{
public:
                voidPush(constT&x)
                {
                                hp.Push(x);
                }
                voidPop()
                {
                                hp.Pop();
                }
private:
                Heap<T> hp;
};

intmain()
{
                PriorityQueue<int> que;
                que.Push(1);
                que.Push(5);
                que.Push(3);
                que.Push(7);
                que.Push(6);
                que.Push(9);
                
                que.Pop();
                que.Pop();
                que.Pop();
                return0;
}

0 0
原创粉丝点击