1
04-May-04 20 - 1
©2004 PM - IKI10100
Red- Red -Black Trees Black Trees
1. Every node is colored either redor black 2. The root is black
3. If a node is red, its children must be black 4. Every path from a node to a null reference
must contain the same number of black nodes
04-May-04 20 - 2
©2004 PM - IKI10100
Red- Red -Black Trees Black Trees
30 15
5 50
10
70 85 60
40 55
80 90 65
20
04-May-04 20 - 3
©2004 PM - IKI10100
Insertion Insertion
1. A new node must be colored red.
2. Convention: null nodes are black.
04-May-04 20 - 4
©2004 PM - IKI10100
Single Rotation Single Rotation
A B
G
X
S P
C D E A B
X G
S P
C
D E
2
04-May-04 20 - 5
©2004 PM - IKI10100
Double Rotation Double Rotation
B C
G
X
S P
A D E A B
P G
S X
C
D E
04-May-04 20 - 6
©2004 PM - IKI10100
Top Down Red
Top Down Red -Black Tree - Black Tree
Insertion :
z Pada saat mencari lokasi utk node baru, Jika X punya dua anak merah, flip color!
z Selesaikan color property violation dengan single/double rotation!
X C2 C1
X
C1 C2
04-May-04 20 - 7
©2004 PM - IKI10100
Color Flip Color Flip
X C2 C1
X
C1 C2
04-May-04 20 - 8
©2004 PM - IKI10100
Insert 45 Insert 45
30 15
5 50
10
70 85 60
40 55
80 90 65
20 Red-Black Trees
3
04-May-04 20 - 9
©2004 PM - IKI10100
Insert 45: after color flip Insert 45: after color flip
30 15
5 10
70 85 60
50
55
80 90 65
20
40 Red-Black Trees
04-May-04 20 - 10
©2004 PM - IKI10100
45 30 15
5
10 70
80 60
50
55 65 85
20
40
90
Insert 45 done Insert 45 done
Red-Black Trees
04-May-04 20 - 11
©2004 PM - IKI10100
AVL & Red
AVL & Red- -Black Trees Black Trees
http://