Red-Black Trees 1

Download Report

Transcript Red-Black Trees 1

Red-Black Trees
1
Black-Height of the tree = 4
2
Red-Black Trees
• Definition: A red-black tree is a binary search
tree where:
–
–
–
–
Every node is either red or black.
Each NULL pointer is considered to be a black node
If a node is red, then both of its children are black.
Every path from a node to a leaf contains the same
number of black nodes.
• Definition: The black-height of a node, n, in
a red-black tree is the number of black nodes
on any path to a leaf, not counting nulls.
3
A valid Red-Black Tree
Black-Height = 2
4
Theorem 1 – Any red-black tree with root x,
has at least n = 2bh(x) – 1 internal nodes,
where bh(x) is the black height of node x.
Proof: by induction on height of x.
5
Theorem 2 – In a red-black tree, at least half
the nodes on any path from the root to a leaf
must be black.
Proof – If there is a red node on the path, there
must be a corresponding black node.
6
Theorem 3 – In a red-black tree, no path from any
node, N, to a leaf is more than twice as long as any
other path from N to any other leaf.
Proof: By definition, every path from a node to any
leaf contains the same number of black nodes. By
Theorem 2, a least ½ the nodes on any such path
are black. Therefore, there can no more than twice
as many nodes on any path from N to a leaf as on
any other path. Therefore the length of every path
is no more than twice as long as any other path
7
Theorem 4 –
A red-black tree with n internal nodes has
height h <= 2 lg(n + 1).
Proof: Let h be the height of the red-black tree
with root x. By Theorem 2,
bh(x) >= h/2
From Theorem 1, n >= 2bh(x) - 1
Therefore n >= 2 h/2 – 1
n + 1 >= 2h/2
lg(n + 1) >= h/2
2lg(n + 1) >= h
8
Bottom –Up Insertion
• Insert node as usual in BST
• Color the Node RED
• What Red-Black property may be violated?
–
–
–
–
Every node is Red or Black
Leaf nodes are Black NULLS
If node is Red, both children must be Black
Every path from node to descendant leaf must
contain the same number of Blacks
9
Bottom Up Insertion
• Insert node; Color it RED; X is pointer to it
• Cases
0: X is the root -- color it black
1: Both parent and uncle are red -- color parent and uncle
black, color grandparent red, point X to grandparent,
check new situation (recoloring may have created a
problem)
2 (zig-zag): Parent is red, but uncle is black. X and its
parent are opposite type children -- color grandparent
red, color X black, rotate left on parent, rotate right on
grandparent
3 (zig-zig): Parent is red, but uncle is black. X and its
parent are both left or both right children -- color parent
10
black, color grandparent red, rotate right on grandparent
G
P
X
U
G
X
P
U
Case 1 – U is Red
Just Recolor see if there
is a new problem at G
11
Note, that if both kids of G were red, we would have recolored.
If both kids were black, there would be no problem.
G
P
S
U
X
Case 2 – Zig-Zag (X and P are
opposite children)
Double Rotate
X around P; X around G
Recolor G and X as can’t rotate a
black from a common path
without compensating
X
P
G
S
U
12
G
P
X
U
S
Case 3 – Zig-Zig (X and P
are both left kids)
P
X
G
Single Rotate P around G
Recolor P and G as we’ve
rotated a black from
common path
U
S
13
Insert 4 into this
R-B Tree
11
14
2
1
7
5
Black node
15
8
Red node
14
Insertion Practice
Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an
initially empty Red-Black Tree
15
Asymptotic Cost of Insertion
• O(lg n) to descend to insertion point
• O(1) to do insertion
• O(lg n) to ascend and readjust == worst
case only for case 1
• Total: O(log n)
16
Red-Black Trees
Bottom-Up Deletion
17
Recall “ordinary” BST Delete
1. If vertex to be deleted is a leaf, just delete
it.
2. If vertex to be deleted has just one child,
replace it with that child
3. If vertex to be deleted has two children,
replace the value of by it’s in-order
predecessor’s value then delete the inorder predecessor (a recursive step)
18
Bottom-Up Deletion
1. Do ordinary BST deletion. Eventually a
“case 1” or “case 2“ will be done (leaf or
just one child). If deleted node, D, is a
leaf, think of deletion as replacing with the
NULL pointer, V. If D had one child, V,
think of deletion as replacing D with V.
2. What can go wrong??
19
Which RB Property may be
violated after deletion?
1. If D is red?
Not a problem – no RB properties violated
2. If D is black?
If D is not the root, deleting it will change
the black-height along some path
20
Fixing the problem
• Think of V as having an “extra” unit of
blackness. This extra blackness must be
absorbed into the tree (by a red node), or
propagated up to the root and out of the
tree.
• There are four cases – our examples and
“rules” assume that V is a left child. There
are symmetric cases for V as a right child
21
Terminology
• The node just deleted was D
• The node that replaces it is V, which has an extra
unit of blackness (it was black and if deleted, will
create an imbalance)
• The parent of V is P
• The sibling of V is S
Black Node
Red or Black and don’t care
Red Node
22
Bottom-Up Deletion
Case 1
• V’s sibling, S, is Red
– Rotate S around P and recolor S & P
• NOT a terminal case – One of the other
cases will now apply
• All other cases apply when S is Black
23
Case 1 Diagram
P
V+
S
Rotate
P
S
V+
S
Recolor
P
V+
24
Bottom-Up Deletion
Case 2
• V’s sibling, S, is black and has two black
children.
– Recolor S to be Red (subtract 1 black from
each)
– P absorbs V’s extra blackness
• If P is Red, we’re done
• If P is Black, it now has extra blackness and
problem has been propagated up the tree
25
Case 2 diagram
P
V+
Recolor and absorb
S
V
P+
S
Either extra black absorbed by P or
P now has extra blackness
26
Bottom-Up Deletion
Case 3
• S is black
• S’s RIGHT child is RED (Left child either
color)
– Rotate S around P
– Swap colors of S and P, and color S’s Right
child Black
• This is the terminal case – we’re done
27
Case 3 diagrams
P
Rotate
P
S
V+
S
V
S
P
V
Recolor
28
Bottom-Up Deletion
Case 4
• S is Black, S’s right child is Black and S’s
left child is Red
– Rotate S’s left child around S
– Swap color of S and S’s left child
– Now in case 3
29
Case 4 Diagrams
P
V+
P
S
V+
P
Rotate
S
V+
S
Recolor
30
65
50
10
80
70
60
90
62
Perform the following deletions, in the order specified
Delete 90, Delete 80, Delete 70
31
Red Black Trees
Top-Down Insertion
32
Review of Bottom-Up Insertion
• In Bottom-Up insertion, “ordinary” BST
insertion was used, followed by correction
of the tree on the way back up to the root
• This is most easily done recursively
– Insert winds up the recursion on the way down
the tree to the insertion point
– Fixing the tree occurs as the recursion unwinds
33
Top-Down Insertion Strategy
• In Top-Down insertion, the corrections are
done while traversing down the tree to the
insertion point.
• When the actual insertion is done, no
further corrections are needed, so no need to
traverse back up the tree.
• So, Top-Down insertion can be done
iteratively which is generally faster
34
Goal of T-D Insertion
• Insertion is always done as a leaf (as in
ordinary BST insertion)
• Recall from the Bottom-Up discussion that
if the uncle of a newly inserted node is
black, we restore the RB tree properties by
one or two local rotations and recoloring –
we do not need to make changes further up
the tree
35
Goal (2)
• Therefore, the goal of top down insertion is
to traverse from the root to the insertion
point in such a way that RB properties are
maintained, and at the insertion point, the
uncle is Black.
• That way we may have to rotate and recolor,
but not propagate back up the tree
36
Possible insertion configurations
X (Red or Black)
Y
Z
If a new node is inserted as a child of Y or Z, there is no
problem since the new node’s parent is black
37
Possible insertion configurations
X
Y
Z
If new node is child of Z, no problem since Z is black.
If new node is child of Y, no problem since the new node’s
uncle (Z) is black – do a few rotations and recolor…. done
38
Possible insertion configurations
X
Y
Z
If new node is inserted as child of Y or Z, it’s uncle will be
red and we will have to go back up the tree. This is the
only case we need to avoid.
39
Top-Down Traversal
X
Y
Z
As we traverse down the tree and
encounter this case, we recolor and
possible do some rotations.
There are 3 cases.
Remember the goal – to create an insertion point at which the
parent of the new node is Black, or the uncle of the new node
is black.
40
Case 1 – X’s Parent is Black
Y
P
P
X
X
Z
Y
Z
Just recolor and continue down the tree
41
Case 2
• X’s Parent is Red (so Grandparent is Black)
and X and P are both left/right children
– Rotate P around G
– Color P black
– Color G red
• Note that X’s uncle, U, must be black
because it (a) was initially black, or (b)
would have been made black when we
encountered G (which would have had two
red children -- X’s Parent and X’s uncle)
42
Case 2 diagrams
G
P
P
U
G
X
X
Y
S
Z
Z
Y
S
U
Rotate P around G. Recolor X, Y, Z, P and G
43
Case 3
• X’s Parent is Red (so Grandparent is Black)
and X and P are opposite children
– Rotate P around G
– Color P black
– Color G red
• Again note that X’s uncle, U, must be black
because it (a) was initially black, or (b)
would have been made black when we
encountered G (which would have had two
red children -- X’s Parent and X’s uncle)
44
Case 3 Diagrams (1 of 2)
G
G
P
X
U
S
P
X
Y
U
Z
S
Z
Y
Step 1 – recolor X, Y and Z. Rotate X around P.
45
Case 3 Diagrams (2 of 2)
X
G
X
U
P
S
G
P
Z
Y
S
Y
Z
U
Step 2 – Rotate X around G. Recolor X and G
46
An exercise – insert F
D
T
W
L
P
J
E
V
Z
K
47
Top-Down Insert Summary
Case 1
P is Black
Recolor
X,Y,Z
P
Just Recolor
P
X
X
Y
Z
Case 2
P is Red
X & P both left children G
(or both right children)
P
Y
G
X
Z
Z
G
P
P
Y
Y
Z
G
Recolor X,Y,Z
Rotate X
around P
X
Y
P
X
Y
Case 3
P is Red
X and P are
opposite children
Rotate P
around G
Recolor P,G
G
Recolor
X,Y,Z
X
Z
X
P
Y
Z
X
Z
Rotate X
around G
Recolor X, G
G
P
Y
Z
48