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

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

            最小堆類

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


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            亚洲AV无码久久精品狠狠爱浪潮 | 久久婷婷综合中文字幕| 一本色道久久99一综合| 2021久久精品国产99国产精品| 久久国产色AV免费看| 久久国产福利免费| 亚洲人成电影网站久久| 国产亚洲精久久久久久无码| 亚洲国产成人久久综合碰| 久久人妻少妇嫩草AV无码专区| …久久精品99久久香蕉国产| 一本综合久久国产二区| 久久精品人人槡人妻人人玩AV| 久久久久亚洲精品中文字幕| 国产精品无码久久综合| 久久男人AV资源网站| 国产精品久久波多野结衣| 2020国产成人久久精品| 性做久久久久久久久久久| 久久线看观看精品香蕉国产| 亚洲人成网站999久久久综合 | 一本色综合久久| 久久精品无码一区二区app| 国产一区二区精品久久凹凸| 无码人妻久久一区二区三区免费 | 国产成人久久精品激情| 99久久免费国产精品特黄| 国产免费久久精品丫丫| 99久久综合国产精品二区| 日韩人妻无码精品久久免费一| 漂亮人妻被中出中文字幕久久| 国产成人精品久久亚洲| 日本免费久久久久久久网站| 久久99国产综合精品免费| 国产成人久久精品一区二区三区 | 欧美一级久久久久久久大片| 狠狠色综合久久久久尤物| 国产999精品久久久久久| 国产福利电影一区二区三区久久久久成人精品综合 | 蜜桃麻豆www久久国产精品| 久久久99精品一区二区|