• <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>
            posts - 200, comments - 8, trackbacks - 0, articles - 0

            最小堆&&最大堆的實現(c++)(轉)

            Posted on 2012-11-19 21:09 鑫龍 閱讀(1209) 評論(1)  編輯 收藏 引用 所屬分類: 數據結構與算法
            最小堆:
            template<class T>
            class MinHeap {
            public:
                MinHeap(
            int MinHeapSize = 10);
                
            ~MinHeap() {delete [] heap;}
                
            int Size() const {return CurrentSize;}
                T Min() {
            if (CurrentSize == 0)
                          
            throw OutOfBounds();
                       
            return heap[1];}
                MinHeap
            <T>& Insert(const T& x);
                MinHeap
            <T>& DeleteMin(T& x);
                
            void Initialize(T a[], int size, int ArraySize);
                
            void Deactivate() {heap = 0;}
                
            void Output() const;
            private:
                
            int CurrentSize, MaxSize;
                T 
            *heap;
            };

            template
            <class T>
            MinHeap
            <T>::MinHeap(int MinHeapSize)
            {
                MaxSize 
            = MinHeapSize;
                heap 
            = new T[MaxSize+1];
                CurrentSize 
            = 0;
            }

            template
            <class T>
            MinHeap
            <T>& MinHeap<T>::Insert(const T& x)
            {
                
            if (CurrentSize == MaxSize)
                    
            throw NoMem();

                
            //為x尋找應插入的位置
                
            //i從新的葉節點開始,并沿著樹上升
                int i = ++CurrentSize;
                
            while (i != 1 && x < heap[i/2]) 
                {
                    heap[i] 
            = heap[i/2]; // 將元素下移
                    i /= 2;                 // 移向父節點
                }
                heap[i] 
            = x;

                
            return *this;
            }

            template
            <class T>
            MinHeap
            <T>& MinHeap<T>::DeleteMin(T& x)
            {
                
            if (CurrentSize == 0)
                    
            throw OutOfBounds();

                x 
            = heap[1];

                T y 
            = heap[CurrentSize--]; //最后一個元素

                
            // 從根開始, 為y尋找合適的位置
                int i = 1,  // 堆的當前節點
                   ci = 2;    // i的子節點

                
            while (ci <= CurrentSize) 
                {
                    
            // 使heap[ci] 是i較小的子節點
                    if (ci < CurrentSize 
                      
            && heap[ci] > heap[ci+1]) 
                        ci
            ++;

                    
            // 能把y放入heap[i]嗎?
                    if (y <= heap[ci]) 
                        
            break;  // 能

                    
            // 不能
                    heap[i] = heap[ci]; // 子節點上移
                    i = ci;                // 下移一層
                    ci *= 2;
                }

                heap[i] 
            = y;

                
            return *this;
            }

            template
            <class T>
            void MinHeap<T>::Initialize(T a[], int size, int ArraySize)
            {
               delete [] heap;
               heap 
            = a;
               CurrentSize 
            = size;
               MaxSize 
            = ArraySize;

               
            // 產生一個最小堆
               for (int i = CurrentSize/2; i >= 1; i--
               {
                    T y 
            = heap[i]; // 子樹的根

                    
            // 尋找放置y的位置
                    int c = 2*i; // c 的父節點是y的目標位置

                    
            while (c <= CurrentSize) 
                    {
                        
            // 使heap[c]是較小的子節點
                        if (c < CurrentSize &&
                         heap[c] 
            > heap[c+1]) c++;

                        
            // 能把y放入heap[c/2]嗎?
                        if (y <= heap[c]) break;  // 能

                        
            // 不能
                        heap[c/2= heap[c]; // 子節點上移
                        c *= 2;              // 下移一層
                    }

                    heap[c
            /2= y;
                }
            }

            template
            <class T>
            void MinHeap<T>::Output() const
            {
                cout 
            << "The " << CurrentSize
                    
            << " elements are"<< endl;
                
            for (int i = 1; i <= CurrentSize; i++)
                  cout 
            << heap[i] << ' ';
                cout 
            << endl;
            }


            最大堆:
            template<class T>
            class MaxHeap {
            public:
                MaxHeap(
            int MaxHeapSize = 10);
                
            ~MaxHeap() {delete [] heap;}
                
            int Size() const {return CurrentSize;}
                T Max() {
            if (CurrentSize == 0)
                          
            throw OutOfBounds();
                       
            return heap[1];}
                MaxHeap
            <T>& Insert(const T& x);
                MaxHeap
            <T>& DeleteMax(T& x);
                
            void Initialize(T a[], int size, int ArraySize);
                
            void Deactivate() {heap = 0;}
                
            void Output() const;
            private:
                
            int CurrentSize, MaxSize;
                T 
            *heap;
            };

            template
            <class T>
            MaxHeap
            <T>::MaxHeap(int MaxHeapSize)
            {
                MaxSize 
            = MaxHeapSize;
                heap 
            = new T[MaxSize+1];
                CurrentSize 
            = 0;
            }

            template
            <class T>
            MaxHeap
            <T>& MaxHeap<T>::Insert(const T& x)
            {
                
            if (CurrentSize == MaxSize)
                    
            throw NoMem();

                
            //為x尋找應插入的位置
                
            //i從新的葉節點開始,并沿著樹上升
                int i = ++CurrentSize;
                
            while (i != 1 && x > heap[i/2]) 
                {
                    heap[i] 
            = heap[i/2]; // 將元素下移
                    i /= 2;              // 移向父節點
                }

                heap[i] 
            = x;
                
            return *this;
            }

            template
            <class T>
            MaxHeap
            <T>& MaxHeap<T>::DeleteMax(T& x)
            {
                
            if (CurrentSize == 0)
                    
            throw OutOfBounds();

                x 
            = heap[1];


                T y 
            = heap[CurrentSize--]; //最后一個元素

                
            // 從根開始, 為y尋找合適的位置
                int i = 1,  // 堆的當前節點
                   ci = 2;    // i的子節點

                
            while (ci <= CurrentSize)
                {
                    
            // 使heap[ci] 是i較大的子節點
                    if (ci < CurrentSize 
                     
            && heap[ci] < heap[ci+1]) 
                        ci
            ++;

                    
            // 能把y放入heap[i]嗎?
                    if (y >= heap[ci]) 
                        
            break;//

                    
            //不能
                    heap[i] = heap[ci]; // 子節點上移
                    i = ci;             // 下移一層
                    ci *= 2;
                }

                heap[i] 
            = y;

                
            return *this;
            }

            template
            <class T>
            void MaxHeap<T>::Initialize(T a[], int size, int ArraySize)
            {
                delete [] heap;
                heap 
            = a;
                CurrentSize 
            = size;
                MaxSize 
            = ArraySize;

                
            // 產生一個最大堆
                for (int i = CurrentSize/2; i >= 1; i--
                {
                    T y 
            = heap[i]; // 子樹的根

                    
            // 尋找放置y的位置
                    int c = 2*i; // c 的父節點是y的目標位置
                            
                    
            while (c <= CurrentSize) 
                    {
                        
            // 使heap[c]是較大的子節點
                        if (c < CurrentSize 
                         
            && heap[c] < heap[c+1])
                            c
            ++;

                        
            // 能把y放入heap[c/2]嗎?
                        if (y >= heap[c]) 
                            
            break;  // 能

                        
            // 不能
                        heap[c/2= heap[c]; // 子節點上移
                        c *= 2;                 // 下移一層
                    }
                    heap[c
            /2= y;
                }
            }

            template
            <class T>
            void MaxHeap<T>::Output() const
            {
               cout 
            << "The " << CurrentSize 
                    
            << " elements are"<< endl;
               
            for (int i = 1; i <= CurrentSize; i++)
                   cout 
            << heap[i] << ' ';
               cout 
            << endl;
            }

            Feedback

            # re: 最小堆&&最大堆的實現(c++)(轉)[未登錄]  回復  更多評論   

            2014-04-04 09:47 by ccc
            錯誤也太多了吧。
            久久国产视频99电影| 伊人久久大香线蕉AV一区二区| 国产成人精品久久| 伊人久久五月天| 亚洲伊人久久大香线蕉综合图片| 日产精品久久久一区二区| 欧美激情精品久久久久| 久久久精品久久久久久| 99久久精品免费看国产一区二区三区 | 一级a性色生活片久久无| 亚洲精品乱码久久久久久自慰| 99久久99久久精品免费看蜜桃| 欧美久久一级内射wwwwww.| 久久综合给合久久狠狠狠97色| 国产精品免费久久| 人妻精品久久无码专区精东影业| 久久精品国产国产精品四凭| 国产精品99久久99久久久| 久久久久九九精品影院| 久久亚洲国产精品一区二区| 亚洲女久久久噜噜噜熟女| 欧美亚洲另类久久综合婷婷| 国产精品久久永久免费| 亚洲狠狠婷婷综合久久蜜芽 | 人妻精品久久无码区| 国产69精品久久久久9999| 蜜臀av性久久久久蜜臀aⅴ| 香蕉久久夜色精品国产2020 | 久久亚洲中文字幕精品有坂深雪 | 日本道色综合久久影院| 久久久久久九九99精品| 亚洲女久久久噜噜噜熟女| 久久99久国产麻精品66| 久久人做人爽一区二区三区| 亚洲欧洲久久av| 综合久久精品色| 亚洲精品97久久中文字幕无码| 久久人人爽人人爽人人片AV麻豆| 久久国产成人精品国产成人亚洲| 91久久精品国产成人久久| 国产亚洲美女精品久久久|