36
Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as w ell as various materials from the web.

Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

Embed Size (px)

Citation preview

Page 1: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

Chapter 13

Red-Black Trees Lee, Hsiu-Hui

Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the web.

Page 2: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 2

• Many search-tree schemes that are “balanced” in order to guarantee that basic dynamic-set operations take O(lg n ) time in the worse case.

e.g. AVL trees Red-black trees

Balanced search Trees

Page 3: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 3

Red-Black Tree

• a variation of binary search trees• with one extra bit of storage per node: its color, which

can either RED or BLACK. • Each node of the tree now contains the fields color, key,

left, right, and p. If a child or the parent of a node does not exist, the corresponding pointer field of the node contains the value NIL. We shall regard these NIL’s as being pointers to external nodes(leaves) of the binary search tree and the normal, key-bearing nodes as being internal nodes of the tree.

Page 4: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 4

Red-Black Properties

1. Every node is either red or black.2.2. The root is black.The root is black.3. Every leaf (NIL) is black4. If a node is red, then both its children are black.5. For each node, all paths from the node x to desce

ndant leaves contain the same number of black nodes (i.e. black-height(x)).

Page 5: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 5

Example of a red-black tree

Page 6: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 6

Example of a red-black tree

1. Every node is either red or black.

Page 7: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 7

Example of a red-black tree

2. 3. The root and leaves (NIL’s) are black.

Page 8: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 8

4. If a node is red, then its children are black.

?? Means (If a node is red, then its parent is black.)

Example of a red-black tree

Page 9: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 9

5. All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x).

Example of a red-black tree

Page 10: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 10

black-height of the node: bh(x)

# of black nodes on any path from, but not including,

a node x down to a leaf

black-height of a RB tree = black-height of its root

Page 11: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 11

Page 12: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 12

Leaves and the root’s parent omitted entirely

Page 13: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 13

Height and black-height

• Height of a node x: h(x)

is the number of edges in a longest path to a leaf.• Black-height of a node x: bh(x) is the number of black nodes (including nil[T ]) on the path from x to leaf, not counting x.

Page 14: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 14

Lemma 13.0aThe subtree rooted at any node x contains ≥ 2bh(x) − 1 internal nodes.Proof By induction on height of x.Basis: Height of x = 0 ⇒ x is a leaf bh⇒ (x) = 0. The subtree

rooted at x has 0 internal nodes. 20 − 1 = 0.Inductive step: Let the height of x be h and bh(x) = b. Any child of x has height h − 1 and black-height either b

(if the child is red) or b − 1 (if the child is black). By the inductive hypothesis, each child has ≥ 2bh(x)-1 − 1 in

ternal nodes.Thus, the subtree rooted at x contains ≥ 2 · (2bh(x)-1 − 1 )+1 =

2bh(x) −1 internal nodes. (The +1 is for x itself.)

Page 15: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 15

Lemma 13.0bAny node with height h has black –height at

least h/2.

Proof By property 4 (If a node is red, then both its

children are black), ≤ h/2 nodes on the path from the node to a leaf are

red.Hence ≥ h/2 are black.

Page 16: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 16

Lemma 13.1A red-black tree with n internal nodes h

as height at most 2lg(n+1).Proof

Let h and b be the height and black-height of the root, respectively.

By the above two claims,

n ≥ 2b − 1 ≥ 2h/2 − 1 .

Adding 1 to both sides and then taking logs gives

lg(n + 1) ≥ h/2,

which implies that h ≤ 2 lg(n + 1).

Page 17: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 17

13.2 Rotations

Left-Rotation makes y’s left subtree into x’s right subtree.Right-Rotation makes x’s right subtree into y’s left subtree.

Page 18: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 18

LEFT-ROTATE(T, x)

1. y ← right[x] ✄ Set y.

2. right[x] ← left[y] ✄ Turn y’s left subtree into x’s right subtree.

3. if left[y] ≠ nil[T ]4. then p[left[y]] ← x5. p[y] ← p[x] ✄ Link x’s parent to y.

6. if p[x] = nil[T ]7. then root[T ]← y8. else if x = left[p[x]]9. then left[p[x]] ← y10. else right[p[x]] ← y11. left[y] ← x ✄ Put x on y’s left.

12. p[x] ← y

The code for RIGHT-ROTATE is symmetric.Both LEFT-ROTATE and RIGHT-ROTATErun in O(1) time.

Page 19: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 19

Example of LEFT-ROTATE(T,x)Rotation makes y’s left subtree into x’s right subtree.

