• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Try Again

            基礎知識學習
            隨筆 - 4, 文章 - 0, 評論 - 0, 引用 - 0
            數據加載中……

            最小堆和最大堆

            #include <iostream>
            #include 
            <string>
            #include 
            <stdio.h>
            using namespace std;

            #define SIZE  500000

            void swap(int &a,int &b)
            {
                
            int temp = a;
                a 
            = b;
                b 
            = temp;
            }

            class Heap
            {
                
            int size;
                
            int heap[SIZE];
            public:
                
            virtual bool cmp(int a,int b) = 0;
            private:
                inline 
            int fathter(int p)
                {
                    
            return p / 2;
                }
                inline 
            int LeftSon(int p)
                {
                    
            int son = 2 * p;
                    
            if (son > size)
                        
            return 0;
                    
            return son;
                }
                inline 
            int RightSon(int p)
                {
                    
            int son = 2 * p + 1;
                    
            if (son > size)
                        
            return 0;
                    
            return son;
                }
                
            int ShiftUp(int p)
                {
                    
            if (p == 1)
                        
            return p;
                    
            if (cmp(heap[p],heap[fathter(p)]))
                    {
                        swap(heap[p],heap[fathter(p)]);
                        
            return fathter(p);
                    }
                    
            return p;
                }
                
            int ShiftDown(int p)
                {
                    
            int lagest = p;

                    
            if ((LeftSon(p)) && (cmp(heap[LeftSon(p)],heap[lagest])))
                        lagest 
            = LeftSon(p);
                    
            if ((RightSon(p)) && (cmp(heap[RightSon(p)],heap[lagest])))
                        lagest 
            = RightSon(p);
                    
            if (lagest != p)
                        swap(heap[lagest],heap[p]);
                    
            return lagest;
                }
            public:
                Heap() { size 
            = 0; }
                
            int insert(int n);
                
            void del(int p);
                
            void DelHead();
                
            int head();
                
            void init();
                
            bool IsEempty();
            };
            int Heap::insert(int n)
            {
                size
            ++;
                heap[size] 
            = n;
                
            int where = size;
                
            int p;
                
            while (((p = ShiftUp(where)) != where))
                {
                    where 
            = p;
                    
            continue;
                }
                
            return where;
            }
            void Heap::del(int p)
            {
                heap[p] 
            = heap[size];
                size
            --;
                
            int where;
                
            while (((where = ShiftDown(p)) != p))
                {
                    p 
            = where;
                    
            continue;
                }
            }
            void Heap::DelHead()
            {
                del(
            1);
            }
            int Heap::head()
            {
                
            if (size == 0)
                    
            return -1;
                
            return heap[1];
            }
            void Heap::init()
            {
                size 
            = 0;
            }
            bool Heap::IsEempty()
            {
                
            if (size == 0)
                    
            return 1;
                
            else
                    
            return 0;
            }

            class MaxHeap : public Heap
            {
                
            bool cmp(int a,int b)
                {
                    
            return a > b;
                }
            };

            class MinHeap : public Heap
            {
                
            bool cmp(int a,int b)
                {
                    
            return a < b;
                }
            };

            int main()
            {
                
            return 0;
            }

            posted on 2008-08-04 10:29 NicYun 閱讀(1792) 評論(0)  編輯 收藏 引用 所屬分類: Algorithm

            精品国产日韩久久亚洲| 久久精品国产亚洲AV无码麻豆| 丰满少妇高潮惨叫久久久| 久久精品www人人爽人人| 亚洲午夜久久久久妓女影院| 久久精品人人做人人爽97| 久久精品国产免费| 久久综合九色综合欧美就去吻| 欧美激情一区二区久久久| 久久人人妻人人爽人人爽| 久久91综合国产91久久精品| 国产精品九九久久免费视频 | 亚洲国产天堂久久久久久| 影音先锋女人AV鲁色资源网久久| 国产精品久久久久国产A级| 精品久久人人妻人人做精品| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 国产成人精品久久免费动漫| 国产99久久久久久免费看| 久久伊人精品一区二区三区 | 久久精品国产精品亜洲毛片| 亚洲精品乱码久久久久久自慰 | 色播久久人人爽人人爽人人片aV | 2020久久精品亚洲热综合一本| 久久综合久久久| 亚洲精品tv久久久久久久久| 久久狠狠一本精品综合网| 97久久国产综合精品女不卡| 免费精品久久久久久中文字幕| 国产成人综合久久综合 | 欧美综合天天夜夜久久| 日韩乱码人妻无码中文字幕久久 | 97精品久久天干天天天按摩| 精品久久亚洲中文无码| 欧美粉嫩小泬久久久久久久 | 久久久一本精品99久久精品88| 精品国产婷婷久久久| 色综合色天天久久婷婷基地| 2021精品国产综合久久| 精品乱码久久久久久久| 久久精品国产亚洲AV影院|