Download presentation
Presentation is loading. Please wait.
1
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
2 /26 Red-black tree insertion: Case 0: New node’s parent is black. There’s nothing to do. newly inserted node
3
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
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
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
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
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
8 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6
9
9 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Insert 9 and flip color of root
10
10 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Need to apply case 2: rotate at 9, then rotate at 2.
11
11 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Need to apply case 2: rotate at 9 …
12
12 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Need to apply case 2: … then rotate at 2
13
13 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Red-black property restored! 1
14
14 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Red-black property violated by inserting 8. An application of case 1 suffices here 8
15
15 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 A color flip restores the red-black property. 2 8
16
16 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Inserting 3 violates the red-black property 8 8 3
17
17 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 This is case 3: Rotate right at 9 to restore. 8 8 3
18
18 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 This is case 3: Rotate right at 9 and flip colors to restore. 8 3 9
19
19 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 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
20 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 The color flip performed: 3, 9, and 8 all flip. 3 9 7
21
21 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 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
22 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 7 7
23
23 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 7 7 Inserting 6 causes a red-black violation … which can be fixed by a color flip (it’s a case 1 violation)
24
24 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 But flipping the colors of 6’s parent, uncle, and grandparent causes another tree violation at 4... 7
25
25 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 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 /26 Inserting 1 9 2 8 3 7 4 6 into a binary search tree without balancing! 2 8 1 3 6
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.