Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Teknik Informatika - Universitas Muhammadiyah Malang (UMM)

Presentasi serupa


Presentasi berjudul: "Teknik Informatika - Universitas Muhammadiyah Malang (UMM)"— Transcript presentasi:

1 Teknik Informatika - Universitas Muhammadiyah Malang (UMM)
Binary Search Tree Oleh : Nur Hayatin, S.ST Teknik Informatika - Universitas Muhammadiyah Malang (UMM) Tahun Akademik

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

3 Heap Tree

4 Heap Tree Definisi : Heap Property :
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 Sift Up Penukaran posisi node induk terhadap node anak yang nilainya paling besar sehingga binary tree memenuhi heap property. 12 8 14 Tree tidak memenuhi heap property 14 8 12 Tree memenuhi heap property Proses ini disebut juga sifting up

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 Contoh max heap tree 9 8 6 7 2 5 1

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

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

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

14 Contoh representasi heap tree
9 8 6 7 2 5 1 9 8 7 6 7 2 6 5 1 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 Contoh Penambahan Node
Tambahkan node baru disini Tambahkan node baru disini Contoh 1 : penambahan node dari cabang kiri ke kanan Contoh 2 : Penambahan node pada level baru

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

53 Hasil Akhir 11 10 7 9 5 6 3 8 4 7 7 2 1 8 Done.

54 Binary Search Tree (BST)

55 Definisi Sebuah binary tree dimana subtree sebelah kiri lebih kecil dari subtree sebelah kanan.

56 Contoh BST 20 10 6 2 8 15 40 30 25

57 Binary Search Tree Operasi BST : penambahan, penghapusan, pencarian node tertentu, pencarian niai terkecil dan pencarian nilai terbesar. Properti Binary Search Tree : Untuk setiap node X, semua elemen di subpohon kirinya bernilai lebih kecil dari nilai X dan semua elemen di subpohon kanannya bernilai lebih besar dari nilai X.

58 Contoh Binary Search Tree Bukan Binary Search Tree

59 Insert Time complexity = O(height of the tree)
Dimulai dengan penelusuran dari root untuk mencari posisi yang tepat. Jika elemen X ditemukan (berarti X sudah ada di BST), maka tidak perlu melakukan aksi apapun. Jika tidak, maka letakkan X sebagai node terakhir pada jalur penelusuran Time complexity = O(height of the tree)

60 Delete Saat akan menghapus sebuah node, kita juga harus memikirkan seluruh node anak dari node tsb. Hal penting adalah agar pohon setelah dihapus tetap merupakan BST.

61 Operasi Penghapusan / remove()
Ada 3 kasus : Elemen ada di leaf/daun. Elemen yang memiliki degree 1. Elemen yang memiliki degree 2.

62 Penghapusan Node Daun (Node 7)
20 10 6 2 8 15 40 30 25 35 7 18 Remove a leaf element. key = 7

63 Penhapusan Node Daun (Node 35)
20 10 6 2 8 15 40 30 25 35 7 18 Remove a leaf element. key = 35

64 Penghapusan Node Ber-degree 1
20 10 6 2 8 15 40 30 25 35 7 18 Remove from a degree 1 node. key = 40

65 Penghapusan Node Ber-degree 1
20 10 6 2 8 15 40 30 25 35 7 18 Remove from a degree 1 node. key = 15

66 Penghapusan Node Ber-degree 2
20 10 6 2 8 15 40 30 25 35 7 18 Remove from a degree 2 node. key = 10

67 Remove From A Degree 2 Node
20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

68 Penghapusan Node Ber-degree 2
20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

69 Penghapusan Node Ber-degree 2
20 8 40 6 15 30 18 25 35 2 8 7 Replace with largest key in left subtree (or smallest in right subtree).

70 Latihan Remove from a degree 2 node. key = 20 20 10 6 2 8 15 40 30 25
35 7 18 Remove from a degree 2 node. key = 20

71 Penghapusan Node Ber-degree 2
20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.

72 Penghapusan Node Ber-degree 2
20 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.

73 Penghapusan Node Ber-degree 2
18 10 40 6 15 30 18 25 35 2 8 7 Replace with largest in left subtree.

74 Hasil Akhir 18 10 40 6 15 30 25 35 2 8 7

75 Pencarian pada BST Jika mencari elemen bernilai 15, maka akan langsung ditemukan. Jika mencari elemen bernilai < 15, maka kita cari di subpohon kiri. Jika mencari elemen bernilai > 15, maka kita cari di subpohon kanan.

76

77 findMin/ findMax findMin : mengembalikan node dengan elemen terkecil pada BST Pencarian dimulai dari root dan bergerak ke kiri terus sepanjang subpohon kiri dan berhenti pada elemen terakhir. Proses serupa terhadap metode findMax

78 Red-Black Tree

79 Red-Black Tree Red-black tree adalah binary search tree yang memenuhi properti sbb : Setiap node adalah red atau black Node root adalah black Setiap daun adalah black Semua anak dari node red adalah black Setiap jalur (path) dari sebuah node ke daun berisi node black yang jumlahnya sama Struktur Data - Tree

80 Contoh Red-Black Tree Struktur Data - Tree

81 AVL Tree

82 AVL Tree AVL Properti : Sebuah node memiliki AVL property jika height (tinggi) subpohon kiri & subpohon kanan node tsb sama atau berbeda 1. Height (tinggi) pohon adalah jarak dari root menuju daun terbawah yang dimiliki pohon tsb AVL Tree adalah pohon yang setiap node nya memiliki AVL property

83 Pustaka Sartaj Sahni , “Data Structures & Algorithms”, Presentation L1. Mitchell Waite, “Data Structures & Algorithms in Java”, SAMS, 2001


Download ppt "Teknik Informatika - Universitas Muhammadiyah Malang (UMM)"

Presentasi serupa


Iklan oleh Google