Redblack tree properties - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

Redblack tree properties

Description:

Given a new node with a key value, the first step is to determine ... Since a Red-Black Tree is a Binary Search Tree (BST), use the BST Insert procedure to: ... – PowerPoint PPT presentation

Number of Views:39
Avg rating:3.0/5.0
Slides: 29
Provided by: csU72
Category:

less

Transcript and Presenter's Notes

Title: Redblack tree properties


1
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 dont force the root to be
black
2
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 nodes 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
Red-black tree insertion Case 0 New nodes
parent is black.
newly inserted node
No property is violated, so theres nothing to do.
4
Red-black tree insertion Case 1 New nodes
parent and uncle are both red.
This node is now red possibly propagating a r-b
violation upward that must also be fixed
newly inserted node
x
x
(x could also be a right child)
compare w/ Figure 13.5
color flip xs parent, uncle, and grandparent
all change color
5
Red-black tree insertion Case 3 New red nodes
parent is red and uncle is black x is a left
child p(x) is a left child a left/left
situation
a
newly red node (caused by either direct insertion
or violation propagation from below)
See Fig. 13.6
b
a
b
x
B
B
x
  • 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

6
Red-black tree insertion Case 2 New red nodes
parent is red and uncle is blackx 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)
b
b
a
x
a
D
x
D
A
B
C
Note that this rotation grabs tree at x, whereas
Case 3 grabs tree up one level at p(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
7
Red-black tree insertion Case 2 continued
was x now is p(a )
b
now acts as x
b
a
D
a
Now apply Case 3 at
a
8
Other Casesfor 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)

b
a
C
B
x
A
b
a
C
x
9
Example Inserting the sequence 1 9 2 8 3 7 4 6
1
No property is violated, so theres nothing to do.
10
Example Inserting the sequence 1 9 2 8 3 7 4 6
1
1
1
9
9
Insert 9 and flip color of root
11
Example Inserting the sequence 1 9 2 8 3 7 4 6
1
1
9
9
2
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
Example Inserting the sequence 1 9 2 8 3 7 4 6
1
1
2
9
9
2
Apply a left/right Case 2 - Rotate right (grab
2) - Now we have a right/right form of Case 3
13
Example Inserting the sequence 1 9 2 8 3 7 4 6
1
2
2
9
1
9
Applying right/right form of Case 3 - change
color of 1 and 2 - rotate left (grab
2) Red-black property restored!
14
Example Inserting the sequence 1 9 2 8 3 7 4 6
2
2
9
9
1
1
8
8
Red-black property violated by inserting 8. An
application of case 1 (color change) will suffice
here because uncle is red.
15
Example Inserting the sequence 1 9 2 8 3 7 4 6
2
9
1
8
8
A color flip restores the red-black property.
16
Example Inserting the sequence 1 9 2 8 3 7 4 6
Inserting 3 violates the red-black property
17
Example Inserting the sequence 1 9 2 8 3 7 4 6
2
9
1
8
8
This is a left/left form of case 3, so change
colors and rotate right to restore (grab 8).
18
Example Inserting the sequence 1 9 2 8 3 7 4 6
Inserting 7 causes a red/red violation both 7s
parent and uncle are red, so a color flip
suffices
19
Example Inserting the sequence 1 9 2 8 3 7 4 6
2
8
1
The color flip performed 3, 9, and 8 all
flip. This forms a red/red violation above
20
Example Inserting the sequence 1 9 2 8 3 7 4 6
2
8
1
To fix the red/red violation above, recolor the
root to black.
21
Example Inserting the sequence 1 9 2 8 3 7 4 6
Inserting 4 causes a red/red violation this is a
left/right form of Case 2
22
Example Inserting the sequence 1 9 2 8 3 7 4 6
2
8
1
Apply left/right form of Case 2 perform a right
rotation (grab 4) to produce a right/right form
of Case 3
23
Example Inserting the sequence 1 9 2 8 3 7 4 6
2
8
1
8
3
9
4
7
Apply right/right form of Case 3 change colors
and rotate left (grab 4)
24
Example Inserting the sequence 1 9 2 8 3 7 4 6
2
8
1
8
4
9
7
3
Inserting 6 causes a red/red violation which
can be fixed by a color flip (its a case 1
violation)
25
Example Inserting the sequence 1 9 2 8 3 7 4 6
But flipping the colors of 6s parent, uncle, and
grandparent causes another tree violation at 4 ...
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
27
Example Inserting the sequence 1 9 2 8 3 7 4 6
2
4
1
7
7
Now apply right/right Case 3 change colors and
rotate left (grab 4)
28
Inserting 1 9 2 8 3 7 4 6 into a binary search
tree without balancing!
1
1
9
2
8
3
7
4
6
Write a Comment
User Comments (0)
About PowerShow.com