1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two consecutive red nodes -- i.e. the children of a red node must be black Every path from a node, x, to a descendant leaf contains the same number of black nodes -- the “black height” of node x.
2 /26 Red-black tree insertion: Case 0: New node’s parent is black. There’s nothing to do. newly inserted node
3 /26 Red-black tree insertion: Case 1: New node’s parent and uncle are both red. color flip: x’s parent, uncle, and grandparent all change color newly inserted node this edge/node is now red; possibly propagating a r-b violation upward x = (x could also be a right child) compare w/ Figure 14.5 x =
4 /26 Red-black tree insertion: Case 3: New red node’s parent is red and uncle is black ; x is a left child & p(x) is a left child Flip color of p(x) and p(p(x)), then do a right rotation at p(p(x)) newly red node (caused by propagation from below) AB C A BC x = x See Fig. 14.6
5 /26 Red-black tree insertion: Case 2: New red node’s parent is red and uncle is black; x is a right child & p(x) is a left child Double rotation: rotate once at p(x), rotate again at the new p(x) newly red node (caused by propagation from below) x A B D A CD C x B
6 /26 Red-black tree insertion: Case 2a: New red node’s parent is red and uncle is black; x is a right child & p(x) is a left child x = p(x); Rotate left at new x = Applying the code in the box above reduces case 2a to case 3. newly red node (caused by propagation from below) A B D C x = AB D C
7 /26 Red-black tree insertion: Case 2b: New red node’s parent is red and uncle is black; x is a right child & p(x) is a left child x = p(x); Rotate left at new x Case 2b: Same as Case 3 described earlier. newly red node (caused by propagation from below) A B D C x = A BDC
8 /26 Example: Inserting the sequence
9 /26 Example: Inserting the sequence Insert 9 and flip color of root
10 /26 Example: Inserting the sequence Need to apply case 2: rotate at 9, then rotate at 2.
11 /26 Example: Inserting the sequence Need to apply case 2: rotate at 9 …
12 /26 Example: Inserting the sequence Need to apply case 2: … then rotate at 2
13 /26 Example: Inserting the sequence Red-black property restored! 1
14 /26 Example: Inserting the sequence Red-black property violated by inserting 8. An application of case 1 suffices here 8
15 /26 Example: Inserting the sequence A color flip restores the red-black property. 2 8
16 /26 Example: Inserting the sequence Inserting 3 violates the red-black property 8 8 3
17 /26 Example: Inserting the sequence This is case 3: Rotate right at 9 to restore. 8 8 3
18 /26 Example: Inserting the sequence This is case 3: Rotate right at 9 and flip colors to restore. 8 3 9
19 /26 Example: Inserting the sequence Inserting 7 causes a red-black violation; both 7’s parent and uncle are red, so a color flip suffices … 3 9 3 9 7
20 /26 Example: Inserting the sequence The color flip performed: 3, 9, and 8 all flip. 3 9 7
21 /26 Example: Inserting the sequence Inserting 4 causes a red-black violation; perform a right rotation at 7... then a left rotation at (the new position of) 4... 7 4
22 /26 Example: Inserting the sequence 7 7
23 /26 Example: Inserting the sequence 7 7 Inserting 6 causes a red-black violation … which can be fixed by a color flip (it’s a case 1 violation)
24 /26 Example: Inserting the sequence But flipping the colors of 6’s parent, uncle, and grandparent causes another tree violation at 4... 7
25 /26 Example: Inserting the sequence This situation is covered by case 2 and results in a double rotation (two single rotations shown here in combination as one double rotation).
26 /26 Inserting into a binary search tree without balancing! 3 6