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

            }


            一個自實現的優先隊列 最小堆。


            #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 閱讀(413) 評論(0)  編輯 收藏 引用

            狠狠色综合网站久久久久久久| 久久夜色精品国产噜噜亚洲a| 国产精品久久久久AV福利动漫| 性欧美大战久久久久久久久 | 香蕉久久夜色精品升级完成| 国产毛片欧美毛片久久久| 99久久免费国产精精品| 久久无码AV中文出轨人妻| 久久精品国产99久久久古代 | 久久精品国产99国产精品导航| 色婷婷久久综合中文久久蜜桃av| 狠狠色婷婷综合天天久久丁香| 久久久久久噜噜精品免费直播| 亚洲国产欧洲综合997久久| 久久精品国产久精国产| 亚洲欧美成人综合久久久| 久久午夜无码鲁丝片午夜精品| 国内精品久久久久久99| 久久男人中文字幕资源站| 久久青草国产手机看片福利盒子| 久久人人爽人人爽人人片AV麻烦| 99久久综合国产精品二区| 久久99国产综合精品女同| 中文字幕日本人妻久久久免费| 久久伊人影视| 日日狠狠久久偷偷色综合96蜜桃| 久久久久国产精品| 久久精品www| 色综合合久久天天综合绕视看| 国内精品久久久久久久97牛牛| 久久影院综合精品| 久久精品欧美日韩精品| 99久久99久久精品国产片果冻| 久久国内免费视频| 久久精品成人欧美大片| 97香蕉久久夜色精品国产| 中文精品99久久国产| 97香蕉久久夜色精品国产| 久久婷婷国产剧情内射白浆| 狠狠色婷婷久久一区二区| 久久综合香蕉国产蜜臀AV|