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

            狠狠色婷婷久久一区二区| 久久精品免费大片国产大片 | 国产人久久人人人人爽| 精品人妻久久久久久888| 国产精品久久永久免费| 久久本道久久综合伊人| 久久久精品国产免大香伊| 国产99久久精品一区二区| 久久成人18免费网站| 亚洲国产精品18久久久久久| 成人国内精品久久久久影院| 日本久久中文字幕| 精品久久久久久成人AV| 无码人妻少妇久久中文字幕| 99久久婷婷国产综合亚洲| 久久久综合香蕉尹人综合网| 97热久久免费频精品99| 中文字幕久久精品| 久久99国产精品成人欧美| 久久中文骚妇内射| 久久无码AV中文出轨人妻| 久久久国产精品| 日本福利片国产午夜久久| 成人久久免费网站| 亚洲国产成人精品91久久久| 精品久久久久久中文字幕| 无码伊人66久久大杳蕉网站谷歌| 久久九九久精品国产| 久久精品成人免费网站| 久久99久久99精品免视看动漫| 伊人久久大香线蕉无码麻豆| 久久精品国产精品亚洲人人| 色偷偷888欧美精品久久久| avtt天堂网久久精品| 久久综合给合久久狠狠狠97色69| 久久精品国产亚洲AV蜜臀色欲| 日日狠狠久久偷偷色综合96蜜桃| 久久本道综合久久伊人| 国产一区二区三精品久久久无广告| 亚洲综合精品香蕉久久网97| 久久久久久a亚洲欧洲aⅴ|