• <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
            最小堆:
            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尋找應(yīng)插入的位置
                
            //i從新的葉節(jié)點(diǎn)開始,并沿著樹上升
                int i = ++CurrentSize;
                
            while (i != 1 && x < heap[i/2]) 
                {
                    heap[i] 
            = heap[i/2]; // 將元素下移
                    i /= 2;                 // 移向父節(jié)點(diǎn)
                }
                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,  // 堆的當(dāng)前節(jié)點(diǎn)
                   ci = 2;    // i的子節(jié)點(diǎn)

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

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

                    
            // 不能
                    heap[i] = heap[ci]; // 子節(jié)點(diǎn)上移
                    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;

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

                    
            // 尋找放置y的位置
                    int c = 2*i; // c 的父節(jié)點(diǎn)是y的目標(biāo)位置

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

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

                        
            // 不能
                        heap[c/2= heap[c]; // 子節(jié)點(diǎn)上移
                        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尋找應(yīng)插入的位置
                
            //i從新的葉節(jié)點(diǎn)開始,并沿著樹上升
                int i = ++CurrentSize;
                
            while (i != 1 && x > heap[i/2]) 
                {
                    heap[i] 
            = heap[i/2]; // 將元素下移
                    i /= 2;              // 移向父節(jié)點(diǎn)
                }

                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,  // 堆的當(dāng)前節(jié)點(diǎn)
                   ci = 2;    // i的子節(jié)點(diǎn)

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

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

                    
            //不能
                    heap[i] = heap[ci]; // 子節(jié)點(diǎn)上移
                    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;

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

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

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

                        
            // 不能
                        heap[c/2= heap[c]; // 子節(jié)點(diǎn)上移
                        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: 最小堆&&最大堆的實(shí)現(xiàn)(c++)(轉(zhuǎn))[未登錄]  回復(fù)  更多評論   

            2014-04-04 09:47 by ccc
            錯誤也太多了吧。
            波多野结衣中文字幕久久| 久久er热视频在这里精品| 久久久无码一区二区三区| 波多野结衣久久| 久久久久国产精品嫩草影院| 国内精品综合久久久40p| 久久这里只有精品18| 精品久久久久久久| 精品久久人妻av中文字幕| 粉嫩小泬无遮挡久久久久久| 久久99精品久久久久久hb无码| 亚洲国产一成人久久精品| 国产精品天天影视久久综合网| 久久精品国产只有精品66 | 亚洲AV日韩AV天堂久久| 久久成人国产精品免费软件| 香港aa三级久久三级老师2021国产三级精品三级在 | 99久久精品免费看国产一区二区三区 | 久久国产成人午夜aⅴ影院| 久久久久久精品成人免费图片| 久久亚洲精品无码aⅴ大香| 中文字幕久久久久人妻| 久久久无码精品亚洲日韩蜜臀浪潮| 国产成人精品久久二区二区| 久久99国产亚洲高清观看首页| 久久99精品国产麻豆蜜芽| 一本一道久久综合狠狠老 | 亚洲AV无码一区东京热久久| a高清免费毛片久久| 理论片午午伦夜理片久久| 奇米影视7777久久精品| 国产V综合V亚洲欧美久久| 久久久久亚洲AV无码专区桃色| 久久伊人五月丁香狠狠色| 丁香五月网久久综合| 亚洲精品无码久久毛片| 99麻豆久久久国产精品免费 | 国产精品久久久久aaaa| 久久受www免费人成_看片中文| 97久久超碰成人精品网站| 久久久精品日本一区二区三区 |