• <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)  編輯 收藏 引用

            国产精品久久波多野结衣| 亚洲精品无码久久久久AV麻豆| 成人精品一区二区久久| 国产精品免费看久久久| 亚洲综合伊人久久大杳蕉| 人妻精品久久久久中文字幕| 97精品伊人久久久大香线蕉 | 久久久久久久久无码精品亚洲日韩| 欧美国产精品久久高清| 久久这里有精品视频| 亚洲&#228;v永久无码精品天堂久久 | 精品国产婷婷久久久| 99久久精品国产一区二区| 青青青青久久精品国产h| 亚洲一区中文字幕久久| 久久www免费人成精品香蕉| 久久综合成人网| 久久久久久久91精品免费观看| 久久久黄色大片| 2020久久精品亚洲热综合一本| 亚洲综合日韩久久成人AV| 人妻无码久久一区二区三区免费 | 思思久久精品在热线热| 久久精品国产免费观看三人同眠| 久久久久久久女国产乱让韩| 久久66热人妻偷产精品9| 久久夜色精品国产亚洲| 女同久久| 久久综合噜噜激激的五月天| 久久精品成人国产午夜| 久久亚洲国产成人精品无码区| 久久乐国产综合亚洲精品| 久久不见久久见免费视频7| 精品国产综合区久久久久久| 97精品国产97久久久久久免费| 久久精品免费一区二区三区| 亚洲国产日韩欧美久久| 久久国产精品-国产精品| 久久频这里精品99香蕉久| 中文字幕亚洲综合久久2| 人妻无码精品久久亚瑟影视|