Transcript PPT

Red-Black Tree Rotations
Red-Black Diagrams
The diagrams which follow show the
rotations and recoloring for each “case” for
Bottom-Up Insertion, Top-Down Insertion
and Bottom-Up Deletion.
Bear in mind that each case has a left-right
symmetrical twin which is not shown here.
Bottom-Up Insertion
Case 0 - X is the root
Color it Black
G
Case 1 – U is Red
U
P
X
Recolor P, G, U
Move X up to G
Move to another case
G
U
P
X
G
Case 2 -Uncle is
black. X and P are
opposite children
U
P
X
S
X
G
S
U
P
U
P
G
P
Recolor X and G
S
Case 3 – Uncle is Black
X and P are both left
(right) children
X
Zig-Zag Rotation
Rotate P around G
Recolor P and G
G
X
S
U
Top Down Insertion
Case 1 – P is Black
P
P
Recolor X, Y and Z
X
Y
Case 2 – P is red
P
and P and X are
both left (right)
X
children
Y
X
P
Y
G
G
U
P
P
Recolor X, Y, Z, P, G
S
U
X
Rotate P around G
Y
G
P
U
S
X
Z
Rotate X
around P
U
P
S
S
U
X
X
Recolor X,Y,Z
Z
Z
G
Y
G
X
S
Z
Y
Case 3 – P is red and
P and X are opposite
children
P
Z
Y
Recolor X, G
Rotate X
around G
G
P
S
Y
Z
U