Presentasi sedang didownload. Silahkan tunggu

Presentasi sedang didownload. Silahkan tunggu

Evaliata Br Sembiring Red -BlackTrees. Red Black Trees (RBT) RBT : pohon pencarian biner (a binary search tree) dengan beberapa fitur spesial. Pokok Bahasan:

Presentasi serupa


Presentasi berjudul: "Evaliata Br Sembiring Red -BlackTrees. Red Black Trees (RBT) RBT : pohon pencarian biner (a binary search tree) dengan beberapa fitur spesial. Pokok Bahasan:"— Transcript presentasi:

1 Evaliata Br Sembiring Red -BlackTrees

2 Red Black Trees (RBT) RBT : pohon pencarian biner (a binary search tree) dengan beberapa fitur spesial. Pokok Bahasan: ● How unbalanced trees degrade performance ● The characteristics of red-black trees ● The color rules ● How to use the RBTree Workshop applet (no) ● How to rotate a subtree

3 Overview A red-black tree is a binary search tree with some special features. Ordinary binary search trees, which we explored in Hours 15, “Binary Trees” and 16, “Traversing Binary Trees,” have an unfortunate defect: If data is inserted in a non-random sequence, the tree might become unbalanced, seriously degrading its performance. A red-black tree can fix this by ensuring that the tree remains balanced at all times. In this hour we’ll learn: ● How unbalanced trees degrade performance ● The characteristics of red-black trees ● The color rules ● How to use the RBTree Workshop applet (not) ● How to rotate a subtree In the next hour we’ll see how to use these techniques to insert a new node into a tree while keeping the tree balanced.

4 “Binary Trees” - “Traversing Binary Trees If data is inserted in a non-random sequence, the tree might become unbalanced. Ketika binary search tree menyimpan data yang sudah terurut (ascending/descending), kemudian dilakukan insert data maka kemungkinan tree menjadi tidak seimbang Alasan: setiap item baru lebih besar nilainya daripada yang terakhir, akan dihubungkan ke subtree kanan dari setiap item sebelumnya atau sebaliknya

5 How unbalanced trees degrade performance item inserted in ascending order The nodes arrange themselves in a line with no branches. Because each node is larger than the previously inserted one, every node is a right child, so all the nodes are on one side of the root. The tree is maximally unbalanced. If you inserted items in descending order, every node would be the left child of its parent; the tree would be unbalanced on the other side.

6 Red Black Trees (RBT) RBT adalah BST (binary search tree) dimana tiap node memiliki atribut warna yang bernilai merah atau hitam.

7 Red-Black Tree Characteristics The nodes are colored. During insertion and deletion, rules are followed that preserve various arrangements of these colors.

8 Colored Nodes In a red-black tree, every node is either black or red. These are arbitrary colors; blue and yellow would do just as well. In fact, the whole concept of saying that nodes have “colors” is somewhat arbitrary. Some other analogy could have been used instead: We could say that every node is either heavy or light, or yin or yang. However, colors are convenient labels.

9 Red-Black Rules Every node is either red or black. The root is always black. If a node is red, its children must be black (although the converse isn’t necessarily true). Every path from the root to a leaf, or to a null child, must contain the same number of black nodes.

10 Red-Black Rules Setiap node berwarna merah atau hitam Root selalu berwarna hitam Node yang berwarna merah tidak dapat memiliki anak yang juga berwarna merah. Jika sebuah node berwarna merah dan warna yang hanya mungkin untuk node berikutnya adalah hitam. Jumlah node hitam bersama setiap jalur di red-black tree harus sama, karena parent hitam dan children merah adalah bagian dari node logis yang sama, node merah tidak dihitung sepanjang jalan. (Setiap path dari node yang menuju ke nil harus mengandung nilai yang sama dengan node yang berwarna hitam.) Semua algoritma yang menjaga keseimbangan dalam red-black tree tidak boleh melanggar aturan ini. Ketika semua aturan berlaku, tree tersebut merupakan red-black tree yang valid, dan tinggi tree tersebut tidak dapat lebih pendek dari log (N + 1) tetapi juga tidak lebih tinggi dari 2 * log (N + 1).

11 Contoh : Insert New Nodes Memberikan root node dengan nilai 50 Tambah node baru yang lebih kecil dari root (50), misal 25 Tambah node kedua dengan nilai yang lebih besar dari root (50), misal

12 Contoh : Rotations Rotasi ada 2 : Rotasi kanan dan Rotasi Kiri. Dimulai dengan 3 node pada contoh sebelumnya

13 Contoh : Perform a Color Flip Mulai dengan contoh sebelumnya (1) Menambah node baru dengan nilai 12 Posisi root tetap berwarna merah Kedua child dari root diganti dari merah menjadi hitam. Catatan: Rule2 (root selalu berwarna hitam)  sehingga RBT masih benar. Karena : tidak ada sebuah parent dan child berwarna merah, dan semua path memiliki jumlah node berwarna hitam yang sama(2).

14 Contoh : Create an Unbalanced Tree Menambah node baru dengan nilai 6 Parent and child adalah merah, (pelanggaran Rule3) Rule3 : node red  child black maka node 6  black. Bagaimana membuatnya seimbang?

15 Path to a Null Child “black height is the number of black nodes from a given node to the root” the black height of the root (50) is 1 from the root to 25 is still 1 from the root to 12 is 2, and so on.

16 Duplicate Keys What happens if there’s more than one data item with the same key? This presents a slight problem in red-black trees. It’s important that nodes with the same key are distributed on both sides of other nodes with the same key. That is, if keys arrive in the order 50, 50, 50, you want the second 50 to go to the right of the first one, and the third 50 to go to the left of the first one. Otherwise, the tree becomes unbalanced. This could be handled by some kind of randomizing process in the insertion algorithm. However, the search process then becomes more complicated if all items with the same key must be found. It’s simpler to outlaw items with the same key. In this discussion we’ll assume duplicates aren’t allowed.

17 The Actions What actions can you take if one of the red-black rules is broken? There are two, and only two, possibilities: ● You can change the colors of nodes. ● You can perform rotations. Changing the color of a node means changing its red-black border color (not the center color). A rotation is a rearrangement of the nodes that hopefully leaves the tree more balanced.

18 Rotation

19

20 Next : Insert RBT


Download ppt "Evaliata Br Sembiring Red -BlackTrees. Red Black Trees (RBT) RBT : pohon pencarian biner (a binary search tree) dengan beberapa fitur spesial. Pokok Bahasan:"

Presentasi serupa


Iklan oleh Google