Balanced Search Trees: Red

advertisement
10/23/2013
Definitions
Insertion
In a nutshell
Balanced Search Trees:
Red-Black Trees
Definition: an extended Node
Binary Search Trees
Before, all our nodes were born equals.
In a red-black tree, a node is either red or black.
• Average case and worst case Big O for
For some algorithms, it is easier to have a pointer to the parent.
– insertion
– deletion
– find
• Balance is important. Unbalanced trees give
worse than log N times for the basic tree
operations
• Can balance be guaranteed?
public class RBNode extends Node{
private Node parent;
private boolean color;
// true for black, false for red
right
parent
public RBNode(int key, T data) {
super(key, data);
parent = null;
color = true;
}
left
parent
}
Definition: Red-Black Tree
• Each node must have exactly two children. For each child
that is lacking, you create a fake black one.
Definition: Red-Black Tree
• Each node must have exactly two children. For each child
that is lacking, you create a fake black one.
10
13
5
needs two fake
children →
7
13
6
Think of those as ghostly
nodes: they are not really
there…
10
← needs one fake child
9
← needs two fake children
13
5
← needs two fake children
In practice, don’t
bother drawing them.
7
13
6
9
# of ghosts = # of nodes + 1
1
10/23/2013
Definition: Red-Black Tree
Example
The root is black.
• Each node must have exactly two children. For each child
that is lacking, you create a fake black one.
The children of red
nodes are both black.
• The root is black.
• Every path from a node to a leaf contains the same number
of black nodes.
• If a node is red then both its children must be black.
Example
The root is black.
The children of red
nodes are both black.
Maintaining the Red Black
Properties in a Tree
• Insertions
• Must maintain rules of Red Black Tree.
• New Node always a leaf
– can't be black or we will violate rule of the same # of
blacks along any path
– therefore the new leaf must be red
– If parent is black, done (trivial case)
– if parent red, things get interesting because a red leaf
with a red parent violates no double red rule.
Algorithm: Insertion
A red-black tree is a particular binary search tree, so create a
new node as red and insert it as in normal search tree.
5
7
Algorithm: Insertion
We have detected a need for balance when X is red and its parent, too.
• If X has a red uncle: colour the parent and uncle black, and grandparent red.
Then replace X by grandparent to see if X’s parent is red.
9
7
Violation!
What property may be violated? The children of a red node
must be black.
X
2
10/23/2013
Algorithm: Insertion
rotateRight(G)
We have detected a need for balance when X is red and his parent too.
G
• If X has a red uncle: colour the parent and uncle black, and grandparent red.
Then replace X by grandparent to see if X’s parent is red.
• If X is a left child and has a black uncle: colour the parent black and the
grandparent red, then rotateRight(X.parent.parent)
S
P
A
D
C
X
E
B
Relative to G, X is at left left positions.
rotateRight(G) will exchange of roles between G
and P, so P becomes G's parent. Also must
recolor P and G.
Algorithm: Insertion
After rotateRight(G)
We have detected a need for balance when X is the left child of a red parent.
P
A
• If X has a red uncle: colour the parent and uncle black, and grandparent red.
Then replace X by grandparent to see if X’s parent is red.
• If X is a right
childand
andhas
hasa ablack
blackuncle:
uncle,colour
then rotateLeft(X.parent)
left child
the parent black andand
the
grandparent red, then rotateRight(X.parent.parent)
G
X
B
C
S
D
E
Apparent rule violation?
rotateLeft(G) will handle the case when X is at right
right position relative to G.
15
Algorithm: Insertion
We have detected a need for balance when X is the left child of a red parent.
• If X has a red uncle: colour the parent and uncle black, and grandparent red.
Then replace X by grandparent to see if X’s parent is red.
• If X is a right child and has a black uncle, then rotateLeft(X.parent) and
• If X is a left child and has a black uncle: colour the parent black and the
grandparent red, then rotateRight(X.parent.parent)
Double Rotation
What if X is at left right relative to G?
– a single rotation will not work
Must perform a double rotation
– rotate X and P
– rotate X and G
G
P
A
S
D
X
B
E
C
3
10/23/2013
Example of Inserting Sorted Numbers
G
A
D
C
P
1 2 3 4 5 6 7 8 9 10
S
X
E
B
Insert 1. A leaf so
red. Realize it is
root so recolor
to black.
After Double
Rotation
1
G
1
X
P
A
C
B
S
Double rotation is also needed when X is
at right left position relative to G.
D
E
Insert 2
make 2 red. Parent
is black so done.
Insert 3
1
1
Insert 3. Parent is red.
Parent's sibling is black
(null) 3 is outside relative
to grandparent. Rotate
parent and grandparent of 3
2
2
3
2
3
1
Insert 4
Insert 5
2
When adding 4,
its uncle is red, so
color both uncle
and parent black
and grandparent
red. Since 2 is the
root, color it black.
2
3
1
4
5's parent is red.
Its uncle is black (null).
5 is right-right relative to
grandparent (3) so rotate
parent and grandparent then
recolor.
4
5
2
1
3
1
2
3
4
1
4
3
5
4
10/23/2013
Insert 6
When adding 6,
its uncle is red, so
color both uncle
and parent black
and grandparent (4)
red.
Insert 7
7's uncle is
black (null). 7 is
right-right relative to
grandparent (5) so
1
rotate parent and
grandparent then recolor
2
4
1
3
2
4
3
5
5
2
2
6
6
4
1
4
1
3
5
3
6
6
Still Inserting 8
2
4
1
3
6
7
5
8
When adding 8,
its uncle (5) is red,
so color both uncle
and parent black
and grandparent (6)
red. This causes
another double red:
4 and 6.
Since (6)’s uncle
(3) is black, we do
a left rotation at (2)
and (4) and recolor.
2
3
6
7
5
8
Insert 9
4
4
2
2
6
3
4
1
Finish inserting 8
1
7
5
Insert 8
When adding 8,
its uncle (5) is red,
so color both uncle
and parent black
and grandparent (6)
red.
7
5
7
1
6
3
5
8
7
8
When adding 9,
its uncle (null) is black, so rotation
(7) and (8) and recolor.
9
5
10/23/2013
Finish Inserting 9
Insert 10
4
4
2
2
6
1
3
8
5
6
1
7
9
3
8
5
7
10’s uncle (7) is red,
so recolor 7, 8 and 9.
After rotations and recoloring
9
10
Still Inserting 10
Finish Inserting 10
4
4
2
2
6
1
3
8
5
1
7
10’s uncle (7) is red,
so recolor 7, 8 and 9. It causes
a double red (6 and 8). Since
8’s uncle (2) is red, we recolor
2, 4, and 6.
9
10
3
9
10
4
2
2
6
8
5
7
Again a rotation of 9 and
10 and recoloring is
needed.
7
Finish inserting 11
4
3
8
5
10’s uncle (7) is red,
so recolor 7, 8 and 9. It causes
a double red (6 and 8). Since
8’s uncle (2) is red, we recolor
2, 4, and 6.
Color 4 black as it is the root
Insert 11
1
6
1
9
6
3
8
5
7
10
10
9
11
11
36
6
10/23/2013
Algorithm: Insertion
To practice more: http://gauss.ececs.uc.edu/RedBlack/redblack.html
7
Properties of Red Black Trees
• If a Red node has any children, it must have
two children and they must be Black.
(Why?)
• If a Black node has only one child, that
child must be a Red leaf. (Why?)
• Due to the rules there are limits on how
unbalanced a Red Black tree may become.
4
9
3
5
6
2
1
Minimal and Maximal Heights of
any Red Black Trees of N nodes
• If a Red Black Tree is complete, with all Black
nodes except for Red leaves at the lowest level
the height will be minimal, <= log2 N.
• To get the max height for N elements there
should be as many Red nodes as possible
down one path and all other nodes are Black
WHAT IS THE MINIMUM HEIGHT OF A
RED-BLACK TREE?
75
40
20
10
90
60
30
80
100
50
– This means the max height would be < 2 * log N
– see example on next slides
WHAT IS THE MAXIMUM HEIGHT?
Algorithm insertItem(key k, obj o)
50
30
20
Analysis of Insertion
1. We search for key k to locate
the insertion node z
2. We add the new item (k, o) at
node z and color z red
3. while doubleRed(z)
if isBlack(sibling(parent(z)))
rotations(z)
return
else // sibling(parent(z)) is red
z  recolorParents(z)
90
40
80
60
131
85
100
150
140
160
185
• Recall that a red-black tree has
O(log n) height
• Step 1 takes O(log n) time
because we visit O(log n)
nodes
• Step 2 takes O(1) time
• Step 3 takes O(log n) time
because we perform
– O(log n) recolorings, each taking
O(1) time, and
– at most two rotations taking O(1)
time
• Thus, an insertion in a redblack tree takes O(log n) time
7
10/23/2013
Search Tree Summary
•
•
•
•
•
•
BST, Red-Black and AVL
Example
50
Binary Trees
Red-black trees
2-3-4 trees
2-3 search trees
B-trees, multiway search trees
AVL Trees
100
60
70
75
90
60
70
80
50
60
90
70
80
90
50
100
100
78
75
Binary Search Tree (a)
2-3-4 Trees
75
78
78
80
AVL Tree (c)
Red-Black Tree (b)
3 Possible Kinds of Nodes
• 2-3-4 Trees are search trees such that every
internal node has 2, 3, or 4 children and the
number of keys in each node is one, two or
three.
• All the leaves of 2-3-4 are at the same level.
• 2-3-4 trees can be viewed as a red-black tree.
2-node
3-node
A
A B
value < A
value > A
4-node
A <B
A B C
value < A A < value < B value > B value < A A < value < B B < value < C value > C
When traversing a 2-3-4 tree during
insertion, always split a node with
three keys, creating a parent node.
2-3-4 Tree Example
12
25
4 8 10
A <B<C
B
A B C
A
2
5
7
9
11
15
35 55
S
T
U
A
C
V
S
T
U
V
48
8
10/23/2013
Insertion Example of 2-3-4 Tree

