Professional Documents
Culture Documents
a!
[Brinton,Rosenfeld,Ozbirn]!
a!
a b!
or!
b!
a!
b c!
a!
c!
Red-Black Tree!
Red-black trees are widely used:!
The children of
red nodes are
both black!
L!
E!
F!
T!
PARENT!
KEY!
COLOR!
R!
I!
G!
H!
T!
[Brinton,Rosenfeld,Sedgewick,Walter]!
A Red-Black Tree!
The root is black!
30
70
15
10
60
20
50
5
40
85
65
55
80
90
Black-height bh(x)!
Black-height of node x is the number of black nodes
on the path from x to an external node (including the
external node but not counting x itself)!
Every node has a"
black-height, bh(x)!
[McCollam]!
2 18
2 20
1 17
1 22
1 19
1 21 1 25
0 0
[Walter]!
Black-height Rule!
bh =!
30
20
bh =!
bh =!
40
80
18
85
65
20
70
bh =!
60
50
bh =!
15
10
90
55
bh =!
17
19
22
21
[McCollam]!
25
a sequence!
[Scott,Ozbirn]!
[Walter,Brinton,Singh]!
h!
h!
Its the equivalent 2-3-4 tree to the red-black tree!!
Height of the resulting tree is h " h/2!
[McCollam,Singh]!
[McCollam,Singh]!
[Luebke]!
[Luebke]!
[Luebke,Walter]!
Red-Black Insert!
1.! as with BST, insert new node as leaf, must be red!
Insertion and deletion must maintain rules of redblack trees and are therefore more complex: still
O(log2 n) time but a bit slower empirically than in
ordinary BST!
[Kellih,Walter]!
[Brinton]!
a!
c!
b!
3-Node, Red-Parent!
1.!
b!
a!
b!
c!
w!
c!
w!
2.!
a!
[Ozbirn]!
R!
c!
w!
a!
w!
L!
b!
L!
R!
b!
R!
L!
c!
a!
c!
b!
v!
[Ozbirn]!
c!
a!
Insert 2!
5
v!
R!
3-node!
b!
12
Single rotation!
Double rotation!
Double rotation!
b!
a!
w!
4-node!
2
c!
Representing a
4-node,!
parent is black,!
children are red!
12
v!
[Ozbirn]!
[Brinton]!
Insert 14!
5
5
12
12
12
R
12
single left !
rotation!
5
14
14
10
R
12
double
rightleft !
rotation!
12
10
[Brinton]!
Red-Black Insert!
[Brinton]!
b!
c!
a!
d!
d!
d!
b!
c!
a!
d!
d!
d!
d!
[Ozbirn]!
20!
40!
50!
60!
55!
[Brinton]!
Four cases:!
!RR: requiring a single left rotation, e.g.,!
L!
R!
L!
R!
P!
P!
X!
X!
A!
B!
G!
Equivalent
2-3-4 tree:!
L!
D
R!
P
B
10
P
P
L!
C
D
R!
G
B!
A!
10!
D!
C!
X
G
[Rosenfeld]!
Equivalent
2-3-4 tree:!
10 85!
Insert 15!
10
10
85
85
15
[Rosenfeld]!
double red!!
[Rosenfeld]!
Equivalent
2-3-4 tree:!
10 15 85!
15
15
10
85
10
85
double red!!
70
[Rosenfeld]!
Equivalent
2-3-4 tree:!
[Rosenfeld]!
15!
10!
70 85!
15
15
10
85
10
85
70
70
20
[Rosenfeld]!
double red!!
[Rosenfeld]!
Equivalent
2-3-4 tree:!
15!
10!
20 70 85!
15
15
10
70
10
70
85
20
85
20
double red!!
60
[Rosenfeld]!
Equivalent
2-3-4 tree:!
[Rosenfeld]!
15 70!
10!
20 60!
85!
15
15
10
70
10
85
20
70
85
20
60
60
30
[Rosenfeld]!
[Rosenfeld]!
Equivalent
2-3-4 tree:!
15 70!
10!
20 30 60!
85!
15
15
10
70
70
85
30
20
10
85
30
60
20
60
double red!!
50
[Rosenfeld]!
[Rosenfeld]!
85
50
50
[Rosenfeld]!
[Rosenfeld]!
Equivalent
2-3-4 tree:!
RBT Removal!
15 30 70!
10!
20!
85!
50 60!
30
15
10
70
20
60
85
!Black?!
! could violate any of root rule, red rule, or black-height rule!
50
Demo: http://gauss.ececs.uc.edu/RedBlack/redblack.html!
[Rosenfeld]!
Rules:!
1.!if node to be deleted is a red leaf, remove leaf, done!
2.!if it is a single-child parent, it must be black (why?);"
replace with its child (must be red) and recolor child black!
3.!if it has two internal node children, swap node to be
deleted with its in-order successor!
! if in-order successor is red (must be a leaf, why?), remove leaf, done!
! if in-order successor is a single child parent, apply second rule!
[Walter]!
10
40
45
30
35
20
60
25
[Singh]!
Red Sibling!
Black-Leaf Removal!
We want to remove v, which is a black leaf!
Replace v with external node u, color u double black!
color"
undetermined!
[!altenis]!
u
u
u
[!altenis]!
[!altenis]!
Remove 8:!
[!altenis]!
[!altenis]!
[!altenis]!
[Kellih]!