• <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| 亚洲中文字幕无码久久综合网| 久久久久久青草大香综合精品| 久久中文骚妇内射| 国内精品久久久久影院免费| 99热精品久久只有精品| 精品久久久中文字幕人妻| 久久99国产精品久久99| 99久久做夜夜爱天天做精品| 久久国产精品国产自线拍免费| 亚洲国产精品婷婷久久| 亚洲欧洲日产国码无码久久99| 99久久er这里只有精品18| 伊人久久大香线蕉综合5g| 国产精品久久久久…| 久久久高清免费视频| 亚洲一本综合久久| 久久亚洲精品中文字幕| 久久国产午夜精品一区二区三区| 欧美日韩精品久久久久| 久久久久久无码国产精品中文字幕| 久久这里都是精品| 久久久久无码精品| 成人午夜精品久久久久久久小说| 国内精品伊人久久久影院| 久久99精品久久久久久不卡| 久久久青草青青亚洲国产免观| 久久笫一福利免费导航| 色99久久久久高潮综合影院| 精品人妻伦一二三区久久| 久久精品免费观看| 麻豆精品久久精品色综合| 久久夜色精品国产亚洲| 亚洲天堂久久精品| 国内精品久久久久久中文字幕| 无码AV中文字幕久久专区| 日韩精品久久无码中文字幕| 亚洲精品无码久久千人斩| 亚洲国产精品18久久久久久|