1 / 28

Bekti - Rita - Sevenhot

z. Struktur Data & Algoritma 2009. Bekti - Rita - Sevenhot. DEFINISI. DEFINISI. HEAP Sebuah complete binary tree dengan ketentuan sebagai berikut : Perbandingan suatu node dengan suatu nilai node child-nya mempunyai ketentuan tertentu berdasarkan jenis heap-nya Jenis heap

lamya
Download Presentation

Bekti - Rita - Sevenhot

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. z Struktur Data & Algoritma 2009 Bekti - Rita - Sevenhot

  2. DEFINISI DEFINISI • HEAP • Sebuah complete binary tree dengan ketentuan sebagai berikut : • Perbandingan suatu node dengan suatu nilai node child-nya mempunyai ketentuan • tertentu berdasarkan jenis heap-nya • Jenis heap • MAXHEAP (elemen maksimum di root) : • A[parent(i)] ≥ A[i] • nilai node lebih besar atau sama dengan nilai childnya • MINHEAP (elemen minimum di root) : • A[parent(i)] ≤ A[i] • nilai node lebih kecil atau sama dengan nilai childnya SDA - 2009

  3. CONTOH HEAP MAXHEAP MINHEAP 12 2 10 7 4 7 6 2 4 7 12 4 8 10 nilai node ≥ nilai childnya nilai node ≤ nilai childnya SDA - 2009

  4. Heap adalah suatu priority queue (mempertahankan antrian prioritas). • Dalam antrian prioritas, elemen yang dihapus adalah elemen yang mempunyai prioritas terbesar (atau terkecil, tergantung keperluan), • dan elemen inilah yang selalu terletak di akar (root). • Suatu heap sewaktu-waktu dapat berubah, baik itu dikarenakan penambahan elemen (insert) ataupun penghapusan elemen (delete). • Struktur data heap biasanya diimplementasikan dalam array SDA - 2009

  5. INDEXING INDEXING 1 parent(i) = i/2 left_child(i) = 2·i right_child(i) = 2·i + 1 3 2 4 5 6 7 SDA - 2009

  6. OPERASI HEAP OPERASI HEAP INSERT Memasukkan data baru ke dalam heap tree Contoh : insert 9 Karena 9 < 10, maka 10 tidak terjadi switch Proses insert berhenti 10 10 10 exchange 9 > 1 exchange 9 > 7 7 7 7 7 9 7 1 3 2 4 9 3 2 4 7 3 2 4 9 1 1 SDA - 2009

  7. OPERASI HEAP OPERASI HEAP ROOT DELETION Menghapus root pada heap tree Contoh : delete root 12 2 Elemen dengan indeks terbesar naik menjadi root HEAPIFY 7 10 7 10 1 3 4 2 1 3 4 SDA - 2009

  8. HEAPiFY HEAPIFY Definisi Proses mengubah binary tree menjadi heap tree Algoritma Heapify(A,i) l ← left [i] r ← right [i] if l ≤ heap-size [A] and A[l] > A[i] then largest ← l     else largest ← i if r ≤ heap-size [A] and A[r] > A[largest]     then largest ← r if largest  ≠ i     then exchange A[i] ↔ A[largest] Heapify (A, largest) SDA - 2009

  9. HEAPiFY HEAPIFY Contoh 2 10 10 7 > 2 10 > 7 7 > 2 7 10 7 7 2 7 1 3 4 1 1 3 3 2 7 SDA - 2009

  10. HEAP SORT HEAP SORT • HEAP SORT ALGORITHM • adalah algoritma sorting yang berdasarkan pada struktur data heap tree • Kompleksitas running time-nye : O(nlog(n)) • Langkah : • Susun array A kedalam heap tree • Ambil root pada heap tree, masukkan ke array • Hapus elemen root (dengan mempertahankan sifat heap, lakukan heapify jika tidak memenuhi sifat heap) • Lakukan terus (langkah 2-3) hingga tree menjadi kosong SDA - 2009

  11. ALGORITHM HEAP SORT ALGORITHM HEAPSORT (A) BuildHeap (A) for i ← length (A) down to 2 do exchange A[1] ↔ A[i] heap-size [A] ← heap-size [A] - 1 Heapify (A, 1) SDA - 2009

  12. Heapify(A,i) l ← left [i] r ← right [i] if l ≤ heap-size [A] and A[l] > A[i] then largest ← l     else largest ← i if r ≤ heap-size [A] and A[r] > A[largest]     then largest ← r if largest  ≠ i     then exchange A[i] ↔ A[largest] Heapify (A, largest) BuildHeap(A) heap-size (A) ← length [A] for i ←  floor(length[A]/2) down to 1 do       Heapify (A, i) SDA - 2009

  13. SIMULASI SIMULASI HEAP SORT Array A = [1, 4, 89, 77, 12, 50, 23] BuildHeap(A) SDA - 2009

  14. SIMULASI SIMULASI HEAP SORT Array A = [1, 4, 89, 77, 12, 50, 23] BuildHeap(A) insert 1 1 SDA - 2009

  15. SIMULASI SIMULASI HEAP SORT Array A = [1, 4, 89, 77, 12, 50, 23] BuildHeap(A) insert 4 insert 1 1 4 1 4 1 SDA - 2009

  16. SIMULASI SIMULASI HEAP SORT Array A = [1, 4, 89, 77, 12, 50, 23] BuildHeap(A) insert 4 insert 1 insert 89 1 4 1 4 89 4 1 1 89 1 4 SDA - 2009

  17. SIMULASI SIMULASI HEAP SORT BuildHeap(A) insert 77 89 89 1 4 77 4 1 77 SDA - 2009

  18. SIMULASI SIMULASI HEAP SORT BuildHeap(A) insert 77 insert 12 89 89 89 1 4 77 4 77 4 1 77 12 1 SDA - 2009

  19. SIMULASI SIMULASI HEAP SORT BuildHeap(A) insert 50 insert 77 insert 12 89 89 89 89 89 1 4 77 4 77 4 77 4 77 50 50 4 1 77 12 12 12 1 1 1 SDA - 2009

  20. SIMULASI SIMULASI HEAP SORT BuildHeap(A) insert 23 89 77 50 4 23 12 1 SDA - 2009

  21. SIMULASI SIMULASI HEAP SORT 89 23 77 Delete root Heapify 77 50 77 50 23 50 4 4 4 23 12 12 12 1 1 1 sorted SDA - 2009

  22. SIMULASI SIMULASI HEAP SORT 77 4 50 Delete root Heapify 23 50 23 50 23 4 4 12 12 12 1 1 1 sorted SDA - 2009

  23. SIMULASI SIMULASI HEAP SORT 50 12 23 Delete root Heapify 23 4 23 4 12 4 12 1 1 1 sorted SDA - 2009

  24. SIMULASI SIMULASI HEAP SORT 23 1 12 Delete root Heapify 12 4 12 4 1 4 1 sorted SDA - 2009

  25. SIMULASI SIMULASI HEAP SORT 12 4 Delete root 1 4 1 sorted SDA - 2009

  26. SIMULASI SIMULASI HEAP SORT 12 4 Delete root Delete root 1 1 4 1 sorted SDA - 2009

  27. SIMULASI SIMULASI HEAP SORT 12 4 Delete root Delete root Delete root 1 1 4 1 sorted SDA - 2009

  28. Selesai & Terima kasih SDA - 2009

More Related