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

            国产69精品久久久久99| 久久亚洲国产精品一区二区| 久久精品国产精品亚洲人人| 少妇人妻综合久久中文字幕| 国产91色综合久久免费分享| 久久亚洲精品国产亚洲老地址 | 久久夜色精品国产亚洲| 精品久久久久香蕉网| 久久乐国产综合亚洲精品| 久久婷婷色香五月综合激情| 久久久久99精品成人片欧美| 亚洲香蕉网久久综合影视| 无码8090精品久久一区| 精品久久久久国产免费 | 日韩电影久久久被窝网| 中文字幕无码久久精品青草| 狠狠久久综合伊人不卡| 日韩人妻无码精品久久免费一| 亚洲国产成人久久精品99 | 亚洲国产精品成人AV无码久久综合影院 | 国产精品久久久久久久久免费| 人人狠狠综合久久亚洲高清| 久久精品国产亚洲AV香蕉| 欧美午夜A∨大片久久 | 欧美黑人又粗又大久久久| 国产精品成人久久久久三级午夜电影| 亚洲av日韩精品久久久久久a| 亚洲va久久久噜噜噜久久狠狠| 久久国产综合精品五月天| 亚洲狠狠久久综合一区77777| 久久99精品国产麻豆不卡| 亚洲狠狠综合久久| 亚洲国产成人久久综合碰碰动漫3d| 国产午夜福利精品久久2021| 狠狠色婷婷久久一区二区三区| 久久精品国产男包| 国产亚州精品女人久久久久久 | 青青热久久国产久精品| 99久久99久久精品国产| 国产精自产拍久久久久久蜜| 久久97久久97精品免视看秋霞|