Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only...

40
9/9/15 CMPS 2200 Intro. to Algorithms 1 CMPS 2200 – Fall 2015 Red-black trees Carola Wenk Slides courtesy of Charles Leiserson with changes by Carola Wenk

Transcript of Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only...

Page 1: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 1

CMPS 2200 – Fall 2015

Red-black treesCarola Wenk

Slides courtesy of Charles Leiserson with changes by Carola Wenk

Page 2: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 2

ADT Dictionary / Dynamic SetAbstract data type (ADT) Dictionary (also called Dynamic Set):A data structure which supports operations• Insert• Delete• FindUsing balanced binary search trees we can implement a dictionary data structure such that each operation takes O(log n) time.

8 1515

1010

1818

2222

3

7

1212 1717

Page 3: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 3

Search Trees

• A binary search tree is a binary tree. Each node stores a key. The tree fulfills the binary search tree property:

For every node x holds: • y x , for all y in the subtree left of x• x < y, for all y in the subtree right of x

8 1515

1010

1818

2222

3

7

1212 1717

Page 4: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 4

Search Trees

Different variants of search trees:

• Balanced search trees (guaranteeheight of O(log n) for n elements)

• k-ary search trees (such as B-trees,2-3-4-trees)

• Search trees that store keys onlyin leaves, and store copies ofkeys as split-values in internal nodes

8 1515

1010

1818

2222

3

7

1212 1717

1 1212

10 25

6

2 7 8

12 15 21

11 14 20 23 24 27 40 50

30 45

121

6 8 12 14

17

26 35 41 42

43

59 61

6 26 41 59

1 14 35 43

428

17

Page 5: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 5

Balanced search treesBalanced search tree: A search-tree data structure for which a height of O(log n) is guaranteed when implementing a dynamic set of n items.

Examples:

• AVL trees• 2-3 trees• 2-3-4 trees• B-trees• Red-black trees

Page 6: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 6

Red-black treesThis data structure requires an extra one-bit color field in each node.Red-black properties:1. Every node is either red or black.2. The root is black.3. The leaves (NIL’s) are black.4. If a node is red, then both its children are black.5. All simple paths from any node x, excluding x,

to a descendant leaf have the same number of black nodes = black-height(x).

Page 7: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 7

Example of a red-black tree

h = 4

8 11

10

18

26

22

3

7

NIL NIL

NIL NIL NIL NIL

NIL

NIL NIL

1. Every node is either red or black.

Page 8: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 8

Example of a red-black tree

8 11

10

18

26

22

3

7

NIL NIL

NIL NIL NIL NIL

NIL

NIL NIL

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

h = 4

Page 9: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 9

Example of a red-black tree

8 11

10

18

26

22

3

7

NIL NIL

NIL NIL NIL NIL

NIL

NIL NIL

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

h = 4

Page 10: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 10

Example of a red-black tree

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

8 11

10

18

26

22

3

7

NIL NIL

NIL NIL NIL NIL

NIL

NIL NIL

bh = 2

bh = 1

bh = 1

bh = 2

bh = 0

h = 4

Page 11: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 11

Height of a red-black tree

Theorem. A red-black tree with n keys has heighth 2 log(n + 1).

Proof.INTUITION:• Merge red nodes

into their black parents.

Page 12: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 12

Height of a red-black tree

Theorem. A red-black tree with n keys has heighth 2 log(n + 1).

Proof.INTUITION:• Merge red nodes

into their black parents.

Page 13: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 13

Height of a red-black tree

Theorem. A red-black tree with n keys has heighth 2 log(n + 1).

Proof.INTUITION:• Merge red nodes

into their black parents.

Page 14: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 14

Height of a red-black tree

Theorem. A red-black tree with n keys has heighth 2 log(n + 1).

Proof.INTUITION:• Merge red nodes

into their black parents.

Page 15: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 15

Height of a red-black tree

Theorem. A red-black tree with n keys has heighth 2 log(n + 1).

Proof.INTUITION:• Merge red nodes

into their black parents.

Page 16: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 16

Height of a red-black tree

Theorem. A red-black tree with n keys has heighth 2 log(n + 1).

Proof.

• This process produces a tree in which each node has 2, 3, or 4 children.

• The 2-3-4 tree has uniform depth h of leaves.

INTUITION:• Merge red nodes

into their black parents.

h

Page 17: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 17

Proof (continued)

h

h

• We haveh h/2, sinceat most halfthe vertices on any path are red.

• The number of leaves in each tree is n + 1 n + 1 2h'

log(n + 1) h' h/2 h 2 log(n + 1).

Page 18: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 18

Query operations

Corollary. The queries SEARCH, MIN, MAX, SUCCESSOR, and PREDECESSORall run in O(log n) time on a red-black tree with n nodes.

88 1111

1010

1818

2626

2222

33

77

NIL NIL

NIL NIL NIL NIL

NIL

NIL NIL

Page 19: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 19

Modifying operations

The operations INSERT and DELETE cause modifications to the red-black tree:1. the operation itself,2. color changes,3. restructuring the links of the tree

via “rotations”.

Page 20: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 20

Rotations

AA

B

RIGHT-ROTATE(B)

B

AA

LEFT-ROTATE(A)

• Rotations maintain the inorder ordering of keys:a , b , c a A b B c.

• Rotations maintain the binary search tree property• A rotation can be performed in O(1) time.

Page 21: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 21

