Red-Black Trees
DEFINISI
Red-Black Trees adalah Binary Search Tree dengan
menyimpan data tambahan yaitu warna (Merah atau
Hitam).
Syarat yang harus dipenuhi oleh sebuah Red-Black Trees
antara lain:
1.
2.
3.
4.
5.
Setiap Node harus mempunyai warna Merah atau hitam
Root berwarna hitam
Setiap nill node diberi warna hitam
Jika sebuah node merah maka kedua anaknya harus hitam
Tiap node, pada seluruh path dari node tersebut ke
descendant leaves mengandung jumlah node hitam yang
sama
PRESENTASI
Red Black Trees dapat dipresentasikan dengan
menggunakan link list
typedef enum {red,black} color;
typedef struct red_black *red_black_ptr
typedef struct red_black {
element data;
red_black_ptr l_child;
red_black_ptr r_child;
color l_color;
color r_color;
}
PRESENTASI
typedef enum {red,black} color;
typedef struct red_black *red_black_ptr
typedef struct red_black {
element data;
color node_color
red_black_ptr l_child;
red_black_ptr r_child;
}
PRESENTASI
26
41
17
16
10
7
3
12
15
19
23
20
47
30
21
14
38
28
35
39
INSERT
Proses insert pada Red-Black Trees sama seperti
proses insert pada Binary Search Trees:
1. Cari posisi dari node baru dimulai dari root
2. Berikan warna merah kepada node tersebut.
3. Periksa apakah setelah dilakukan insert tree
tersebut masih RB Trees. Jika tidak perbaiki
menjadi RB Trees (sesuai dengan syarat pada
slide awal)
Insert
Case 1
Jika Uncle (Y) berwarna merah
X->parent->color=black;
Y->color=black;
X->parent->parent->color=red;
X=X->parent->parent;
Insert
Case 2
Jika uncle(y) black node x adalah right child
Parent left child
X=X->parent;
Leftrotate(x)
Continue to Case 3
Insert
Case 3
Jika uncle(y) black node x adalah left child
dari parent yang merupakan left child
X->parent->color=black;
X->parent->parent->color=red
X=X->parent->parent
Rightrotate(x)
Insert
Case 4
Jika uncle(y) black node x adalah left child
dari Parent yang merupakan right child
X=X->parent;
Rightrotate(x)
Continue to Case 5
Insert
Case 5
Jika uncle(y) black node x adalah right child
dari parent yang merupakan right child
X->parent->color=black;
X->parent->parent->color=red
X=X->parent->parent
Leftrotate(x)
INSERT
Rotasi terdiri dari 2 macam
• Left Rotation (Rotasi kiri)
• Right Rotation (Rotasi kanan)
Left Rotation
X
X
Right Rotation
1
Y
2
3
T
3
1
2
Implementation
rb_insert( Tree T, node x )
{ /* Insert in the tree in the usual way */
tree_insert( T, x );
/* Now restore the red-black property */
x->colour = red;
while ( (x != T->root) && (x->parent->colour == red) ) {
if ( x->parent == x->parent->parent->left ) {
/* If x's parent is a left, y is x's right 'uncle' */
y = x->parent->parent->right;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
}
else {
/* y is a black node */
if ( x == x->parent->right ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
left_rotate( T, x );
}
/* case 3 */
x->parent->colour = black;
x->parent->parent->colour = red;
x=x->parent->parent
right_rotate( T, x );
}
}
Implementation (2)
else {
y = x->parent->parent->left;
if ( y->colour == red ) {
/* case 1 - change the colours */
x->parent->colour = black;
y->colour = black;
x->parent->parent->colour = red;
/* Move x up the tree */
x = x->parent->parent;
}
else {
/* y is a black node */
if ( x == x->parent->left ) {
/* and x is to the right */
/* case 2 - move x up and rotate */
x = x->parent;
right_rotate( T, x );
}
/* case 3 */
x->parent->colour = black;
x->parent->parent->colour = red;
x=x->parent->parent
left_rotate( T, x);
}
}
}
}
/* Colour the root black */
T->root->colour = black; }
INSERT
Insert(1)
1
Insert(2)
1
Colour root is black
No repair needed
1
2
Insert(3)
1
1
2
2
Left rotation
3
Insert(4)
2
1
1
2
3
4
3
3
Parent is redrebalance required
Case 5:parent to right
of grandparent
2
1
3
Colour Root
is black
4
2
1
3
4
INSERT
Insert(5)
2
1
2
Left rotation
1
3
2
1
3
4
4
2
1
4
3
5
3
2
Parent is redrebalance required
Case 5:parent to right
of grandparent
4
1
3
5
6
Insert(7)
2
3
6
Left rotation
4
1
5
5
5
Insert(6)
4
2
4
1
3
5
2
5
3
6
6
7
4
1
6
5
7
7
INSERT
Insert(8)
2
4
1
3
6
5
Parent is redrebalance required
Case 5:parent to right
of grandparent
7
2
1
6
3
5
7
8
4
1
3
Left rotation
2
1
4
3
6
5
8
4
2
7
6
5
8
7
8
Delete
• Proses delete pada RBT sama dengan proses delete
pada BST
• Jika yang didelete adalah node dengan warna merah
maka tree tersebut masih tetap RBT. Jika node yang
didelete memiliki 1 anak berwarna merah maka ganti
warna anak tersebut menjadi hitam.
• Jika proses delete tersebut tidak menghasilkan sebuah
RBT maka perbaiki (Fixup). Hal ini terjadi karena proses
delete dilakukan terhadap node yang berwarna hitam.
Catatan: proses perpindahan hanya melakukan
perpindahan data (key) tidak warna
Delete
Node yang akan didelete
X
X
T2
T2
T2
T1
T2
T1
X
X
T3
T3
Delete Fixup
•
•
Case 1
Precondition:
– X adalah left child dan black
– Sibling dari x adalah red (parent black)
•
To do:
– Parent dari x ubah menjadi red
– brother dari x ubah menjadi black
– Left Rotate
Delete Fixup
Case 1
B
D
X
X
A
D
C
B
E
A
E
C
Delete Fixup
•
Case 2
– Precondition:
•
•
•
X adalah left child dan black
Sibling dari x adalah black
Kedua anak dari Brother x black
– To do:
•
•
Sibling dari x ubah menjadi red
Pindahkan x ke parent dari x
Delete Fixup
Case 2
New X
B
X
B
X
A
D
C
A
E
D
C
E
Delete Fixup
•
Case 3
– Precondition:
•
•
•
•
X adalah left child dan black
Sibling dari x adalah black
Right child dari brother x adalah black
Left child dari brother x adalah red
– To do:
•
•
•
Sibling dari x ubah menjadi red
Left child dari brother x ubah menjadi black
Right rotate dari brother
Delete Fixup
Case 3
New X
B
X
B
X
A
D
C
A
C
D
E
T
T
E
Delete Fixup
•
Case 4
– Precondition:
•
•
•
X adalah left child dan black
Sibling dari x adalah black
Right child dari brother x adalah red
– To do:
•
•
•
•
Ubah Sibling x menjadi warna dari parent x
Ubah warna parent x menjadi black
Right child dari Sibling x ubah menjadi black
Left rotate dari parent x dan stop
Delete Fixup
Case 4
B
D
X
A
X
D
C
E
T
A
B
E
C
Delete Fixup
delete(rbTree T, node n)
node child, cut;
if isNill(del.left) or isNill(del.right) then
cut = n;
else
cut = successor(n);
end if
if not isNill(cut.left) then
child = cut.left;
else
child cut.right;
end if
child.parent = cut.parent;
if isNill(cut.parent) then
T.root = child;
else
if cut.parent.left = cut then
cut.parent.left = child;
else
cut.parent.right = child;
end if
end if
if cut <> n then
n.values = cut.values;
end if
if cut.color = BLACK then
rbDeleteFixUp(T, child);
end if
return cut;
end
Delete Fixup
rbDeleteFixUp(rbTree T, node n)
while n <> T.root and n.color = BLACK do
//Fix either case, returns a node that if red we can color black.
if n = n.parent.left then
n = leftCase(T, n);
else
n = rightCase(T, n);
end if
end while
n.color = BLACK;
end
Delete Fixup
node rightCase(rbTree T, node n)
node sibling;
sibling = n.parent.left;
//CASE 1: rotate right to convert into case 2, 3 or 4
if sibling.color = RED then
sibling.color = BLACK;
n.parent.color = RED;
rotateRight(T, n.parent);
sibling = n.parent.left;
end if
//CASE 2: No rotation needed, simply recolor and move up the tree.
if sibling.left.color = BLACK and sibling.right.color = BLACK then
sibling.color = RED;
n = n.parent;
else
//CASE: 3: rotate left to turn this into case 4
if sibling.left.color = BLACK then
sibling.right.color = BLACK;
sibling.color = RED;
rotateLeft(T, sibling);
sibling = n.parent.left;
end if
//CASE 4: We have to rotate right
sibling.color = n.parent.color;
n.parent.color = BLACK;
sibling.left.color = BLACK;
rightRotate(T, n.parent);
n = T.root // to terminate the while loop
end if
return n;
end
© Copyright 2026 Paperzz