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

            The Fourth Dimension Space

            枯葉北風寒,忽然年以殘,念往昔,語默心酸。二十光陰無一物,韶光賤,寐難安; 不畏形影單,道途阻且慢,哪曲折,如渡飛湍。斬浪劈波酬壯志,同把酒,共言歡! -如夢令

            最小堆類

            #include<iostream>
            #include
            <cmath>
            #include
            <algorithm>
            using namespace std;


            template
            <class T>
            class MinHeap
            {
            private:
                T 
            *heap;
                
            int CurrentSize;
                
            int MaxSize;
                
            void FilterDown(const int start,const int end);
                
            void FilterUp(int start);
            public:
                MinHeap(
            int n);
                MinHeap();
                
            ~MinHeap(){delete []heap;}
                
            bool Insert(const T &x);
                T RemoveMin();
                T GetMin();
                
            bool IsEmpty() const{return CurrentSize==0;}
                
            bool IsFull() const{return CurrentSize==MaxSize;}
                
            void Clear(){CurrentSize=0;}
            }
            ;


            template
            <class T>
            MinHeap
            <T>::MinHeap()
            {

                MaxSize
            =1000;
                heap
            =new T[MaxSize];
                CurrentSize
            =0;

            }

            template
            <class T>
            MinHeap
            <T>::MinHeap(int n)
            {

                MaxSize
            =n;
                heap
            =new T[MaxSize];
                CurrentSize
            =0;
            }


            template
            <class T>
            void MinHeap<T>::FilterDown(const int start,const int end)
            {

                
            int i=start,j=2*i+1;
                T temp
            =heap[i];
                
            while(j<=end)
                
            {

                    
            if(j<end&&heap[j]>heap[j+1])
                        j
            ++;
                    
            if(temp<=heap[j])
                        
            break;
                    
            else 
                    
            {

                        heap[i]
            =heap[j];i=j;j=2*j+1;
                    }

                }

                heap[i]
            =temp;
            }



            template
            <class T>
            bool MinHeap<T>::Insert(const T &x)
            {

                
            if(CurrentSize==MaxSize)
                    
            return false;
                heap[CurrentSize]
            =x;
                FilterUp(CurrentSize);
                CurrentSize
            ++;
                
            return true;
            }



            template
            <class T>
            void MinHeap<T>::FilterUp(int start)
            {

                
            int j=start,i=(j-1)/2;
                T temp
            =heap[j];
                
            while(j>0)
                
            {

                    
            if(heap[i]<=temp)break;
                    
            else
                        heap[j]
            =heap[i];j=i;i=(i-1)/2;

                }

                heap[j]
            =temp;
            }




            template
            <class T>
            T MinHeap
            <T>::RemoveMin( )
            {
                T x
            =heap[0];
                heap[
            0]=heap[CurrentSize-1];
                CurrentSize
            --;
                FilterDown(
            0,CurrentSize-1);
                
            return x;
            }


            template
            <class T>
            T MinHeap
            <T>::GetMin()
            {

                
            return heap[0];
            }



            int main ()
            {
                MinHeap
            <int> test(8);
                
            int k;
                
            bool tem;
                
            for(k=1;k<=10;k++)
                
            {

                    tem
            =test.Insert(10-k);
                }

                tem
            =test.IsEmpty();
                tem
            =test.IsFull();
                
            for(k=1;k<=5;k++)
                    test.RemoveMin();
                
            return 0;

            }


            一個自實現(xiàn)的優(yōu)先隊列 最小堆。


            #include<iostream>
            #include
            <cmath>
            #include
            <algorithm>
            using namespace std;


            template
            <class T>
            class MinHeap
            {
            public:
                T 
            *heap;
                
            int CurrentSize;
                
            int MaxSize;
                
            void FilterDown(const int start,const int end);
                
            void FilterUp(int start);
            public:
                MinHeap(
            int n);
                
            ~MinHeap(){delete []heap;}
                
            bool Insert(const T &x);
                T RemoveMin();
                T GetMin();
            }
            ;

            template
            <class T>
            MinHeap
            <T>::MinHeap(int n)
            {

                MaxSize
            =n;
                heap
            =new T[MaxSize];
                CurrentSize
            =0;
            }


            template
            <class T>
            void MinHeap<T>::FilterDown(const int start,const int end)
            {

                
            int i=start,j=2*i+1;
                T temp
            =heap[i];
                
            while(j<=end)
                
            {

                    
            if(j<end&&heap[j+1]<heap[j])
                        j
            ++;
                    
            if(temp<heap[j])
                        
            break;
                    
            else 
                        heap[i]
            =heap[j];i=j;j=2*j+1;
                }

                heap[i]
            =temp;
            }



            template
            <class T>
            bool MinHeap<T>::Insert(const T &x)
            {

                
            if(CurrentSize==MaxSize)
                    
            return false;
                heap[CurrentSize]
            =x;
                FilterUp(CurrentSize);
                CurrentSize
            ++;
                
            return true;
            }



            template
            <class T>
            void MinHeap<T>::FilterUp(int start)
            {

                
            int j=start,i=(j-1)/2;
                T temp
            =heap[j];
                
            while(j>0)
                
            {
                    
            if(heap[i]<temp) break;
                    
            else heap[j]=heap[i];j=i;i=(i-1)>>1;
                }

                heap[j]
            =temp;
            }

            template
            <class T>
            T MinHeap
            <T>::RemoveMin( )
            {
                T x
            =heap[0];
                heap[
            0]=heap[CurrentSize-1];
                CurrentSize
            --;
                FilterDown(
            0,CurrentSize-1);
                
            return x;
            }


            template
            <class T>
            T MinHeap
            <T>::GetMin()
            {
                
            return heap[0];
            }

            稍微改良一下啊 只需要重載<符號即可

            posted on 2009-05-08 16:57 abilitytao 閱讀(412) 評論(0)  編輯 收藏 引用

            青草久久久国产线免观| 国产精品久久成人影院| 久久综合久久伊人| 国产精品99久久久精品无码| 国产成人久久精品一区二区三区 | 久久综合亚洲色一区二区三区| 国产成人久久精品一区二区三区| 99久久这里只有精品| 久久久久久亚洲精品不卡 | 久久本道久久综合伊人| 久久夜色精品国产亚洲| 国产免费久久精品丫丫| 熟妇人妻久久中文字幕| 婷婷久久综合九色综合绿巨人| AV无码久久久久不卡蜜桃| 久久精品无码一区二区三区日韩| 99精品久久久久久久婷婷| 久久天天日天天操综合伊人av| 77777亚洲午夜久久多喷| 免费精品久久天干天干| 久久久久亚洲av成人无码电影 | 国产69精品久久久久777| 综合久久一区二区三区 | 久久综合香蕉国产蜜臀AV| 色婷婷狠狠久久综合五月| 伊人久久大香线焦综合四虎| 97久久超碰成人精品网站| 伊人久久大香线蕉av不变影院| 一本久久精品一区二区| 日韩va亚洲va欧美va久久| 久久www免费人成看国产片| 品成人欧美大片久久国产欧美... 品成人欧美大片久久国产欧美 | 久久国产香蕉一区精品| 国产精品99久久精品爆乳| 国产精品视频久久| 丁香五月网久久综合| 97r久久精品国产99国产精| 国产精品美女久久久| 国产精品无码久久综合| 国产精品久久久久无码av| 精品综合久久久久久97超人|