Red -BlackTrees

Red -BlackTrees
Evaliata Br Sembiring
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
“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
Red Black Trees (RBT)
 RBT adalah BST (binary search tree) dimana tiap node memiliki
atribut warna yang bernilai merah atau hitam.
Red-Black Tree Characteristics
 The nodes are colored.
 During insertion and deletion, rules are followed that preserve
various arrangements of these colors.
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.
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.
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).
Contoh : Insert New Nodes
 Memberikan root node dengan nilai 50
 Tambah node baru yang lebih kecil dari root (50),
50
25
75
misal 25
 Tambah node kedua dengan nilai yang lebih besar dari root (50),
misal 75
Contoh : Rotations
 Rotasi ada 2 : Rotasi kanan dan Rotasi Kiri.
 Dimulai dengan 3 node pada contoh sebelumnya
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).
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?
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.
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.
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.
Rotation
Rotation
Next : Insert RBT