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

            国产精品成人久久久久三级午夜电影 | 中文字幕精品久久久久人妻| 欧美性大战久久久久久| 久久人人爽人人爽人人爽| 久久精品国产99久久久古代| 久久国产乱子伦免费精品| 久久本道久久综合伊人| 亚洲精品乱码久久久久久| 国产免费久久精品丫丫| 亚洲伊人久久精品影院| 国产一区二区精品久久凹凸| 东方aⅴ免费观看久久av| 国产精品美女久久久网AV| 亚洲AV日韩AV天堂久久| 久久天天日天天操综合伊人av| 久久久久无码精品国产不卡| 色欲综合久久躁天天躁| 青青青国产精品国产精品久久久久| 欧美久久久久久| 久久精品亚洲乱码伦伦中文| 99久久久精品免费观看国产| 久久久这里只有精品加勒比 | 久久久久国产| 99国产精品久久| 日产精品久久久一区二区| 青青久久精品国产免费看| 国产精品免费看久久久香蕉| 狠狠色丁香久久综合五月| 久久精品中文闷骚内射| 久久亚洲精品国产精品| 亚洲AV日韩精品久久久久久久| 久久国产精品无| 日日狠狠久久偷偷色综合免费| 伊人久久综在合线亚洲2019| 精品久久久久久| 热久久国产精品| 久久亚洲精品视频| 伊人久久免费视频| 久久久WWW成人| 99久久国产亚洲综合精品| 无码任你躁久久久久久老妇App|