red black tree - uc davis mathematicslrademac/sp16_2331/red_black_tree.pdfcse 2331 red-black trees...
TRANSCRIPT
CSE 2331
Red-Black Trees
6.1
CSE 2331
Red-Black Trees
Definition. A red-black tree is a binary search tree with the
following properties:
• Every node is either red or black;
• The root is black;
• Every leaf is NIL and is black;
• If a node is red, then both its children are black;
• For each node, all simple paths from the node to descendant
leaves contain the same number of black nodes.
(Note: Every node in a binary tree is either a leaf or has BOTH a
left AND right child.)
6.2
CSE 2331
Red-Black Tree: Example
26
llllllllllll
SSSSSSSSSSSS
17
ssssss
sKKKKKKK 41
sssssssAA
AAA
14
}}}}
}AA
AAA 21
||||
| 000
30
��� AA
AAA 47
���� ''
''
10
��� 00
016
���
''''
19
���� 00
0 23
���� ''
''28
���� ''
''38
��� BB
BBB
7
���
%%%%
12
���� ''
''15
���� 00
0020
���� 00
0035
���� ''
''39
���� ''
''
3
��� ..
.
6.3
CSE 2331
Red-Black Tree: Example
26
llllllllllll
SSSSSSSSSSSS
17
ssssss
sKKKKKKK 41
sssssssAA
AAA
14
}}}}
}AA
AAA 21
||||
| 000
30
��� AA
AAA 47
10
��� 00
016
���
19
000 23 28 38
��� BB
BBB
7
���
12 15 20 35 39
3
6.4
CSE 2331
NOT a Red-Black Tree
This tree is NOT a red-black tree. Why not?
46
iiiiiiiiiiiiiiiii
RRRRRRRRRRR
25
uuuu
uuIIIIII 65
uuuuuu>>
>>
17
���� >>
>>40
~~~~ @@
@@53
��� >>
>>72
10
��� ..
.21
���
29
/// 43
��� //
/ 49 60
��� @@
@@
7 13 20 36 42 45 56 62
6.5
CSE 2331
Red-Black Tree Exercise
Color the following tree so that it is a red-black tree:
20
ooooooooo??
???
13
���� ??
??? 26
���� ??
???
8
����
,,,,
16
���� //
//32
���� //
//
5
��� 00
0
6.6
CSE 2331
Red-Black Tree Height
Definition. A red-black tree satisfies the following properties:
• Every node is either red or black;
• The root is black;
• Every leaf is NIL and is black;
• If a node is red, then both its children are black;
• For each node, all simple paths from the node to descendant
leaves contain the same number of black nodes.
Theorem. A red-black tree with n internal nodes has height at
most 2 log2(n + 1).
6.7
CSE 2331
Complete Binary Tree Size
Theorem 1. A complete binary search tree of height h has
2h+1 − 1 nodes.
Proof. Level i has 2i nodes (i = 0, 1, . . . , h).
1 + 2 + 22 + 23 + . . . + 2h = 2h+1 − 1.
6.8
CSE 2331
Red-Black Tree Size
Theorem 2. A red-black tree of height h has at least 2⌈h/2⌉ − 1
internal nodes.
Proof. (By Dr. Y. Wang.)
Let T be a red-black tree of height h.
Remove the leaves of T forming a tree T ′ of height h− 1.
Let r be the root of T ′.
Since no child of a red node is red and r is black, the longest path
from r to a leaf has at least ⌈h/2⌉ black nodes.
Since every path from r to a leaf has the same number of black
nodes, every path from r to a leaf has at least ⌈h/2⌉ black nodes.
Thus, T ′ contains a complete binary tree of height at least ⌈h/2⌉−1.
(Note: height = Number of EDGES on longest path from root to
leaf.)
By Theorem 1, tree T ′ has at least 2⌈h/2⌉−1+1 − 1 nodes so tree T
has at least 2⌈h/2⌉ − 1 internal nodes.
6.9
CSE 2331
Red-Black Tree Height
Theorem. A red-black tree with n internal nodes has height at
most 2 log2(n + 1).
Proof. Let h be the height of a red-black tree with n nodes.
By Theorem 2, n ≥ 2⌈h/2⌉ − 1.
Thus, log2(n + 1) ≥ ⌈h/2⌉ ≥ h/2 so h ≤ 2 log2(n + 1).
6.10
CSE 2331
Red-Black Tree: Insert
26
llllllllllll
SSSSSSSSSSSS
17
ssssss
sKKKKKKK 41
sssssssAA
AAA
14
}}}}
}AA
AAA 21
||||
| 000
30
��� AA
AAA 47
10
��� 00
016
���
19
000 23 28 38
��� BB
BBB
7
���
12 15 20 35 39
3
6.11
CSE 2331
Red-Black Tree Insert
function RBLocateParent(T , z)
/* Return future parent of z in tree */
1 y ← NIL;
2 x← T.root;
3 while (x is not a leaf) do
4 y ← x;
5 if (z.key < x.key) then x← x.left;
6 else x← x.right;
7 end
8 return (y);
6.12
CSE 2331
Red-Black Tree Insert
8jjjjjjjj
UUUUUUUUUU
3III
I 15qqq
qVVVVVVVVVV
5 12 19qqq
q MMMM
17 25function RBTreeInsert(T , z)
1 y ← RBLocateParent(T, z);
2 z.parent← y;
3 if (y = NIL) then T.root← z; /* tree T was empty*/
4 else if (z.key < y.key) then y.left← z;
5 else y.right← z;
6 z.left← leaf;
7 z.right← leaf;
8 z.color← Red;
9 RBInsertFixup(T ,z);
6.13
CSE 2331
Insert Fixup: Case I
w
wwww
wwAA
AAA
x
���� 33
33y
���
////
a z b c
Recolor=⇒
w
wwww
wwAA
AAA
x
���� 33
33y
���
////
a z b c
If the parent and “uncle” of z are Red:
• Color the parent of z Black;
• Color the uncle of z Black;
• Color the grandparent of z Red;
• Repeat on the grandparent of z.
6.14
CSE 2331
Red-Black Tree Insert Fixup: Case I
10
����
VVVVVVVVVVVVVVVVVVV
... 21
uuuuuuuIIIIIII
18
���� 88
8825
����
JJJJJJJ
15
����
19 22 30
���� 88
88
13 27 34
6.15
CSE 2331
Sibling
p
wwwwwwGGGGGG
x
���� ::
::y
���� ::
::
p
wwwwwwGGGGGG
y
���� ::
::x
���� ::
::
function Sibling(x)
/* Return sibling of x */
1 if (x.parent = NIL) then error “Root has no siblings.”;
2 p← x.parent;
3 if (p.left = x) then return (p.right);
4 else return (p.left);
6.16
CSE 2331
Red-Black Tree Insert Fixup: Case I
w
sssssFF
FF
x
88
8y
�� 333
a z b c
Recolor=⇒
w
sssssFF
FF
x
88
8y
�� 333
a z b c
function RBInsertFixupA(T , alters z)
1 while (z 6= T.root) and (z.parent.color 6= Black) do
2 y ← Sibling(z.parent);
3 if (y.color = Black) then return;
4 z.parent.color← Black;
5 y.color← Black;
6 z ← z.parent.parent;
7 z.color← Red;
8 end
6.17
CSE 2331
Insert Fixup: Case III
w
��� 00
0
x
��� ((
(y
��� ''
'
z a b c
RightRotate
=⇒
x
��� 22
2
z w
���� ,,
,
a y
��� ((
(
b c
Recolor=⇒
x
��� 22
2
z w
���� ,,
,
a y
��� ((
(
b c
If the parent of z is red and its “uncle” is black:If z is a left child and its parent is a left child:
• Right Rotate on the grandparent of z;
• Color the parent of z Black;
• Color the sibling of z red.
6.18
CSE 2331
Red-Black Tree Insert Fixup: Case III
10
����
VVVVVVVVVVVVVVVVVVV
... 21
uuuuuuuIIIIIII
18
���� 88
8825
����
JJJJJJJ
15
����
19 22 30
���� 88
88
13 27 34
6.19
CSE 2331
Red-Black Tree Insert Fixup: Case III
w�� ;;
x y
z a b c
RightRotate
=⇒
x ??z w
��a y
b c
Recolor=⇒
x ??z w
��a y
b c
function RBInsertFixupC(T , alters z)
1 if (z = T.root) or (z.parent.color = Black) then return;
2 x← z.parent;
3 w ← x.parent;
4 if (z = x.left) and (x = w.left) then
5 RightRotate(T ,w);
6 x.color← Black;
7 w.color← Red;
8 else if (z = x.right) and (x = w.right) then
9 Handle same as above with “right” and “left” exchanged
10 . . .
6.20
CSE 2331
Insert Fixup: Case II
wooooo HHH
x�� ??
y
a zzz EE
b c
LeftRotate
=⇒
wrrr
r HHH
z�� 88
y
x DD c
a b
If the parent x of z is red and its “uncle” is black:If z is a right child and its parent x is a left child:
• z ← x
• Left Rotate on x;
• Apply algorithm for Case III.
6.21
CSE 2331
Red-Black Tree Insert Fixup: Case II
10
����
VVVVVVVVVVVVVVVVVVV
... 21
uuuuuuuIIIIIII
18
���� 88
8825
����
JJJJJJJ
15
����
19 22 30
���� 88
88
13 27 34
6.22
CSE 2331
Red-Black Tree Insert Fixup: Case II
w
nnnnn III
x�� ??
y
a zzz FF
b c
LeftRotate
=⇒
w
rrrr III
z�� 99
y
x DD c
a b
function RBInsertFixupB(T , alters z)
1 if (z = T.root) or (z.parent.color = Black) then return;
2 x← z.parent;
3 w ← x.parent;
4 if (z = x.right) and (x = w.left) then
5 z ← x;
6 LeftRotate(T ,x);
7 else if (z = x.left) and (x = w.right) then
8 Handle same as above with “right” and “left” exchanged
9 . . .
6.23
CSE 2331
Red-Black Tree Insert Fixup
8jjjjjjjj
UUUUUUUUUU
3III
I 15qqq
qVVVVVVVVVV
5 12 19qqq
q MMMM
17 25
function RBInsertFixup(T , z)
1 RBInsertFixupA (T ,z);
2 RBInsertFixupB (T ,z);
3 RBInsertFixupC (T ,z);
4 T.root.color← Black;
6.24
CSE 2331
Red-Black Tree Insert Fixup
10
����
VVVVVVVVVVVVVVVVVVV
... 30
uuuuuuuIIIIIII
18
����
JJJJJJJ 35
15
����
25
���� 88
88
13 22 27
6.25
CSE 2331
Running Time Analysis: RBInsertFixup
8jjjjjjjj
UUUUUUUUUU
3III
I 15qqq
qVVVVVVVVVV
5 12 19qqq
q MMMM
17 25
function RBInsertFixup(T , z)
1 RBInsertFixupA (T ,z);
2 RBInsertFixupB (T ,z);
3 RBInsertFixupC (T ,z);
4 T.root.color← Black;
6.26