• <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

            基礎(chǔ)知識(shí)學(xué)習(xí)
            隨筆 - 4, 文章 - 0, 評(píng)論 - 0, 引用 - 0
            數(shù)據(jù)加載中……

            最小堆和最大堆

            #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) 評(píng)論(0)  編輯 收藏 引用 所屬分類: Algorithm

            欧美久久久久久| 久久综合九色综合久99| 性做久久久久久久久久久| 欧美伊香蕉久久综合类网站| 人妻无码αv中文字幕久久琪琪布| 中文精品99久久国产| 亚洲国产成人久久精品动漫| 99久久国产综合精品成人影院| 色狠狠久久AV五月综合| 久久婷婷五月综合色奶水99啪| 亚洲AV无码久久精品蜜桃| 久久久噜噜噜www成人网| 久久久久久久人妻无码中文字幕爆| 亚洲女久久久噜噜噜熟女| 精品综合久久久久久888蜜芽| 久久人人爽人人爽人人AV| 精品免费久久久久久久| 夜夜亚洲天天久久| 久久久黄片| 一本久久a久久精品亚洲| 久久国产精品77777| 国产99久久久国产精品~~牛| 久久国产精品波多野结衣AV| 色天使久久综合网天天| 少妇久久久久久久久久| 久久国产乱子伦精品免费强| 国产精品一区二区久久精品无码| 亚洲国产小视频精品久久久三级| 久久久久亚洲av成人网人人软件| 婷婷伊人久久大香线蕉AV| 亚洲一区中文字幕久久| 中文国产成人精品久久亚洲精品AⅤ无码精品| 国产色综合久久无码有码| 91精品国产乱码久久久久久| 久久99精品国产麻豆婷婷| 一本久久知道综合久久| 久久精品国产亚洲7777| 亚洲va中文字幕无码久久不卡| 国内精品伊人久久久久网站| 777午夜精品久久av蜜臀| 久久久久香蕉视频|