1 / 43

Algoritma dan Struktur Data

Algoritma dan Struktur Data. AVL Tree. Outline. AVL Tree Definisi Sifat Operasi. AVL Tree. Binary Search Trees yang tidak imbang memiliki efisiensi yang buruk . Worst case: O(n). AVL ( Adelson-Velskii & Landis) tree adalah BST yang imbang .

Télécharger la présentation

Algoritma dan Struktur Data

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. Algoritma dan Struktur Data AVL Tree

  2. Outline • AVL Tree • Definisi • Sifat • Operasi

  3. AVL Tree • Binary Search Trees yang tidakimbangmemilikiefisiensi yang buruk. Worst case: O(n). • AVL (Adelson-Velskii & Landis) tree adalah BST yang imbang. • Setiap node di AVL tree memiliki balance factor bernilai -1, 0, atau 1. X X H H-2 H-1

  4. 10 10 5 20 5 20 43 3 3 1 2 1 3 AVL Tree 5

  5. AVL Tree 12 8 16 4 10 14 2 6

  6. Penyisipan node di AVL Tree • Setelah insert 1 12 8 16 4 10 14 2 6 1

  7. Penambahan node di AVL Tree • Untuk menjaga tree tetap imbang, setelah penyisipan sebuah node, dilakukan pemeriksaan dari node baru → root. Node pertama yang memiliki |balance factor| > 1 diseimbangkan • Proses penyeimbangan dilakukan dengan: • Single rotation • Double rotation

  8. AVL Tree Balance Factor • Balance factor = HL – HR • Balance factor node di AVL tree harus +1, 0, -1 • Identifier: • LH left high (+1) left subtree lebih panjang dari right subtree. • EH even high (0) subtree kiri dan kanan heightnya sama. • RH right high (-1)left subtree lebih pendek dari right subtree.

  9. Menyeimbangkan AVL Tree • AVL trees diseimbangkan dengan merotasikan node ke kiri atau ke kanan • Kasus penyeimbangan pada sebuah node: 1. Left of left: mengalami left high dan left subtreenya mengalami left high. 2. Right of right: mengalami right high dan right subtreenya mengalami right high. 3. Right of left: Mengalami left high dan left subtreenya mengalami right high. 4. Left of right: Mengalami right high dan right subtreenya mengalami left high.

  10. HL =3 HR =1 HR =3 HL =1

  11. (continued) HL =3 HR =1 HL =1 HR =3

  12. Case 1: Left of Left

  13. Case 2: Right of Right

  14. Case 3: Right of Left

  15. Case 4: Left of Right

  16. 11 11 8 20 4 20 4 16 27 3 16 8 27 3 Contoh • Sisipkan 3 ke AVL tree 8

  17. 11 11 8 20 5 20 4 16 27 4 16 8 27 5 Contoh • Penyisipan 5 ke AVL tree 8

  18. Latihan • Sisipkan data berikut ke AVL tree secara berurutan: • 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55

  19. Menghapus node di AVL Tree • Proses menghapus sebuah node di AVL tree hampir sama dengan BST. Penghapusan sebuah node dapat menyebabkan tree tidak imbang • Setelah menghapus sebuah node, lakukan pengecekan dari node yang dihapus → root. • Gunakan single atau double rotation untuk menyeimbangkan node yang tidak imbang. • Pencarian node yang imbalance diteruskan sampai root.

  20. Menghapus node di AVL Tree • Tahap penghapusan: • Case 1: X merupakan leaf, hapus X • Case 2: jika X memiliki 1 child, gunakan child tersebut untuk menggantikan X. Kemudian hapus X • Case 3: Jika X memiliki 2 child, ganti nilai X dengan node terbesar pada left subtree atau node terkecil pada right subtree. Hapus node yang nilainya digunakan untuk mengganti X • Tahap menyeimbangkan node yang balance factornya tidak -1, 0, 1, dilakukan dari node yang dihapus menuju root.

  21. Delete 55 (case 1) 60 20 70 10 40 65 85 80 90 5 15 30 50 55

  22. Delete 55 (case 1) 60 20 70 10 40 65 85 80 90 5 15 30 50 55

  23. Delete 50 (case 2) 60 20 70 10 40 65 85 80 90 5 15 30 50 55

  24. Delete 50 (case 2) 60 20 70 10 40 65 85 50 80 90 5 15 30 55

  25. prev Delete 60 (case 3) 60 20 70 10 40 65 85 80 90 5 15 30 50 55

  26. Delete 60 (case 3) 55 20 70 10 40 65 85 80 90 5 15 30 50

  27. prev Delete 55 (case 3) 55 20 70 10 40 65 85 80 90 5 15 30 50

  28. Delete 55 (case 3) 50 20 70 10 40 65 85 80 90 5 15 30

  29. prev Delete 50 (case 3) 50 20 70 10 40 65 85 80 90 5 15 30

  30. Delete 50 (case 3) 40 20 70 10 30 65 85 80 90 5 15

  31. prev Delete 40 (case 3) 40 20 70 10 30 65 85 80 90 5 15

  32. Delete 40 : Rebalancing 30 20 70 10 Case ? 65 85 80 90 5 15

  33. Delete 40: after rebalancing 30 10 70 20 65 85 5 80 90 15

More Related