instructor: ching-chi lin 林清池 助理教授 chingchi.lin@gmail

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

If you can't read please download the document

Upload: nerita

Post on 06-Jan-2016

60 views

Category:

Documents


5 download

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 Presentation

TRANSCRIPT

  • Instructor: Ching-Chi Lin [email protected] of Computer Science and Engineering National Taiwan Ocean University Data Structures MAX HEAP

  • Max heap*

    Min heap

    Ch5,P.62

  • Max and Min heaps141210876965330252710846102050831121Max heap.Min heap.

  • Insertion into a Max heap*Max heap

    bubbling uproot

  • Insertion into a Max heap*2015214102015214101Is a max heap!Insert 1.

  • Insertion into a Max heap201514102Insert 5.2015141025Not max heap!Bubbling up.2015141052Max heap!

  • Insertion into a Max heap20151410220151410221Insert 21.Not max heap!Bubbling up.20151410212Not max heap!Bubbling up.21151410202Max heap.

  • 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;*

  • Insert into a max heapvoid 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;}*

  • Insertion into a Max heap-PseudoCode*Ch5,P.69-70

  • Deletion from a Max heap*

    rootbubbling downchild leaf node

  • Deletion from a Max heap21151410202Delete 21.151410202Move 2 to root.215141020Not max heap!Bublling down!201514102Max heap!

  • Deletion from a Max heap 1/2201514102Delete 20.1514102Move 10 to root.1015142Not max heap!Bubbling down!1510142

  • Deletion from a Max heap 2/2Not max heap!Bubbling down!1514102Max heap!1510142

  • Deletion from a Max 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 = 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*heap[parent] = temp;return item;}

  • *Max heapMax heapMax heapMax heapLevel order

    heap.txtMax heapnode/node

  • *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]

    *********