CS2420: Lecture 31 - PowerPoint PPT Presentation

1 / 50
About This Presentation
Title:

CS2420: Lecture 31

Description:

No path is more than twice as long as any other path. ... 2 Reds!!! 45. Question. Which Rotation and Where? Single Right at 70: 85 is Black. 70 ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 51
Provided by: Vladimir120
Category:
Tags: cs2420 | lecture | reds

less

Transcript and Presenter's Notes

Title: CS2420: Lecture 31


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
70
10
20
85
60
5
50
65
80
90
40
55
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 2B 1 black nodes (Proof is by induction
    on B).
  • The height of a red-black tree is at most
    2log(N1).

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
S
P
X
C
D
E
X is inserted and colored red Xs parents
sibling, S, is black
A
B
11
Single Right at G S is Black
G
P
S
P
X
G
X
S
C
D
E
C
A
B
D
E
A
B
Before Rotation
After Rotation
12
Single Left at G S is Black
G
P
S
X
A
B
C
E
D
X is inserted and colored red Xs parents
sibling, S, is black
13
Single Left at G S is Black
G
P
P
S
X
G
X
C
S
C
A
B
D
E
D
E
A
B
Before Rotation
After Rotation
14
Double Rotation (LR) at G S is Black
X is inserted and colored red Xs parents
sibling, S, is black
G
S
P
X
A
D
E
B
C
15
Double Rotation (LR) at G S is Black
X
G
P
S
P
G
S
D
E
A
B
C
X
A
D
E
B
C
Before Rotation
After 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
S
P
X
C
D
E
A
B
X is inserted and colored red Xs parents
sibling, S, is red
20
Single Right at G S is Red
P
G
P
S
X
G
S
A
B
C
X
C
D
E
D
E
A
B
Before Rotation
After 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
S
P
X
A
D
E
B
C
23
Double Rotation (LR) at G S is Red
X
G
S
P
G
P
A
B
X
A
D
E
C
S
B
C
D
E
Before Rotation
After 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
X
XL
XR
XL
XR
Before Color Flip
After 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
45
15
70
10
20
85
60
5
50
65
80
90
40
55
35
Top-Down Insertion Example
Flip?
30
45
15
70
10
20
85
60
5
50
65
80
90
40
55
36
Top-Down Insertion Example
Flip? No!
30
45
15
70
10
20
85
60
5
50
65
80
90
40
55
37
Top-Down Insertion Example
Flip? No!
30
Flip?
15
70
45
10
20
85
60
5
50
65
80
90
40
55
38
Top-Down Insertion Example
Flip? No!
30
Flip? No!
15
70
45
10
20
85
60
5
50
65
80
90
40
55
39
Top-Down Insertion Example
Flip? No!
30
Flip? No!
15
70
Flip?
10
20
85
60
45
5
50
65
80
90
40
55
40
Top-Down Insertion Example
Flip? No!
30
Flip? No!
15
70
Flip? No!
10
20
85
60
45
5
50
65
80
90
40
55
41
Top-Down Insertion Example
Flip? No!
30
Flip? No!
15
70
Flip? No!
10
20
85
60
Flip?
5
50
65
80
90
45
40
55
42
Top-Down Insertion Example
Flip? No!
30
Flip? No!
15
70
Flip? No!
10
20
85
60
Flip? Yes!
5
50
65
80
90
45
40
55
43
Top-Down Insertion Example
30
15
70
2 Reds!!!
10
20
85
60
5
50
65
80
90
45
40
55
44
Question
  • Which Rotation and Where?

45
Single Right at 70 85 is Black
70
60
85
60
50
70
50
85
C
D
E
C
A
B
D
E
A
B
Before Rotation
After Rotation
46
Top-Down Insertion After the Rotation
30
15
60
10
70
20
50
5
85
65
40
55
45
80
90
47
Top-Down Insertion Example
30
15
60
10
70
20
50
Flip?
5
85
65
40
55
45
80
90
48
Top-Down Insertion Example
30
15
60
10
70
20
50
Flip? No!
5
85
65
40
55
45
80
90
49
Top-Down Insertion Example
30
15
60
10
70
20
50
5
85
65
40
55
80
90
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.
Write a Comment
User Comments (0)
About PowerShow.com