基於堆的基本操作的介紹

來源:互聯網
上載者:User

  我們期望的資料結構能支援插入操作,並能方便地從中取出具有最小或最大關鍵碼的記錄,這樣的資料結構即為優先順序隊列。在優先順序隊列的各種實現中,堆是最高效的一種資料結構。
  最小堆:任一結點的關鍵碼均小於或等於它的左右子女的關鍵碼,位於堆頂的結點的關鍵碼是整個元素集合的最小的,所以稱它為最小堆。最大堆類似定義。

  建立堆:採用從下向上逐步調整形成堆得方法來建立堆。為下面的分支結點調用下調演算法siftDown,將以它們為根的子樹調整為最小堆。從局部到整體,將最小堆逐步擴大,直到將整個樹調整為最小堆。

  插入一個元素:最小堆的插入演算法調用了另一種堆得調整方法siftUp,實現自下而上的上滑調整。因為每次新結點總是插在已經建成的最小堆後面,這時必須遵守與sift相反的比較路徑,從下向上,與父結點的關鍵碼進行比較,對調。

  刪除一個元素:從最小堆刪除具有最小關鍵碼記錄的操作時將最小堆的堆頂元素,即其完全二叉樹的順序表示的第0號元素刪去,去把這個元素取走後,一般以堆得最後一個結點填補取走的堆頂元素,並將堆的實際元素個數減1.但是用最後一個元素取代堆頂元素將破壞堆,需要調用siftDown演算法進行調整堆。

本文代碼均以最小堆的實現為例。

複製代碼 代碼如下:#include<iostream>
#include<assert.h>
usingnamespace std;

constint maxheapsize=100;
staticint currentsize=0;

//從上到下調整堆
void siftDown(int* heap,int currentPos,int m)
{
int i=currentPos;
int j=currentPos*2+1;//i's leftChild
int temp=heap[i];
while(j<=m)
{
if(j<m&&heap[j]>heap[j+1]) j++;// j points to minChild
if(temp<=heap[j]) break;
else
{
heap[i]=heap[j];
i=j;
j=2*i+1;
}
}
heap[i]=temp;
}

//從下向上調整堆
void siftUp(int* heap, int start)
{
int i=start,j=(i-1)/2;
int temp=heap[i];

while(i>0)
{
if(heap[j]>temp)
{
heap[i]=heap[j];
i=j;
j=(i-1)/2;
}
elsebreak;
}
heap[i]=temp;
}

//構建堆
int* Heap(int*arr, int size)
{
int i;
currentsize=size;
int* heap =newint[maxheapsize];
assert(heap!=NULL);
for(i=0;i<currentsize;i++) heap[i]=arr[i];
int currentPos=(currentsize-2)/2;
while(currentPos>=0)
{
siftDown(heap,currentPos,currentsize-1);
currentPos--;
}
return heap;
}

//增加一個元素
void insert(int* heap,int value)
{
if(currentsize>=maxheapsize)
{
cout<<"Heap is full!"<<endl;
return ;
}
heap[currentsize]=value;
siftUp(heap,currentsize);
currentsize++;
}

//刪除一個元素,並返回刪除前的堆頂元素
int removemin(int* heap)
{
assert(currentsize>=0);
int removeValue=heap[0];
heap[0]=heap[currentsize-1];
currentsize--;
siftDown(heap,0,currentsize-1);
return removeValue;
}

int main()
{
constint size=10;
int arr[size]={2,1,3,0,8,1,6,9,7,10};
int* heap=Heap(arr,size);
//堆排序
for(int i=0;i<size;i++)
{
arr[i]=removemin(heap);
cout<<arr[i]<<endl;
}
delete []heap;
return0;

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.