• <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 鑫龍 閱讀(1206) 評論(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久久这里只精品国产免费| 九九热久久免费视频| 欧美亚洲国产精品久久| 久久婷婷五月综合国产尤物app| 国产产无码乱码精品久久鸭| 久久精品视频免费| 久久亚洲av无码精品浪潮| 中文字幕无码免费久久| 狠狠色噜噜狠狠狠狠狠色综合久久| 久久96国产精品久久久| 久久久久国产精品嫩草影院| 久久青青草原国产精品免费| 国内精品久久久久影院薰衣草 | 久久久无码精品亚洲日韩软件| 无码人妻精品一区二区三区久久| 国产V亚洲V天堂无码久久久| 一本久久a久久精品亚洲| 久久美女人爽女人爽| 亚洲中文久久精品无码ww16| 亚洲国产成人久久笫一页| 中文字幕无码av激情不卡久久| 久久久久这里只有精品| 久久久久久国产精品无码下载| 午夜精品久久久久久久无码| 久久国产影院| 久久强奷乱码老熟女网站| 99久久精品免费看国产一区二区三区| 中文字幕久久精品| 久久精品国产久精国产思思| 一级做a爰片久久毛片人呢| 久久精品国产精品亚洲下载| 久久精品国产亚洲AV不卡| 一本色道久久综合狠狠躁| 一本大道加勒比久久综合| 久久人人青草97香蕉| 国产91色综合久久免费| 亚洲色大成网站WWW久久九九|