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.
8/3/2007
UMBC CMSC 341 RBTDiagrams
2
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
8/3/2007
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
UMBC CMSC 341 RBTDiagrams
U
3
8/3/2007
UMBC CMSC 341 RBTDiagrams
4
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
Recolor X, Y, Z, P, G
S
U
X
Rotate P around G
Y
G
P
U
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
S
8/3/2007
P
P
Y
Case 3 – P is red and
P and X are opposite
children
P
Z
Y
UMBC CMSC 341 RBTDiagrams
Recolor X, G
Rotate X
around G
G
P
S
Y
Z
U
5