Upload presentasi
Presentasi sedang didownload. Silahkan tunggu
Diterbitkan olehKhoirul Kuncoro Telah diubah "10 tahun yang lalu
1
Red -BlackTrees Evaliata Br Sembiring
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
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. item inserted in ascending order
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 75 50 25 75
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? The good news is we fixed the problem of both parent and child being red. The bad news is that now the message says Error: Black heights differ. The path from the root to node 6 has three black nodes in it, while the path from the root to node 75 has only two. Thus Rule 4 is violated.
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 ● You can change the colors of nodes.
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 What’s Rotating?
The term rotation can be a little misleading. The nodes themselves aren’t rotated, it’s the relationship between them that changes. One node is chosen as the “top” of the rotation. If we’re doing a right rotation, this “top” node will move down and to the right, into the position of its right child. Its left child will move up to take its place. Remember that the top node isn’t the “center” of the rotation. If we talk about a car tire, the top node doesn’t correspond to the hubcap, it’s more like the topmost part of the tire tread. ======================================================== Summary rotation: In a rotation, one node is designated the top node. ● A right rotation moves the top node into the position of its right child, and the top node’s left child into its position. ● A left rotation moves the top node into the position of its left child, and the top node’s right child into its position.
19
Rotation
20
Next : Insert RBT
Presentasi serupa
© 2024 SlidePlayer.info Inc.
All rights reserved.