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. In these slides we don’t force the root to be black
2
2 /26 Insertion: The First Step Given a new node with a key value, the first step is to determine where to insert the new node. Since a Red-Black Tree is a Binary Search Tree (BST), use the BST Insert procedure to: –determine the location where the new node’s key value belongs in the Red-Black Tree. –insert the new node at that location. Color the new node red. Use the cases described in the following slides to restore any Red-Black Tree properties that are violated by the new node.
3
3 /26 Red-black tree insertion: Case 0: New node’s parent is black. No property is violated, so there’s nothing to do. newly inserted node
4
4 /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 node is now red; possibly propagating a r-b violation upward that must also be fixed… (x could also be a right child) compare w/ Figure 14.5 x x
5
5 /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 [a left/left situation] Change color : - p(x) to black - p(p(x)) to red Rotate right: grab p(x) and shake; p(p(x)) drops down to the right; right subtree of p(x) slides down to become a left subtree newly red node (caused by either direct insertion or violation propagation from below) A x = x See Fig. 14.6 B CA B C
6
6 /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 [a right/left situation] Reduce to Case 3! newly red node (caused by propagation from below) A B D C x AB D C x Rotate left: grab x and shake; p(x) drops down to the left; left subtree of x slides down to become a right subtree Note that this rotation grabs tree at x, whereas Case 3 grabs tree up one level at p(x).
7
7 /26 Red-black tree insertion: Case 2: continued Now apply Case 3 at A BDC AB D C now acts as x was x; now is p( )
8
8 /26 Other Cases for red x, red parent, black uncle If x is right child and p(x) is right child –Change colors: p(x) to black; p(p(x)) to red –Rotate left (a right/right form of Case 3) If x is left child and p(x) is right child –Rotate right –Apply case above (a left/right form of Case 2) A x B C A x B C
9
9 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 No property is violated, so there’s nothing to do.
10
10 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Insert 9 and flip color of root
11
11 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Insert 2 as left child of 9. This causes a red/red violation! Plan: Apply a left/right Case 2: - Rotate right (grab 2) - Apply right/right form of Case 3
12
12 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Apply a left/right Case 2: - Rotate right (grab 2) - Now we have a right/right form of Case 3
13
13 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Applying right/right form of Case 3 - change color of 1 and 2 - rotate left (grab 2) 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 (color change) will suffice here because uncle is red. 8 1
15
15 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 A color flip restores the red-black property. 8 8
16
16 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Inserting 3 violates the red-black property 8 3 8
17
17 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 This is a left/left form of case 3, so change colors and rotate right to restore (grab 8). 3 8 3 9
18
18 /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 7 3 9
19
19 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 The color flip performed: 3, 9, and 8 all flip. This forms a red/red/ violation above… 7 3 9 7 3 3
20
20 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 To fix the red/red violation above, recolor the root to black. 7 3 3 7 3 3
21
21 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Inserting 4 causes a red-black violation; this is a left/right form of Case 2 7 3 3 7 3 3 4
22
22 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Apply left/right form of Case 2; perform a right rotation (grab 4) to produce a right/right form of Case 3 7 3 3 4 4
23
23 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 4 Apply right/right form of Case 3; change colors and rotate left (grab 4) 7
24
24 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Inserting 6 causes a red-black violation … which can be fixed by a color flip (it’s a case 1 violation) 7 7
25
25 /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 7
26
26 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 This is left/right Case 2: first grab at 4 and rotate right; this reduces the situation to right/right Case 3 7 7
27
27 /26 Example: Inserting the sequence 1 9 2 8 3 7 4 6 Now apply right/right Case 3: change colors and rotate left (grab 4) 7
28
28 /26 Inserting 1 9 2 8 3 7 4 6 into a binary search tree without balancing! 2 8 1 3 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.