Page 20: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 20

Some Good Java Appletsto simulate Red-Black Tree• http://

webpages.ull.es/users/jriera/Docencia/AVL/AVL tree applet.htm

• http://gauss.ececs.uc.edu/RedBlack/redblack.html

Page 21: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

Insertion and Deletion

Page 22: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 22

RB-INSERT(T, x)1. y ← nil[T]2. x ← root[T]3. while x ≠ nil[T]4. do y ← x5. if key[z] < key[x]6. then x ← left[x]7. else x ← right[x]8. p[z] ← y9. if y = nil[T]10. then root[T] ← z11. else if key[z] < key[y]12. then left[y] ← z13. else right[y] ← z14. left[z] ← nil[T]15. right[z] ← nil[T]16. color[z] ← RED17. RB-INSERT-FIXUP(T, z)

Page 23: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 23

RB-INSERT-FIXUP(T, z)1 while color[p[z]] = RED

2 do if p[z] = left[p[p[z]]]

3 then y ← right[p[p[z]]]

4 if color[y] = RED

5 then color[p[z]] ←BLACK Case 1

6 color[y] ← BLACK Case 1

7 color[p[p[z]]] ← RED Case 1

8 z ← p[p[z]] Case 1

Page 24: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 24

9 else if z = right[p[z]]

10 then z ← p[p[z]] Case 2

11 LEFT-ROTATE(T,z) Case 2

12 color[p[z]] ← BLACK Case 3

13 color[p[p[z]]] ← RED Case 3

14 RIGHT-ROTATE(T,p[p[z]]) Case 3

15 else (same as then clause

with “right” and “left” exchanged)

16 color[root[T]] ← BLACK

Page 25: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 25

The operation of RB-INSERT-FIXUP

紅叔

黑叔右子

Page 26: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 26

黑叔左子

Page 27: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 27

case 1 of the RB-INSERT(z’s uncle y is red)

紅叔右子

紅叔左子

Page 28: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 28

case 2/3 of the RB-INSERT(z’s uncle y is black and z is a right/left child)

黑叔右子 黑叔左子

Page 29: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 29

Analysis

• RB-INSERT take a total of O(lg n) time.• It never performs more than two rotatio

ns, since the while loop terminates if case 2 or case 3 executed.

Page 30: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 30

13.4 Deletion

RB-DELETE(T, z)1 if left[z] = nil[T] or right[z] = nil[T]

2 then y ← z

3 else y ← TREE-SUCCESSOR(z)

4 if left[y] ≠ nil[T]

5 then x ← left[y]

6 else x ← right[y]

7 p[x] ← p[y]

8 if p[y] = nil[T]

Page 31: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 31

9 then root[T] ← x10 else if y = left[p[y]]11 then left[p[y]] ← x12 else right[p[y]] ← x13 if y ≠ z14 then key[z] ← key[y]15 copy y’s satellite data into z16 if color[y] = BLACK

17 then RB-DELETE-FIXUP(T, x)18 return y

Page 32: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 32

RB-DELETE-FIXUP(T,x)

RB-DELETE-FIXUP (T, x)

1 while x ≠ root[T] and color[x] = BLACK

2 do if x = left[p[x]]

3 then w ← right[p[x]]

4 if color[w] = RED

5 then color[w] ← BLACK Case1

6 color[p[x]] = RED Case1

7 LEFT-ROTATE(T,p[x]) Case1

8 w ← right[p[x]] Case1

Page 33: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 33

9 if color[right[w]] = BLACK and color[right[w]= BLACK10 then color[w] ← RED Case211 x ← p[x] Case212 else if color[right[w]] = BLACK 13 then color[left[w]] ← BLACK Case314 color[w] ← RED Case315 RIGHT-ROTATE(T,w) Case3 16 w ← right[p[x]] Case317 color[w] ← color[p[x]] Case418 color[p[x]] ← BLACK Case419 color[right[w]] ← BLACK Case420 LEFT-ROTATE(T,p[x]) Case421 x ← root[T] Case422 else (same as then clause with “right” and “left” exchan

ged)23 color[x] ← BLACK

Page 34: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 34

the case in the while loop of RB-DELETE-FIXUP

Page 35: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 35

Page 36: Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the

20071130 chap13 Hsiu-Hui Lee 36

Analysis

• The RB-DELETE-FIXUP takes O(lg n) time and performs at most three rotations.

• The overall time for RB-DELETE is therefore also O(lg n)