Red-black trees Carola Wenk Slides courtesy of Charles Leiserson - - PowerPoint PPT Presentation

red black trees
SMART_READER_LITE
LIVE PREVIEW

Red-black trees Carola Wenk Slides courtesy of Charles Leiserson - - PowerPoint PPT Presentation

CMPS 2200 Fall 2017 Red-black trees Carola Wenk Slides courtesy of Charles Leiserson with changes by Carola Wenk 9/13/17 1 CMPS 2200 Intro. to Algorithms Dynamic Set A dynamic set , or dictionary , is a data structure which supports


slide-1
SLIDE 1

9/13/17 CMPS 2200 Intro. to Algorithms 1

CMPS 2200 – Fall 2017

Red-black trees

Carola Wenk

Slides courtesy of Charles Leiserson with changes by Carola Wenk

slide-2
SLIDE 2

9/13/17 CMPS 2200 Intro. to Algorithms 2

Dynamic Set

A dynamic set, or dictionary, is a data structure which supports operations

  • Insert
  • Delete
  • Find

Using balanced binary search trees we can implement a dictionary data structure such that each operation takes O(log n) time.

8 15 15 10 10 18 18 22 22 3 7 12 12 17 17

slide-3
SLIDE 3

9/13/17 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 15 15 10 10 18 18 22 22 3 7 12 12 17 17

x  x  x

slide-4
SLIDE 4

9/13/17 CMPS 2200 Intro. to Algorithms 4

Search Trees

Different variants of search trees:

  • Balanced search trees (guarantee

height of O(log n) for n elements)

  • k-ary search trees (such as B-trees,

2-3-4-trees)

  • Search trees that store keys only

in leaves, and store copies of keys as split-values in internal nodes

8 15 15 10 10 18 18 22 22 3 7 12 12 17 17 1 12 12

10 25 6 2 7 8 12 15 21 11 14 20 23 24 27 40 50 30 45 12 1 6 8 12 14 17 26 35 41 42 43 59 61 6 26 41 59 1 14 35 43 42 8 17

slide-5
SLIDE 5

9/13/17 CMPS 2200 Intro. to Algorithms 5

Balanced search trees

Balanced 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
slide-6
SLIDE 6

9/13/17 CMPS 2200 Intro. to Algorithms 6

Red-black trees

This 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 (null’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).

slide-7
SLIDE 7

9/13/17 CMPS 2200 Intro. to Algorithms 7

Example of a red-black tree

h = 4

8 11 10 18 26 22 3 7

  • 1. Every node is either red or black.

null null null null null null null null null

slide-8
SLIDE 8

9/13/17 CMPS 2200 Intro. to Algorithms 8

Example of a red-black tree

8 11 10 18 26 22 3 7

2., 3. The root and leaves (null’s) are black. h = 4

null null null null null null null null null

slide-9
SLIDE 9

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

Example of a red-black tree

8 11 10 18 26 22 3 7

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

black. h = 4

null null null null null null null null null

slide-10
SLIDE 10

9/13/17 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

bh = 2 bh = 1 bh = 1 bh = 2 bh = 0

h = 4

null null null null null null null null null

slide-11
SLIDE 11

9/13/17 CMPS 2200 Intro. to Algorithms 11

Height of a red-black tree

  • Theorem. A red-black tree with n keys has height

h  2 log(n + 1). Proof. INTUITION:

  • Merge red nodes

into their black parents. T

slide-12
SLIDE 12

9/13/17 CMPS 2200 Intro. to Algorithms 12

Height of a red-black tree

  • Theorem. A red-black tree with n keys has height

h  2 log(n + 1). Proof. INTUITION:

  • Merge red nodes

into their black parents. T

slide-13
SLIDE 13

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

Height of a red-black tree

  • Theorem. A red-black tree with n keys has height

h  2 log(n + 1). Proof. INTUITION:

  • Merge red nodes

