1. 程式人生 >實用技巧 >其它 >最小堆和最大堆的建立以及基本操作

最小堆和最大堆的建立以及基本操作


前言:

堆的特性:用陣列表示的完全二叉樹。有序性:任一結點的關鍵字是其子樹所有結點的最大值 (最小值)
堆的本質:就是一顆 完全二叉樹
堆的資料儲存: 用的是 陣列
建堆時主要的操作:就是調整 對陣列的元素按照已有的順序建立的完全二叉樹 進行調整(根據建最大堆或是最小堆)。

建立最小堆

#include<bits/stdc++.h>
using namespace std;

// 根據 已有的陣列 進行調整(向下調整)如果 孩子比關鍵值小 就交換;
void downadjust(int heap[],int adjust,int last){//adjust時待調整的下標 last是陣列最後的下標
	int i = adjust;
    int j = 2 * i;//為i的左孩子

    while ( j <= last)
    {
        if( j + 1 <=last && heap[j+1] < heap[j]){//比較 關鍵值的左右孩子 誰更小
            j++;
        }

        if( heap[i] > heap[j]){//如果待調整的點比其孩子值(已將其左右孩子的最小值進行比較)大 那麼就交換
            swap(heap[i],heap[j]);
            i = j;    //完成交換 將其孩子的下標賦值給 待調整值得下標
            j = 2 * i;//因為 現在 待調整點得下標更新了 所以 其孩子得下標也得更新 (方便繼續向下調整)
        }

        else{
            break;//如果待調整的值 已經比其孩子的值小那就不需要調整;
        }
    }
    
} 

//建立堆的函式
void creatheap(int heap[],int n){
    int i;
    for(i = n / 2; i >= 1; i--){//完全二叉樹當中其[1,n/2]為非葉子結點的下標  在已經有的完全二叉樹當中(進行調整,那麼調整的效果 從非葉子結點開始較好)
        downadjust(heap,i,n);
    }
}

//刪除堆頂元素
int deletetop(int heap[],int last){
	
	int temp = heap[1]; 
    heap[1] = heap[last];//將最後一個元素賦值給堆頂元素 然後再進行向下調整
    heap[last] = temp;
    last--;
    downadjust(heap,1,last);//從 堆頂開始 向下調整 
	return temp;
}

//(向上調整 與其雙親比較大小 如果 比雙親小 那就交換)插入元素要用的函式
void upadjust(int heap[],int last){//向上調整 (將要插入的元素放到陣列的最後面,然後與其雙親進行比較)
     int i = last;
     int j = i / 2;//表示其雙親

     while( j >= 1){//只要父母不是堆頂就調整
         
         if(heap[i] < heap[j]){
            swap(heap[i], heap[j]);
            i = j; //將其父母的下標賦值給待調整的下標
            j = i/2;//更新 其父母的下標
         }
         else{
             break;//說明插入的已經比其父母的值小 那就不用調整。
         }
     }
}

//插入函式
void insert(int heap[],int last,int x){
    heap[++last] = x; //++last先開闢空間比如 last=8 ;++last = 9; last++ = 8;
    upadjust(heap,last);
}

//堆排序  (這樣輸出的順序 是降序)
void sortheap(int heap[],int last){
    
    for(int i = last; i >= 1; i--){
        swap(heap[1],heap[i]);
        downadjust(heap,1,i-1);//每次將最小的放到最後面 然後調整 前面(i-1)個元素
    }
}

int main(){
    int heap[6] ={0,2,1,3,4,5}; //陣列的第一個值為0 我們是從 i = 1;開始的, 如果 沒有 0 那麼2對應的下標就為0    

    creatheap(heap,5);
    
//    cout << "依次刪除堆頂元素 那麼出來的順序是升序 " << endl;
//    for(int i = 5; i >= 1; i--){
//    	int number = deletetop(heap,i);
//        cout<< number <<' ';
//    }
     
    cout<<endl;
    
    cout<<"插入元素7 並逆序輸出"<<endl;
    insert(heap,5,7);
    sortheap(heap,6);
    for(int i = 1; i < 7; i++){//如果想要升序輸出的話可以 將 i=6;i>=1;i--;即可升序輸出 
        printf("%d ",heap[i]);
    }
}

執行 結果 :

最小堆和最大堆的建立以及基本操作

建立最大堆

#include<bits/stdc++.h>
using namespace std; 

//向下調整建堆函式。 
void downadjust(int heap[],int adjust,int last){     //heap[]為實現堆的陣列,adjust是待調整結點的下標,last為最後一個數組元素的小標。
    int i=adjust,j=2*i;                              //i為工作下標,j是預調整結點的左孩子。 
    while(j<=last){
        if(j+1<=last&&heap[j+1]>heap[j]){            //選擇左右孩子結點值更大的那一個 
            j++;
        } 
        if(heap[j]>heap[i]){                        // 如果左右孩子中最大的那個都比待調整結點要大 
            swap(heap[i],heap[j]);               //交換兩個結點的值 
            i=j;                                 //i更新,始終記錄待調整節點的下標 
            j=2*i;                               //j更新為i的左孩子 
        } 
        else{                          //如果待調整結點的值比左右孩子結點都要大,那麼停止。 
            break;
        }
    }   
}
void creatheap(int heap[],int n){           //堆的建立 
    for(int i=n/2;i>=1;i--){                //從最後一個非葉子結點進行 
        downadjust(heap,i,n);
    }
} 
void del(int heap[],int n){          //從堆中刪除結點。 堆頂元素出去。  n為結點的個數 
    heap[1]=heap[n];                 //用最後一個元素將第一個元素(堆頂元素彈出)覆蓋,然後從上往下調整 
    n--;
    downadjust(heap,1,n);
} 
void upadjust(int heap[],int last){     //新增一個元素,首先將其新增到堆的最後一個位置,然後向上調整即可 
    int i=last,j=i/2;                //i記錄最後一個元素(即待調整元素)的下標,j記錄了i的父親結點的下標 
    while(j>=1){
        if(heap[i]>heap[j]){         //如果待調整元素的值要大於其父親節點的值,那麼交換 
            swap(heap[i],heap[j]);
            i=j;
            j=i/2;                 //更新i,j 
        }
        else{
            break;
        }
    }
}
void insert(int heap[],int n,int x){   // 將新元素x新增到最後一個位置。然後向上調整 
    heap[++n]=x;
    upadjust(heap,n);
}

void heapsort(int heap[],int n){    
    creatheap(heap,n);               //建堆 
    for(int i=n;i>=1;i--){     //從後往前遍歷 
        swap(heap[1],heap[i]);      //交換堆頂和i號位的結點 
        downadjust(heap,1,i-1);    //調整 
    }
}

int main()
{
    int heap[13]={0,3,6,1,9,3,13,56,77,12,45,43,65};
    heapsort(heap,12) ;

    for(int i=1;i<=12;i++){
        printf("%d ",heap[i]);
    }


}

執行結果:

最小堆和最大堆的建立以及基本操作

結尾

加油 陌生人 指的是默默努力素未謀面的生人;