2
2 15
Insert 2
4
2 12 15
Insert 15
2
Insert 12
12
4
2
15
4
Split 4-node (2, 12, 15)
15
2
4
12
8 10
12
8
10
4
15
2
8
4
15 25
12
8 10
2
8
4
25
15
12
4
11
15
35
55
55
5
2
25
10
25
11
35
15
55
8
2
9
11
Split 4-node (8, 10, 11)
15
35
55
Insert 9
Convert 2-3-4 Tree to Red Black Tree
• Convert a node with 3 entries into a binary search tree by:
25
9
Insert 5
4
12
4 8 10
25
5 8 9
2
35
12
10
8
Insert 5, 7
Example (Cont…)
10
15
Insert 11
35 55
15
Insert 55
Split 4-node (15, 25, 35)

10 11
25
8 10
2
35
15
12
8
2
12
15 25 35
2
4
55
Insert 35
8 10
2
2
35
25
(4, 12,
25)
Split 4-node (15,
25, 35)
12
4
12
15
10
12
4
25
Insert 10
Insert 25
Insert 4
4
2
Split 4-node (2, 4, 8)
12
2
15
Example (Cont…)
12
12
8
Insert 11, 9, …5

Insert: 2, 15, 12, 4, 8, 10, 25, 35, 55, ….11
15
11
35
55
Split 4-node (5, 8, 9)
12
4
5
2
8
10
7
25
9
15
11
35
55
Converting a 2-3-4 Tree to Red-Black
Tree Example
Red-Black Trees
Rep resentation with a black
p arent and two red children
4-node (A, B, C)
in a 2-3-4 Tree
A
B
B
C
8
A
S
T
U
T
Representation with a black
parent and a red right child
A
T
Representation with a black
parent and a red left child
1 3 4
30 40
12 15
30
12 15
9
40
10
10
8
8
20
20
B
S
S
9
1 3 4
V
U
A
B
20
C
V
S
3-node (A, B)
in a 2-3-4 Tree
10
8 10 20
U
A
B
U
T
U
S
T
9
3
1
4
12
15
30
9
3
40
1
4
12
40
15
30
9
10/23/2013
From 2-3-4 Trees to Red-Black Trees
• A red-black tree is a representation of a 2-3-4 tree by means
of a binary tree whose nodes are colored red or black
• Comparing to 2-3-4 tree, a red-black tree has
– same logarithmic time performance
– simpler implementation with a single node type
4
3
4
5
5
3
2 6 7
3
OR
6
5
2
7
10
Download