• <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 閱讀(1790) 評論(0)  編輯 收藏 引用 所屬分類: Algorithm

            久久久久久九九99精品| 一本一道久久综合狠狠老| 99国产欧美久久久精品蜜芽| 亚洲狠狠综合久久| 伊人色综合久久天天人守人婷| 久久夜色精品国产噜噜噜亚洲AV| 国产精品久久久久久久 | 久久久久亚洲av毛片大| 精品国产乱码久久久久软件| 亚洲精品美女久久777777| 97精品国产97久久久久久免费 | 国产亚洲精久久久久久无码| 久久精品国产亚洲7777| 久久免费的精品国产V∧| 久久精品国产精品亜洲毛片| 精品熟女少妇av免费久久| 奇米影视7777久久精品人人爽| 久久99国产精品二区不卡| 三上悠亚久久精品| 99久久综合国产精品免费| 久久精品国产99国产精品| 久久青青草原综合伊人| 久久91亚洲人成电影网站| 久久精品无码午夜福利理论片| 色综合合久久天天给综看| 伊人久久精品线影院| .精品久久久麻豆国产精品| 久久亚洲日韩看片无码| 欧美伊人久久大香线蕉综合69 | 久久久精品国产sm调教网站 | 久久久www免费人成精品| 久久久国产精华液| 久久最新免费视频| 亚洲婷婷国产精品电影人久久| 久久艹国产| 亚洲国产成人久久综合野外| 精品久久久久久99人妻| 精品国产一区二区三区久久蜜臀| 亚洲综合久久综合激情久久| 观看 国产综合久久久久鬼色 欧美 亚洲 一区二区 | 94久久国产乱子伦精品免费|