1 / 83

Binary Search Tree

Binary Search Tree. Oleh : Nur Hayatin, S.ST. Teknik Informatika - Universitas Muhammadiyah Malang (UMM) Tahun Akademik 2010-2011. Sub Topik. Heap Tree Binary Search Tree AVL Tree Red-Black Tree. Heap Tree. Heap Tree. Definisi :

ronna
Download Presentation

Binary Search Tree

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. Binary Search Tree Oleh : Nur Hayatin, S.ST Teknik Informatika - Universitas Muhammadiyah Malang (UMM) Tahun Akademik 2010-2011

  2. Sub Topik • Heap Tree • Binary Search Tree • AVL Tree • Red-Black Tree

  3. Heap Tree

  4. Heap Tree • Definisi : • Heap tree adalah pohon biner yang tiap node nya memenuhi heap property. • Heap Property : • Kondisi 1: Nilai pada node induk harus lebih besar ( > ) atau lebih besar sama dengan( >= ) dari node anak. • Kondisi 2: Nilai pada node induk harus lebih besar ( < ) atau lebih besar sama dengan( <= ) dari node anak.

  5. Contoh Heap Tree 12 12 12 8 3 8 12 8 14 Node 12 memiliki properti heap Node 12 tidak memiliki properti heap Node 12 memiliki properti heap

  6. Penukaran posisi node induk terhadap node anak yang nilainya paling besar sehingga binary tree memenuhi heap property. Proses ini disebut juga sifting up 14 12 8 12 8 14 Tree tidak memenuhi heap property Tree memenuhi heap property SiftUp

  7. Sift Up • Proses sift up ini dilakukan berulang kali hingga : • Node berada di posisi yang tepat dalam arti nilai node tsb masih lebih kecil daripada node induknya, atau • Prosesnya telah sampai pada node root

  8. Jenis Heap Tree • Berdasarkan Heap Property : • Max heap tree • Min heap tree

  9. Max Heap Tree • Nilai node induk (root) lebih besar atau sama dengan nilai subtree. (heap property kondisi 1)

  10. 9 8 7 6 7 2 6 5 1 Contoh max heap tree

  11. Min Heap Tree Nilai dari node induk (root) lebih kecil atau sama dengan nilai dari subtree. (heap property kondisi 2)

  12. 2 4 3 6 7 9 3 8 6 Contoh min heap tree

  13. Representasi Heap Tree • Representasi Heap Tree lebih efisien menggunakan Array.

  14. 9 8 7 6 7 2 6 5 1 Contoh representasi heap tree 9 8 7 6 7 2 6 5 1 0 1 2 3 4 5 6 7 8 9 10

  15. Aturan Penambahan Node • Penambahan node baru di posisi paling kiri pada level terbawah. • Jika level terbawah penuh, tambahkan node pada level baru. • Harus mengikuti aturan heap property. Jika tidak lakukan sift up.

  16. Tambahkan node baru disini Tambahkan node baru disini Contoh Penambahan Node Contoh 1 : penambahan node dari cabang kiri ke kanan Contoh 2 : Penambahan node pada level baru

  17. 1 9 2 3 8 7 4 7 5 6 6 7 2 6 5 1 8 9 Penambahan Node Pada Max Heap Tree

  18. 9 8 7 6 7 2 6 5 1 7 Penambahan Node Baru Complete binary tree with 10 nodes.

  19. 9 Penambahan Node(5) 8 7 6 7 2 6 New element is 5. 5 1 7 5

  20. 9 Penambahan Node(20) 8 7 6 2 6 7 New element is 20. 5 1 7 7

  21. 9 Sifting Up(7) 8 7 6 2 6 New element is 20. 5 1 7 7 7

  22. 9 Sifting Up(8) 7 6 8 2 6 New element is 20. 5 1 7 7 7

  23. 20 Hasil Penambahan Node(20) 9 7 6 8 2 6 New element is 20. 5 1 7 7 7

  24. 20 Penambahan Node(15) ???? 9 7 6 8 2 6 New element is 15. 5 1 7 7 7

  25. 20 Sifting Up(8) 9 7 6 2 6 New element is 15. 8 5 1 7 7 8 7

  26. 20 Sifting Up(9) 7 6 9 2 6 New element is 15. 8 5 1 7 7 8 7

  27. 20 Hasil Penambahan Node(15) 7 15 6 9 2 6 New element is 15. 8 5 1 7 7 8 7

  28. 20 7 15 6 9 2 6 8 5 1 7 7 8 7 Penghapusan Max Node Max element is in the root.

  29. Removed Root 7 15 6 9 2 6 After max element is removed. 8 5 1 7 7 8 7

  30. 15 Sifting Up(15) 7 6 9 2 6 Reinsert 15 into the heap. 8 5 1 7 7 7

  31. 15 Sifting Up(9) 9 7 6 2 6 Reinsert 9 into the heap. 8 5 1 7 7 7

  32. 15 Sifting Up(8) 9 7 6 2 6 8 Reinsert 8 into the heap Max element is 15. 5 1 7 7 7

  33. Penghapusan Max Node 9 7 6 2 6 8 After max element is removed. 5 1 7 7 7

  34. 9 Sifting Up(9) 7 6 2 6 8 5 1 7

  35. 9 Sifting Up(8) 7 8 6 2 6 8 5 1 7

  36. 9 Sifting Up(7) 8 7 6 2 6 7 5 1

  37. 9 Hasil Remove Max Element (15) 8 7 6 2 6 7 5 1 7

  38. 1 3 2 4 5 6 7 11 8 9 7 7 8 10 Latihan input array = [-, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

  39. 1 Max Heap Tree 3 2 4 5 6 7 Start at rightmost array position that has a child. 11 8 9 7 7 8 10

  40. 1 Sifting up(11) 3 2 4 11 6 7 Move to next lower array position. 5 8 9 7 7 8 10

  41. 1 Initializing A Max Heap 3 2 4 11 6 7 5 8 9 7 7 8 10

  42. 1 Initializing A Max Heap 3 2 9 11 6 7 5 8 4 7 7 8 10

  43. 1 Initializing A Max Heap 3 2 9 11 6 7 5 8 4 7 7 8 10

  44. 1 Initializing A Max Heap 7 2 9 11 6 3 5 8 4 7 7 8 10

  45. 1 Initializing A Max Heap 7 2 9 11 6 3 5 8 4 7 7 8 10

  46. 1 Initializing A Max Heap 7 11 9 6 3 5 8 4 7 7 8 10 Find a home for 2.

  47. 1 Initializing A Max Heap 7 11 9 10 6 3 8 4 8 7 7 5 Find a home for 2.

  48. 1 Initializing A Max Heap 7 11 9 10 6 3 8 4 7 7 8 5 2 Done, move to next lower array position.

  49. 1 Initializing A Max Heap 7 11 9 10 6 3 2 8 4 7 7 8 5 Find home for 1.

  50. 11 Initializing A Max Heap 7 9 10 6 3 2 8 4 7 7 8 5 Find home for 1.

More Related