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

            99久久久精品免费观看国产| 亚洲精品97久久中文字幕无码| 久久婷婷五月综合97色直播| 亚洲va久久久噜噜噜久久 | 伊人久久大香线蕉av一区| 久久国产欧美日韩精品| 久久精品无码一区二区日韩AV | 亚洲国产另类久久久精品小说| 99久久综合国产精品二区| 精品亚洲综合久久中文字幕| 国产成人无码精品久久久免费 | 久久久久国产| 色综合久久中文字幕无码| 久久精品国产91久久麻豆自制 | 国内精品久久久久影院网站| 久久天天婷婷五月俺也去 | 久久99国产精品二区不卡| 一级a性色生活片久久无| AV无码久久久久不卡网站下载 | 手机看片久久高清国产日韩 | 精品熟女少妇a∨免费久久| 99久久99久久精品国产片果冻| 中文字幕久久亚洲一区| 99久久无色码中文字幕| 久久国产精品无| 99久久精品国产一区二区三区 | 香蕉aa三级久久毛片| 色综合久久中文综合网| 久久久无码精品亚洲日韩蜜臀浪潮| 久久青青草原国产精品免费| 亚洲成色WWW久久网站| 久久久亚洲精品蜜桃臀| 国产精品久久久99| 亚洲一区二区三区日本久久九| 久久综合亚洲色HEZYO社区| 久久综合九色综合97_久久久| 久久婷婷五月综合97色| 国色天香久久久久久久小说| 亚洲国产精品嫩草影院久久 | 亚洲欧美成人综合久久久| 久久综合久久性久99毛片|