into their black parents. T

slide-14
SLIDE 14

9/13/17 CMPS 2200 Intro. to Algorithms 14

Height of a red-black tree

  • Theorem. A red-black tree with n keys has height

h  2 log(n + 1). Proof. INTUITION:

  • Merge red nodes

into their black parents. T

slide-15
SLIDE 15

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

Height of a red-black tree

  • Theorem. A red-black tree with n keys has height

h  2 log(n + 1). Proof. INTUITION:

  • Merge red nodes

into their black parents. T’

slide-16
SLIDE 16

9/13/17 CMPS 2200 Intro. to Algorithms 16

Height of a red-black tree

  • Theorem. A red-black tree with n keys has height

h  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 T’

slide-17
SLIDE 17

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

Proof (continued)

h h

  • # leaves in T = # leaves in T’
  • # leaves in T = n+1 (fact about

binary trees with exactly 2 children per internal node)

  • # leaves in T’ 2h’(fact about

binary trees; T’ can only have more)  n + 1  2h'  log(n + 1)  h'  h/2  h  2 log(n + 1).

T’ T

  • We have h  h/2,

since at most half the vertices on any path are red.

slide-18
SLIDE 18

9/13/17 CMPS 2200 Intro. to Algorithms 18

Query operations

  • Corollary. The queries SEARCH, MIN,

MAX, SUCCESSOR, and PREDECESSOR all run in O(log n) time on a red-black tree with n nodes.

8 11 11 10 10 18 18 26 26 22 22 3 7

slide-19
SLIDE 19

9/13/17 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”.

slide-20
SLIDE 20

9/13/17 CMPS 2200 Intro. to Algorithms 20

Rotations

A B   

RIGHT-ROTATE(B)

B A   

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

 Can be applied to any BST, not just red-black trees

  • A rotation can be performed in O(1) time.
slide-21
SLIDE 21

9/13/17 CMPS 2200 Intro. to Algorithms 21

Rotation Example

39 39 41 41 42 42 9 31 31 6 20 20 2 8 16 16 22 22 15 15 17 17 11 11

RIGHT-ROTATE(20)

39 39 41 41 42 42 9 31 31 6 16 16 2 8 15 15 20 20 11 11 17 17 22 22

A B    RIGHT-ROTATE(B) B A    LEFT-ROTATE(A)

In-order(v){ if v==null return; In-order(v.left); print(v.key+” ”); In-order(v.right); }

In-order traversal: 2 6 8 9 11 15 16 17 20 22 31 39 41 42 In-order traversal: 2 6 8 9 11 15 16 17 20 22 31 39 41 42

 Maintains sorted order of keys, and can reduce height

slide-22
SLIDE 22

9/13/17 CMPS 2200 Intro. to Algorithms 22

Red-black trees

This 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 (null’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).

slide-23
SLIDE 23

9/13/17 CMPS 2200 Intro. to Algorithms 23

Insertion into a red-black tree

15 15

Example:

  • Insert x =15.

8 11 11 10 10 18 18 26 26 22 22 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.

slide-24
SLIDE 24

9/13/17 CMPS 2200 Intro. to Algorithms 24

Insertion into a red-black tree

15 15

Example:

  • Insert x =15.
  • Recolor, moving the

violation up the tree.

8 11 11 10 10 18 18 26 26 22 22 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.

slide-25
SLIDE 25

9/13/17 CMPS 2200 Intro. to Algorithms 25

Insertion into a red-black tree

15 15

Example:

  • Insert x =15.
  • Recolor, moving the

violation up the tree.

8 11 11 10 10 18 18 26 26 22 22 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.

slide-26
SLIDE 26

9/13/17 CMPS 2200 Intro. to Algorithms 26

Insertion into a red-black tree

8 11 11 10 10 18 18 26 26 22 22 7 15 15

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.

slide-27
SLIDE 27