Red-black treesThis data structure requires an extra one-bit color field in each node.Red-black properties:1. Every node is either red or black.2. The root is black.3. The leaves (NIL’s) are black.4. If a node is red, then both its children are black.5. All simple paths from any node x, excluding x,

to a descendant leaf have the same number of black nodes = black-height(x).

Page 22: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 22

Insertion into a red-black tree

1515

Example:• Insert x =15.

8 1111

1010

1818

2626

2222

7

3

IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring.

Page 23: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 23

Insertion into a red-black tree

1515

Example:• Insert x =15.• Recolor, moving the

violation up the tree. 8 1111

1010

1818

2626

2222

7

3

IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring.

Page 24: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 24

Insertion into a red-black tree

1515

Example:• Insert x =15.• Recolor, moving the

violation up the tree. 8 1111

1010

1818

2626

2222

7

3

IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring.

Page 25: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 25

Insertion into a red-black tree

8 1111

1010

1818

2626

2222

7

1515

Example:• Insert x =15.• Recolor, moving the

violation up the tree.• RIGHT-ROTATE(18).

3

IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring.

Page 26: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 26

Insertion into a red-black tree

8

1111

1010

1818

2626

2222

7

1515

Example:• Insert x =15.• Recolor, moving the

violation up the tree.• RIGHT-ROTATE(18).

3

IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring.

Page 27: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 27

Insertion into a red-black tree

8

1111

1010

1818

2626

2222

7

1515

Example:• Insert x =15.• Recolor, moving the

violation up the tree.• RIGHT-ROTATE(18).• LEFT-ROTATE(7)

3

IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring.

Page 28: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 28

Insertion into a red-black treeIDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring.

8 1111

1010

1818

2626

2222

7

1515

Example:• Insert x =15.• Recolor, moving the

violation up the tree.• RIGHT-ROTATE(18).• LEFT-ROTATE(7)

3

Page 29: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 29

Insertion into a red-black treeIDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring.

8 1111

1010

1818

2626

2222

7

1515

Example:• Insert x =15.• Recolor, moving the

violation up the tree.• RIGHT-ROTATE(18).• LEFT-ROTATE(7) and recolor.

3

Page 30: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 30

Insertion into a red-black treeIDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring.

8 1111

1010

1818

2626

2222

7

1515

Example:• Insert x =15.• Recolor, moving the

violation up the tree.• RIGHT-ROTATE(18).• LEFT-ROTATE(7) and recolor.

3

Page 31: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 31

PseudocodeRB-INSERT(T, x)

TREE-INSERT(T, x)color[x] RED ⊳ only RB property 4 can be violatedwhile x root[T] and color[p[x]] = RED

do if p[x] = left[p[p[x]]then y right[p[p[x]] ⊳ y = aunt/uncle of x

if color[y] = REDthen Case 1else if x = right[p[x]]

then Case 2 ⊳ Case 2 falls into Case 3Case 3

else “then” clause with “left” and “right” swappedcolor[root[T]] BLACK

Page 32: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 32

Graphical notation

Let denote a subtree with a black root.

All ’s have the same black-height.

Page 33: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 33

Case 1

B

C

DA

xy

(Or, A’s children are swapped.)

B

C

DA

new x

Push C’s black onto Aand D, and recurse, since C’s parent may be red.

Recolor

p[x] = left[p[p[x]]y right[p[p[x]]color[y] = RED

Continue

Page 34: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 34

Case 2

B

C

A

x

yLEFT-ROTATE(A)

A

C

B

x

y

Transform to Case 3.p[x] = left[p[p[x]]y right[p[p[x]]

x = right[p[x]]color[y] = BLACK

Page 35: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 35

RIGHT-ROTATE(C)(and recolor)

Case 3

A

C

B

x

yA

B

C

Done! No more violations of RB property 4 are possible.

p[x] = left[p[p[x]]y right[p[p[x]]

x = left[p[x]]color[y] = BLACK

Page 36: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 36

Analysis

• Go up the tree performing Case 1, which only recolors nodes.

• If Case 2 or Case 3 occurs, perform 1 or 2rotations, and terminate.

Running time: O(log n) with O(1) rotations.RB-DELETE — same asymptotic running time and number of rotations as RB-INSERT.

Page 37: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 37

Pseudocode (part II)else “then” clause with “left” and “right” swapped⊳ p[x] = right[p[p[x]]then y left[p[p[x]] ⊳ y = aunt/uncle of x

if color[y] = REDthen Case 1’else if x = left[p[x]]

then Case 2’ ⊳ Case 2’ falls into Case 3’Case 3’

color[root[T]] BLACK

Page 38: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 38

Case 1’C

y

(Or, A’s children are swapped.)

Recolor

p[x] = right[p[p[x]]y left[p[p[x]]color[y] = RED

B

A

xD

Push C’s black onto Aand D, and recurse, since C’s parent may be red.

new xC

y

B

A

xD

Continue

Page 39: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 39

Case 2’

C RIGHT-ROTATE(A)

p[x] = right[p[p[x]]y left[p[p[x]]

x = left[p[x]]color[y] = BLACK

AyBx

Transform to Case 3’.

C

A

y B x

Page 40: Red-black trees · Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only red-black property 4 might be violated. Move the violation up the tree by recoloring until

9/9/15 CMPS 2200 Intro. to Algorithms 40

Case 3’

LEFT-ROTATE(C)(and recolor)

C

B

A

Done! No more violations of RB property 4 are possible.

p[x] = right[p[p[x]]y left[p[p[x]]

x = right[p[x]]color[y] = BLACK

C

A

y B x