2.5 - Heap
介紹heap、運作及其應用
1. 定義
可以分為max-heap與min-heap(以max-heap為例)
本身是complete binary tree(適合用array存放)
所有節點的parent值都大於其children
root具有最大值
40
/ \
20 10
/ \
5 9
2. 運作
2.1 Insert x in heap
將x存放於最後一個節點的下一個位置
往上和父點比較,若父點叫小則交換,直到root。
2.2 Delete max in heap
取出root的最大值
將最後一個節點刪除,並放到root中
從root往下調整,直到root為最大值
2.3 Build a Heap(Top-Down)
從上到下使用insert()
來建立Heap。
Time:
2.4 Build a Heap(Bottom-Up)
先將資料以Complete Binary Tree呈現
從最後一個父點開始調整成Heap,直到Root
Time:
假設height= ,節點總數= ,某一節點位於第i階(level)
則調整節點最大成本為 ,level i最多有 個節點
總成本:
Time = O(n)
void AdjustBottomUp(char *heap, int i, int n){
int j = 2*i+1; //左子點
while(j <= n){
if (j < n)
if (heap[j] < heap[j+1])
Swap(&heap[j], &heap[j+1]); 左右子點交換
if (heap[i] > heap[j])
break;
else{
Swap(&heap[j/2], &heap[j]); //父點與子點交換
j=j*2+1;
i=(j-1)/2;
}
}
}
void BuildHeap(char *heap){
int n = strlen(heap);
for (int i = n/2-1; i>=0; i--){ //i從最後一個父點開始
AdjustBottomUp(heap, i, n-1);
}
}
void Swap(char *a, char *b){
char temp = *a;
*a = *b;
*b = temp;
}
3. 應用
製作priority queue的最佳資料結構,因為insert(),delete_max()皆只需O(logn)。
Heap Sort
Last updated