9/13/17 CMPS 2200 Intro. to Algorithms 27

Insertion into a red-black tree

8 11 11 10 10 18 18 26 26 22 22 7 15 15

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.

slide-28
SLIDE 28

9/13/17 CMPS 2200 Intro. to Algorithms 28

Insertion into a red-black tree

8 11 11 10 10 18 18 26 26 22 22 7 15 15

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.

slide-29
SLIDE 29

9/13/17 CMPS 2200 Intro. to Algorithms 29

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 it can be fixed with rotations and recoloring.

8 11 11 10 10 18 18 26 26 22 22 7 15 15

Example:

  • Insert x =15.
  • Recolor, moving the

violation up the tree.

  • RIGHT-ROTATE(18).
  • LEFT-ROTATE(7)

3

slide-30
SLIDE 30

9/13/17 CMPS 2200 Intro. to Algorithms 30

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 it can be fixed with rotations and recoloring.

8 11 11 10 10 18 18 26 26 22 22 7 15 15

Example:

  • Insert x =15.
  • Recolor, moving the

violation up the tree.

  • RIGHT-ROTATE(18).
  • LEFT-ROTATE(7) and recolor.

3

slide-31
SLIDE 31

9/13/17 CMPS 2200 Intro. to Algorithms 31

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 it can be fixed with rotations and recoloring.

8 11 11 10 10 18 18 26 26 22 22 7 15 15

Example:

  • Insert x =15.
  • Recolor, moving the

violation up the tree.

  • RIGHT-ROTATE(18).
  • LEFT-ROTATE(7) and recolor.

3

slide-32
SLIDE 32

9/13/17 CMPS 2200 Intro. to Algorithms 32

Pseudocode

RB-INSERT(T, x) TREE-INSERT(T, x) color[x]  RED ⊳ only RB property 4 can be violated while 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] = RED then Case 1 else if x = right[p[x]] then Case 2 ⊳ Case 2 falls into Case 3 Case 3 else “then” clause with “left” and “right” swapped color[root[T]]  BLACK

slide-33
SLIDE 33

9/13/17 CMPS 2200 Intro. to Algorithms 33

Graphical notation

Let denote a subtree with a black root. All ’s have the same black-height.

slide-34
SLIDE 34

9/13/17 CMPS 2200 Intro. to Algorithms 34

Case 1

B C D A

x y

(Or, A’s children are swapped.)

B C D A

new x

Push C’s black onto A and 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

slide-35
SLIDE 35

9/13/17 CMPS 2200 Intro. to Algorithms 35

Case 2

B C A

x y LEFT-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

slide-36
SLIDE 36

9/13/17 CMPS 2200 Intro. to Algorithms 36

RIGHT-ROTATE(C) (and recolor)

Case 3

A C B

x y

A 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

slide-37
SLIDE 37

9/13/17 CMPS 2200 Intro. to Algorithms 37

Analysis

  • Go up the tree performing Case 1, which only

recolors nodes.

  • If Case 2 or Case 3 occurs, perform 1 or 2

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

slide-38
SLIDE 38

9/13/17 CMPS 2200 Intro. to Algorithms 38

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] = RED then Case 1’ else if x = left[p[x]] then Case 2’ ⊳ Case 2’ falls into Case 3’ Case 3’ color[root[T]]  BLACK

slide-39
SLIDE 39

9/13/17 CMPS 2200 Intro. to Algorithms 39

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

x

D

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

new x

C

y

B A

x

D

Continue

slide-40
SLIDE 40

9/13/17 CMPS 2200 Intro. to Algorithms 40

Case 2’

C

RIGHT-ROTATE(A)

p[x] = right[p[p[x]] y  left[p[p[x]] x = left[p[x]] color[y] = BLACK A

y

B

x Transform to Case 3’.

C A

y

B x

slide-41
SLIDE 41

9/13/17 CMPS 2200 Intro. to Algorithms 41

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