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

            久久AV无码精品人妻糸列| 韩国三级中文字幕hd久久精品| 中文精品久久久久人妻| 久久婷婷五月综合国产尤物app| 蜜臀av性久久久久蜜臀aⅴ麻豆| 国产一区二区精品久久| 久久人人爽人人爽人人片AV麻豆| 人妻无码久久一区二区三区免费| 国产高潮国产高潮久久久91| 无码人妻久久一区二区三区免费| 国产精品久久自在自线观看| 久久久SS麻豆欧美国产日韩| 国产午夜精品理论片久久| 99久久国产热无码精品免费| 亚洲一级Av无码毛片久久精品| 久久国产一区二区| 久久超碰97人人做人人爱| 综合久久一区二区三区 | 99久久综合狠狠综合久久| 久久精品国产亚洲AV久| 久久久久亚洲av毛片大| 久久精品国产秦先生| 99久久国产热无码精品免费| 亚洲精品乱码久久久久久按摩| 香蕉99久久国产综合精品宅男自 | 国产精品成人久久久久久久| 久久精品人人做人人妻人人玩| 狠狠色丁香久久婷婷综合| 伊人久久一区二区三区无码| 久久99精品久久久久久野外| 国产精品99久久久久久猫咪 | 久久综合欧美成人| 欧美伊香蕉久久综合类网站| 99国产精品久久久久久久成人热| 麻豆AV一区二区三区久久| 久久久一本精品99久久精品66| 无码国内精品久久人妻蜜桃 | 日韩va亚洲va欧美va久久| 亚洲欧美日韩久久精品| 久久久久av无码免费网| 久久九九精品99国产精品|