Red-Black Slideshow.pdf

The following notation is used in these slides
Splitting a node cases
R0 R1 R2
C0 C1 C2
P P0 P1
T0 T1 T2 T3 T4 T5
Keys in the root being split
Keys in a child node being split
Keys in the parent of a node being split
Subtrees of node being split (can be empty)
Inserting into a leaf cases
E E0 E1
N
Existing keys in a leaf being inserted into
New key being inserted
SPLIT ROOT
BEFORE
2-3-4 Tree
R0R1R2
T0 T1 T2 T3
Red-Black Equivalent
AFTER
2-3-4 Tree
Red-Black Equivalent
SPLIT ROOT
BEFORE
2-3-4 Tree
AFTER
Red-Black Equivalent
R0R1R2
T0 T1 T2 T3
R1
R0
R2
T0 T1
T2 T3
2-3-4 Tree
Red-Black Equivalent
SPLIT ROOT
BEFORE
2-3-4 Tree
AFTER
Red-Black Equivalent
R0R1R2
T0 T1 T2 T3
2-3-4 Tree
Red-Black Equivalent
R1
R1
R0
R2
T0 T1
T2 T3
R0
R2
T0 T1
T2 T3
SPLIT ROOT
BEFORE
2-3-4 Tree
AFTER
Red-Black Equivalent
R0R1R2
T0 T1 T2 T3
2-3-4 Tree
Red-Black Equivalent
R1
R1
R0
R2
T0 T1
T2 T3
R0
T0 T1
R1
R2
R0
T2 T3 T0 T1
R2
T2 T3
SPLIT LEFT CHILD OF 2-NODE
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P
C0C1C2
T0 T1 T2 T3
T4
2-3-4 Tree
Red-Black Equivalent
SPLIT LEFT CHILD OF 2-NODE
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P
C0C1C2
T0 T1 T2 T3
P
C1
T4
T4
C0
C2
T0 T1
T2 T3
2-3-4 Tree
Red-Black Equivalent
SPLIT LEFT CHILD OF 2-NODE
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P
C0C1C2
T0 T1 T2 T3
2-3-4 Tree
C1P
P
C1
T4
Red-Black Equivalent
T4
C0
C2
T0 T1
T2 T3
C0
C2
T0 T1 T2 T3
T4
SPLIT LEFT CHILD OF 2-NODE
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P
2-3-4 Tree
Red-Black Equivalent
C1P
P
P
C1
C0C1C2
T0 T1 T2 T3
C1
T4
T4
C0
C2
T0 T1
T2 T3
C0
C2
T0 T1 T2 T3
T4
C0
C2
T0 T1 T2 T3
T4
SPLIT LEFTMOST CHILD OF 3-NODE - RED ON RIGHT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P0P1
C0C1C2
T0 T1 T2 T3
T 4 T5
2-3-4 Tree
Red-Black Equivalent
SPLIT LEFTMOST CHILD OF 3-NODE - RED ON RIGHT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P0P1
P0
P1
C0C1C2
T0 T1 T2 T3
T 4 T5
C1
T4 T 5
C0
C2
T0 T1
T2 T3
SPLIT LEFTMOST CHILD OF 3-NODE - RED ON RIGHT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P0P1
2-3-4 Tree
C1P0P1
P0
P1
C0C1C2
T 4 T5
C1
C0
T0 T1 T2 T3
T0 T1
T4 T 5
C0
C2 T4 T 5
C2
T2 T3
T0 T1 T2 T3
Red-Black Equivalent
SPLIT LEFTMOST CHILD OF 3-NODE - RED ON RIGHT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P0P1
2-3-4 Tree
Red-Black Equivalent
C1P0P1
P0
P0
P1
C0C1C2
T 4 T5
C1
C0
T0 T1 T2 T3
T0 T1
T4 T 5
C1
C0
C2 T4 T 5
C0
P1
C2 T4 T 5
C2
T2 T3
T0 T1 T2 T3
T0 T1 T2 T3
T0 T1
SPLIT LEFTMOST CHILD OF 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P0P1
C0C1C2
T0 T1 T2 T3
T 4 T5
2-3-4 Tree
Red-Black Equivalent
SPLIT LEFTMOST CHILD OF 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P0P1
P1
P0
C0C1C2
T0 T1 T2 T3
T 4 T5
C1
T4 T 5
C0
C2
T0 T1
T2 T3
2-3-4 Tree
Red-Black Equivalent
SPLIT LEFTMOST CHILD OF 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P0P1
2-3-4 Tree
C1P0P1
P1
P0
C0C1C2
T 4 T5
C1
C0
T0 T1 T2 T3
T0 T1
T4 T 5
C0
C2 T4 T 5
C2
T2 T3
T0 T1 T2 T3
Red-Black Equivalent
SPLIT LEFTMOST CHILD OF 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P0P1
2-3-4 Tree
Red-Black Equivalent
C1P0P1
P1
P0
C1
P0
C0C1C2
T 4 T5
C1
C0
T0 T1 T2 T3
T0 T1
T4 T 5
C0
C2 T4 T 5
C0
P1
C2 T4 T 5
C2
T2 T3
T0 T1 T2 T3
T0 T1 T2 T3
SPLIT MIDDLE CHILD OF 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
P0P1
T0
C0C1C2 T5
T1 T2 T3 T4
Red-Black Equivalent
2-3-4 Tree
Red-Black Equivalent
SPLIT MIDDLE CHILD OF 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P0P1
P1
P0
T0
C0C1C2 T5
T1 T2 T3 T4
T0
C1
T5
C0
C2
T1 T2
T3 T4
2-3-4 Tree
Red-Black Equivalent
SPLIT MIDDLE CHILD OF 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P0P1
2-3-4 Tree
P0C1P1
P1
P0
T0
C0C1C2 T5
T0
C1
C0
T1 T2 T3 T4
T1 T2
T5
T0
C0
C2 T5
C2
T3 T4
T1 T2 T3 T4
Red-Black Equivalent
SPLIT MIDDLE CHILD OF 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
P0P1
2-3-4 Tree
Red-Black Equivalent
P0C1P1
P1
P0
P0
T0
C0C1C2 T5
T0
C1
C0
T1 T2 T3 T4
T1 T2
C1
T5
T0
C0
C2 T5 T0
C0
P1
C2 T5
C2
T3 T4
T1 T2 T3 T4
T1 T2 T3 T4
There are three more cases that arise when splitting a node that
is a child of a 3-Node.
Each is symmetrical (mirror-image) with one of the cases we have
just considered
1) Split right child of a 3-Node whose red is on the left symmetrical with split left child of a 3-Node whose red is on the right.
2) Split right child of a 3-Node whose red is on the right symmetrical with split left child of a 3-Node whose red is on the left.
3) Split middle child of a 3-Node whose red is on the right symmetrical with split middle child of a 3-Node whose red is on
the left.
INSERT NEW LEFT KEY INTO 2-NODE
BEFORE
AFTER
2-3-4 Tree
E
Red-Black Equivalent
2-3-4 Tree
Red-Black Equivalent
INSERT NEW LEFT KEY INTO 2-NODE
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
E
E
2-3-4 Tree
Red-Black Equivalent
INSERT NEW LEFT KEY INTO 2-NODE
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
E
E
2-3-4 Tree
N E
Red-Black Equivalent
INSERT NEW LEFT KEY INTO 2-NODE
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
E
E
2-3-4 Tree
Red-Black Equivalent
N E
E
N
INSERT NEW LEFTMOST KEY INTO 3-NODE - RED ON RIGHT
BEFORE
AFTER
2-3-4 Tree
E0E1
Red-Black Equivalent
2-3-4 Tree
Red-Black Equivalent
INSERT NEW LEFTMOST KEY INTO 3-NODE - RED ON RIGHT
BEFORE
AFTER
2-3-4 Tree
E0E1
Red-Black Equivalent
E0
E1
2-3-4 Tree
Red-Black Equivalent
INSERT NEW LEFTMOST KEY INTO 3-NODE - RED ON RIGHT
BEFORE
AFTER
2-3-4 Tree
E0E1
Red-Black Equivalent
2-3-4 Tree
E0
N E0 E 1
E1
Red-Black Equivalent
INSERT NEW LEFTMOST KEY INTO 3-NODE - RED ON RIGHT
BEFORE
AFTER
2-3-4 Tree
E0E1
Red-Black Equivalent
2-3-4 Tree
E0
N E0 E 1
E1
Red-Black Equivalent
E0
N
E1
INSERT NEW LEFTMOST KEY INTO 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
E0E1
Red-Black Equivalent
2-3-4 Tree
Red-Black Equivalent
INSERT NEW LEFTMOST KEY INTO 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
E0E1
E1
E0
2-3-4 Tree
Red-Black Equivalent
INSERT NEW LEFTMOST KEY INTO 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
2-3-4 Tree
E1
N E0 E 1
E0E1
E0
Red-Black Equivalent
INSERT NEW LEFTMOST KEY INTO 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
2-3-4 Tree
E1
N E0 E 1
E0E1
E0
Red-Black Equivalent
E0
N
E1
INSERT NEW MIDDLE KEY INTO 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
E0E1
Red-Black Equivalent
2-3-4 Tree
Red-Black Equivalent
INSERT NEW MIDDLE KEY INTO 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
E0E1
E1
E0
2-3-4 Tree
Red-Black Equivalent
INSERT NEW MIDDLE KEY INTO 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
2-3-4 Tree
E1
E0 N E1
E0E1
E0
Red-Black Equivalent
INSERT NEW MIDDLE KEY INTO 3-NODE - RED ON LEFT
BEFORE
AFTER
2-3-4 Tree
Red-Black Equivalent
2-3-4 Tree
E1
E0 N E1
E0E1
E0
Red-Black Equivalent
N
E0
E1
There are three more cases that arise when inserting a new key
into a 3-Node.
Each is symmetrical (mirror-image) with one of the cases we have
just considered
1) Insert new rightmost child into a 3-Node whose red is on the left symmetrical with insert new leftmost child into a 3-Node whose red is
on the right.
2) Insert new rightmost child into a 3-Node whose red is on the right symmetrical with insert new leftmost child of a 3-Node whose red is on
the left.
3) Insert new middle child into a 3-Node whose red is on the right symmetrical with insert new middle child into a 3-Node whose red is
on the left.