Download presentation
Presentation is loading. Please wait.
1
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University
2
Outline Red-Black Tree –Bottom-up Insertion –Top-Down Insertion Section 12.2
3
Red-Black Tree: Properties 1.Every node is colored black or red. 2.The root is black. 3.If a node is red, its children must be black. 4.Every path from a node to a NULL pointer must have the same number of black nodes. 5.The NULL pointers are black.
4
Red-Black Tree: Example 30 15 10 5 70 2085 60 5065 4055 8090
5
Red-Black Tree: Properties No path is more than twice as long as any other path. If every path from the root to a NULL node contains B black nodes, the tree contains at least 2 B – 1 black nodes (Proof is by induction on B). The height of a red-black tree is at most 2log(N+1).
6
Red-Black Tree: Bottom-Up Insertion A new node is always inserted as a leaf, because this is a BST. There are two choices for coloring the new node: black or red. If we color the node black, we violate property 4. If we color the node red, we may violate property 3. Which should we choose: black or red?
7
Red-Black Tree: Bottom-Up Insertion A new node is colored red. Why? Because it is easier. We may have to do no work.
8
Red-Black Tree: Three Basic Insertion Operations Color Flip (Color Change) Two Rotations –Single rotation –Double rotation
9
Red-Black Tree: Rotations In terms of pointer manipulation, Red-Black tree rotations are the same as the AVL search tree rotations. Thus, there are two single rotations (right and left) and two double rotations (left right and right left). For implementation purposes, we need only the two single rotations. The rotations are symmetric. Unlike the AVL search tree rotations, the Red- Black tree rotations change node colors.
10
Single Right at G: S is Black G P X S X is inserted and colored red X’s parent’s sibling, S, is black AB CDE
11
Single Right at G: S is Black G P X S AB CDE P XG S DE CAB Before RotationAfter Rotation
12
Single Left at G: S is Black G P X S X is inserted and colored red X’s parent’s sibling, S, is black AB C D E
13
Single Left at G: S is Black G P X S AB C DE P XG S DE C AB Before RotationAfter Rotation
14
Double Rotation (LR) at G: S is Black G P X S A BC DE X is inserted and colored red X’s parent’s sibling, S, is black
15
Double Rotation (LR) at G: S is Black G P X S A BC DE P AB G X C S DE Before RotationAfter Rotation
16
Double Rotation (RL) at G: S is Black RL at G is symmetric to LR at G. We first rotate P right and then rotate G left. The color flips are the same as in LR.
17
Question When S is black, will the single and double rotations re-balance the tree?
18
Answer Yes, when S is black, both the single and double rotations re-balance the tree. Why? Because the number of black nodes on all paths remains the same.
19
Single Right at G: S is Red G P X S X is inserted and colored red X’s parent’s sibling, S, is red AB CDE
20
Single Right at G: S is Red G P X S AB CDE P X AB G C S DE Before RotationAfter Rotation
21
Single Left at G : S is Red Single left at G when S is Red is symmetric to single right at G when S is Red. G goes down, P goes up. The color flips are the same.
22
Double Rotation (LR) at G: S is Red G P X S A BC DE
23
G P X S A BC DE X PG S DE AB C Before RotationAfter Rotation
24
Question When S is red, will the single or double rotations re-balance the tree?
25
Answer When S is red, neither the single nor the double rotations re-balance the tree, because the root of the sub-tree is now red. Property 3 may be violated higher up.
26
Bottom-Up Insertion We insert a leaf, color it red, and then climb up the insertion path until we reach the root or perform a rotation that rebalances the tree. We may have to make two passes: one down and one up the tree.
27
Red-Black Tree: Top-Down Insertion Can we insert, change colors, and rotate in one pass down the tree? Yes, we can. How? We need to guarantee that when we insert a node (leaf), S is never red. Basically, we have to flip colors as we drill down the tree.
28
Top-Down Color Flip When a node X has two red children, X is colored red and its two children are colored black.
29
Top-Down Color Flip X XLXR X XLXR Before Color FlipAfter Color Flip
30
Question Is there anything that can go wrong during the top-down color flip?
31
Answer Yes, the color flip may introduce two consecutive red nodes. Can it introduce more than two consecutive red nodes? No! Because that would mean that property 3 was already violated before the insertion.
32
Question Can we fix property 3 violations?
33
Answer Yes! If a color flip results in two consecutive red nodes, we can apply either single or double rotation, whichever is appropriate. But, since we have ensured that S is black, the rotation will rebalance the tree.
34
Top-Down Insertion: Example 30 15 10 5 70 2085 60 5065 4055 8090 45
35
Top-Down Insertion: Example 30 15 10 5 70 2085 60 5065 4055 8090 45 Flip?
36
Top-Down Insertion: Example 30 15 10 5 70 2085 60 5065 4055 8090 45 Flip? No!
37
Top-Down Insertion: Example 30 15 10 5 70 2085 60 5065 4055 8090 Flip? No! Flip? 45
38
Top-Down Insertion: Example 30 15 10 5 70 2085 60 5065 4055 8090 Flip? No! 45
39
Top-Down Insertion: Example 30 15 10 5 70 2085 60 5065 4055 8090 Flip? No! Flip? 45
40
Top-Down Insertion: Example 30 15 10 5 70 2085 60 5065 4055 8090 Flip? No! 45
41
Top-Down Insertion: Example 30 15 10 5 70 2085 60 5065 4055 8090 Flip? No! Flip? 45
42
Top-Down Insertion: Example 30 15 10 5 70 2085 60 5065 4055 8090 Flip? No! Flip? Yes! 45
43
Top-Down Insertion: Example 30 15 10 5 70 2085 60 5065 4055 8090 2 Reds!!! 45
44
Question Which Rotation and Where?
45
Single Right at 70: 85 is Black 70 60 50 85 AB CDE 60 5070 85 DE CAB Before RotationAfter Rotation
46
Top-Down Insertion: After the Rotation 30 15 10 5 7020 85 60 50 65 4055 8090 45
47
Top-Down Insertion: Example 30 15 10 5 7020 85 60 50 65 4055 8090 Flip? 45
48
Top-Down Insertion: Example 30 15 10 5 7020 85 60 50 65 4055 8090 Flip? No! 45
49
Top-Down Insertion: Example 30 15 10 5 7020 85 60 50 65 4055 8090 45
50
Red-Black Tree: Implementation nullNode is a node used in place of a NULL pointer. nullNode is always colored black. header is a node that is used as a dummy root. The value of the header is -9999. The real root is the right child of the header.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.