instructor: ching-chi lin 林清池 助理教授 chingchi.lin@gmail
DESCRIPTION
Data Structures 實習十 最大累堆 ( MAX HEAP ). Department of Computer Science and Engineering National Taiwan Ocean University. Instructor: Ching-Chi Lin 林清池 助理教授 [email protected]. Max heap 的定義. 是一個完整二元樹。 是一個最大樹。 所有的父節點一定比子節點來的大。 反之, Min heap 的父節點一定比子節點來的小。 - PowerPoint PPT PresentationTRANSCRIPT
Instructor: Ching-Chi Lin林清池 助理教授
Department of Computer Science and EngineeringNational Taiwan Ocean University
Data Structures實習十 最大累堆( MAX HEAP )
Max heap 的定義
2
是一個完整二元樹。
是一個最大樹。 所有的父節點一定比子節點來的大。 反之, Min heap 的父節點一定比子節點來的小。
請參考課本 Ch5,P.62
Max and Min heaps
14
12
10 8
7
6
9
6
5
3
30
25
2
7
10 8
4
6
10
20
50
83
11
21
Max heap.
Min heap.
Insertion into a Max heap
4
新增節點時,依然需要維持 Max heap 的性質。 完整二元樹。 最大樹。
新增節點的步驟: 依照完整二元樹的定義,加上新增的節點。 從新節點開始使用 bubbling up (冒泡)的過程,來計算新節點的位置。
判斷父節點是不是比新增加的節點小。 如果是的話則交換位置,如果不是的話就跳出。
重複上列判斷,直到跳出或是新節點已位在 root 。
Insertion into a Max heap
5
20
15 2
14 10
20
15 2
14 10 1
Is a max heap!
Insert 1.
Insertion into a Max heap
20
15
14 10
2Insert 5.
20
15
14 10
2
5
Not max heap!
Bubbling up.
20
15
14 10
5
2
Max heap!
Insertion into a Max heap
20
15
14 10
2
20
15
14 10
2
21
Insert 21. Not max heap!
Bubbling up.
20
15
14 10
21
2
Not max heap!Bubbling up.
21
15
14 10
20
2
Max heap.
Insertion into a Max heap-PseudoCode
8
請參考課程投影片 Ch5,P.69-70 。
Deletion from a Max heap
9
當我們要從最大累堆刪除一個元素,我們永遠從累堆的根節點刪除 。
從最大累堆刪除一個元素的步驟。 刪除根節點。 將最後一個節點插入根節點。 從 root 開始使用 bubbling down (冒泡)過程來保證目前的累
堆依然是最大累堆。 從兩個 child 中,找出比較大的那一個。 判斷找出的節點是不是比目前的節點小,如果是的話則交換位
置 ;如果不是的話就跳出。 重複以上判斷,直到跳出或是該節點已是 leaf node 。
Deletion from a Max heap
21
15
14 10
20
2
Delete 21.15
14 10
20
2
Move 2 to root.
2
15
14 10
20
Not max heap!
Bublling down!
20
15
14 10
2Max heap!
Deletion from a Max heap 1/2
20
15
14 10
2
Delete 20.
15
14 10
2Move 10 to root.
10
15
14
2
Not max heap!
Bubbling down!
15
10
14
2
Deletion from a Max heap 2/2
Not max heap!
Bubbling down!
15
14
10
2
Max heap!
15
10
14
2
Deletion from a Max heap-PseudoCode 1/2
13
element delete_max_heap(int* n)
{ /*delete element with the highest key from the heap.*/
int parent,child; element item,temp;
if (Heap_Empty(*n)) { fprintf(stderr,”The heap is empty.\n”); exit(1);
}
item = heap[1]; /*save value of the element with the highest key.*/
temp = heap[(*n)--]; /*use last element in heap to adjust heap.*/
parent = 1; child = 2;
while (child <= *n) { /*find the larger child of the current parent.*/
if (child < *n) && (heap[child].key < heap[child+1].key) child++;
if (temp.key >= heap[child].key) break; /*move to the next lower level.*/
heap[parent] = heap[child]; parent = child; child* = 2;
}
Deletion from a Max heap-PseudoCode 2/2
14
heap[parent] = temp;
return item;
}
練習
15
程式需求。 新增一個空的 Max heap (使用陣列實現)。 插入值至 Max heap 。 由 Max heap 刪除一值。 輸出 Max heap 的內容( Level order )。
測試資料: 請至教學網站下載 heap.txt 。 每個要輸入的數字用空白隔開。 依序刪除 Max heap 中的 node 。 每輸入 / 刪除一個 node ,即印出整棵樹。
練習 以數字代表完整二元樹中的每個位置,以此類推。
1
2
4 5
3
6 7
輸出答案 (insertion)
17
1[247] 1[247] 2[24] 1[534] 2[24] 3[247] 1[775] 2[534] 3[247] 4[24] 1[775] 2[637] 3[247] 4[24] 5[534] 1[775] 2[637] 3[331] 4[24] 5[534] 6[247] 1[775] 2[637] 3[331] 4[24] 5[534] 6[247] 7[5] 1[775] 2[637] 3[331] 4[589] 5[534] 6[247] 7[5] 8[24] 1[964] 2[775] 3[331] 4[637] 5[534] 6[247] 7[5] 8[24] 9[589]
輸出答案 (deletion) 1[775] 2[637] 3[331] 4[589] 5[534] 6[247] 7[5] 8[24] 1[637] 2[589] 3[331] 4[24] 5[534] 6[247] 7[5] 1[589] 2[534] 3[331] 4[24] 5[5] 6[247] 1[534] 2[247] 3[331] 4[24] 5[5] 1[331] 2[247] 3[5] 4[24] 1[247] 2[24] 3[5] 1[24] 2[5] 1[5]
18