1 / 18

Instructor: Ching-Chi Lin 林清池 助理教授 chingchi.lin@gmail

Data Structures 實習十 最大累堆 ( MAX HEAP ). Department of Computer Science and Engineering National Taiwan Ocean University. Instructor: Ching-Chi Lin 林清池 助理教授 chingchi.lin@gmail.com. Max heap 的定義. 是一個完整二元樹。 是一個最大樹。 所有的父節點一定比子節點來的大。 反之, Min heap 的父節點一定比子節點來的小。

nerita
Download Presentation

Instructor: Ching-Chi Lin 林清池 助理教授 chingchi.lin@gmail

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Data Structures實習十 最大累堆(MAX HEAP) Department of Computer Science and EngineeringNational Taiwan Ocean University Instructor: Ching-Chi Lin林清池 助理教授 chingchi.lin@gmail.com

  2. Max heap的定義 • 是一個完整二元樹。 • 是一個最大樹。 • 所有的父節點一定比子節點來的大。 • 反之,Min heap的父節點一定比子節點來的小。 • 請參考課本Ch5,P.62

  3. MaxandMin heaps 14 9 30 12 7 6 3 25 10 8 6 5 Max heap. 10 11 2 20 83 21 7 4 50 10 8 6 Min heap.

  4. InsertionintoaMax heap • 新增節點時,依然需要維持Max heap的性質。 • 完整二元樹。 • 最大樹。 • 新增節點的步驟: • 依照完整二元樹的定義,加上新增的節點。 • 從新節點開始使用 bubbling up(冒泡)的過程,來計算新節點的位置。 • 判斷父節點是不是比新增加的節點小。 • 如果是的話則交換位置,如果不是的話就跳出。 • 重複上列判斷,直到跳出或是新節點已位在root。

  5. InsertionintoaMax heap 20 20 Insert 1. 15 2 15 2 14 10 14 10 1 Is a max heap!

  6. InsertionintoaMax heap 20 20 Not max heap! Insert 5. 15 2 15 2 Bubbling up. 14 10 14 10 5 20 15 5 Max heap! 14 10 2

  7. InsertionintoaMax heap 20 20 Not max heap! Insert 21. 15 Bubbling up. 2 15 2 14 10 21 14 10 21 20 Max heap. Not max heap! Bubbling up. 15 20 15 21 14 10 2 14 10 2

  8. Declarations: #define MAX_ELEMENTS 200 /* maximum heap size+1 */ #define HEAP_FULL (n) (n == MAX_ELEMENTS −1) #define HEAP_EMPTY (n) (!n) typedef struct { int key; /* other fields */ } element; element heap[MAX_ELEMENTS]; int n = 0;

  9. Insert into a max heap void push (element item, int *n) { /* insert item into a max heap of current size *n */ int i; if (HEAP_FULL (*n)) { fprintf(stderr, “The heap is full. \n”); exit (EXIT_FAILURE); } i = ++ (*n); while ((i != 1) && (item.key > heap[i/2].key)) { heap[i] = heap [i/2]; i /= 2; } heap[i] = item; }

  10. InsertionintoaMax heap-PseudoCode • 請參考課程投影片Ch5,P.69-70。

  11. DeletionfromaMax heap • 當我們要從最大累堆刪除一個元素,我們永遠從累堆的根節點刪除。 • 從最大累堆刪除一個元素的步驟。 • 刪除根節點。 • 將最後一個節點插入根節點。 • 從root開始使用bubbling down(冒泡)過程來保證目前的累堆依然是最大累堆。 • 從兩個child中,找出比較大的那一個。 • 判斷找出的節點是不是比目前的節點小,如果是的話則交換位置   ;如果不是的話就跳出。 • 重複以上判斷,直到跳出或是該節點已是leaf node。

  12. DeletionfromaMax heap 21 Delete 21. Move 2 to root. 15 20 15 20 14 10 2 14 10 2 20 2 Not max heap! Bublling down! 15 2 15 20 Max heap! 14 10 14 10

  13. DeletionfromaMax heap 1/2 20 Delete 20. Move 10 to root. 15 2 15 2 14 10 14 10 15 10 Not max heap! Bubbling down! 10 2 15 2 14 14

  14. DeletionfromaMax heap 2/2 15 15 Not max heap! 14 2 10 2 Bubbling down! 10 14 Max heap!

  15. DeletionfromaMax heap-PseudoCode 1/2 • 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; }

  16. DeletionfromaMax heap-PseudoCode 2/2 heap[parent] = temp; return item; }

  17. 練習 • 程式需求。 • 新增一個空的Max heap(使用陣列實現)。 • 插入值至Max heap。 • 由Max heap刪除一值。 • 輸出Max heap的內容(Level order)。 • 測試資料: • 請至教學網站下載heap.txt。 • 每個要輸入的數字用空白隔開。 • 依序刪除Max heap中的node。 • 每輸入/刪除一個node,即印出整棵樹。

  18. 輸出範例 • 1[1] • 1[5]2[1] • 1[9]2[1]3[5] • 1[12]2[9] 3[5]4[1] • 1[13] 2[12] 3[5]4[1] 5[9] • 1[20] 2[12] 3[13]4[1] 5[9] 6[5] • 1[22] 2[12] 3[20] 4[1] 5[9] 6[5] 7[13] • 1[20] 2[12] 3[13]4[1] 5[9] 6[5] • 1[13] 2[12] 3[5] 4[1] 5[9] • 1[12] 2[9]3[5] 4[1] • 1[9]2[1]3[5] • 1[5]2[1] • 1[1]

More Related