Top Banner
Data Structures Haim Kaplan and Uri Zwick November 2013 Lecture 4 Red-Black Trees
80

Data Structures

Feb 24, 2016

Download

Documents

Amora

Data Structures. Lecture 4 Red -Black Trees. Haim Kaplan and Uri Zwick November 2013. Red -Black trees [Bayer 1972] [ Guibas-Sedgewick 1978]. External leaves. Red -Black trees. Binary search tree with external leaves Each node is colored red or black - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Data Structures

Data Structures

Haim Kaplan and Uri ZwickNovember 2013

Lecture 4Red-Black Trees

Page 2: Data Structures

2

Red-Black trees[Bayer 1972] [Guibas-Sedgewick 1978]

External leaves

Page 3: Data Structures

3

Binary search tree with external leaves

Each node is colored red or blackEach root to leaf path contains the

same number of black nodesThe parent of a red node must be blackThe root and external leaves are black

Red-Black trees

Page 4: Data Structures

4

Red-Black trees

Paths from each node to the leaveshave the same length

Page 5: Data Structures

5

Red-Black trees

What is the maximal height of a Red-Black tree containing n nodes?

Page 6: Data Structures

6

Height and Black height

Black height - Number of black nodes on paths from the node to the external leaves (excluding the node)

Height 2 black height

1

2

3

4

1

1

0

0 0

0

1

2

1 1

1

2

0

0 0

0

Page 7: Data Structures

7

Height of Red-Black trees

Lemma: The subtree of a node of black height kcontains at least 2k1 nodes

Corollary: The black height of a Red-Black treecontaining n nodes is at most log2(n+1)

Corollary: The height of a Red-Black treecontaining n nodes is at most 2 log2(n+1)

Page 8: Data Structures

8

Height of Red-Black trees

Lemma: The subtree of a node of black height kcontains at least 2k1 nodes

k ork−1

k

k ork−1

1

0

Page 9: Data Structures

9

Fold red nodes into their parents

Red-Black trees 2-4 trees

Page 10: Data Structures

10

Fold red nodes into their parents

Red-Black trees 2-4 trees

Page 11: Data Structures

11

Height of 2-4 tree = black height of Red-Black tree

Red-Black trees 2-4 trees

Page 12: Data Structures

Red-Black trees have logarithmic height

Page 13: Data Structures

13

Insert

Page 14: Data Structures

14

Insert (cont.)

Page 15: Data Structures

15

Insert (cont.)

Page 16: Data Structures

16

Insert (cont.)

Page 17: Data Structures

17

Rotations

y

x

B

C

A

x

y

B

A

C

Right rotate

Left rotate

Page 18: Data Structures

19

Insert (cont.)

xy

z

yz x

====>

Page 19: Data Structures

20

Insert (cont.)

Page 20: Data Structures

21

Insert (cont.)

Page 21: Data Structures

22

Insert (cont.)

Page 22: Data Structures

23

Insert (cont.)

Page 23: Data Structures

24

C

A D

Bz

w

Insert: Case 1a

C

A D

Bz

new z

Recolor z’s parent, uncle and grandparent

Red rule violation resolved or moved up the treeBlack heights do not change

z’s uncle w is red, z is a right child

Page 24: Data Structures

25

C

A D

Bz

w

C

A D

Bz

y

new z

Symmetric

Insert: Case 1bz’s uncle w is red, z is a left child

Page 25: Data Structures

26

C

A

Bz

C

A

B

z

Use a left rotation to convert to case 3Black heights do not change

Note: the roots of ,,, are black

Insert: Case 2z’s uncle w is black, z is a right child

Page 26: Data Structures

27

C

A

B

z

Use a right rotation to resolve

Note: the roots of ,,, are black

B

CA

Insert: Case 3z’s uncle w is black, z is a left child

Page 27: Data Structures

28

In all cases above, z’s parent is a left child

The other cases (z’s parent is a right child)are mirror images of the cases shown

All cases covered?

Case 1: z’s uncle is redCase 2: z’s uncle is black and z is a right childCase 3: z’s uncle is black and z is a left child

Page 28: Data Structures

29

Case 1

Fixing a Red-Black Tree after insertion

Case 2

Case 3

finish

Can only loop in Case 1

Page 29: Data Structures

30

Insert - analysisFind insertion point – O(log n) time

Insert – O(1) timeFix the tree – O(log n) time

Fixing the tree:At most 2 rotations

O(1) amortized time

Total time spend on fixing the tree while inserting n elements is O(n)

Page 30: Data Structures

31

Amortized analysis of Fixing the tree

Inserted nodes are colored redCase 1 is the only non-terminal case

= #red nodes

2 red + 1 black 1 red + 2 blackNumber of red nodes decreases by 1

In Case 1:

Total fix-up time in n insertions = O(n)

In n insertions Case 1 can be executed at most n times!

Page 31: Data Structures

32

Red-Black trees - Implementation

right

key

left

x parent info

color

Page 32: Data Structures

33

Red-Black trees - Implementation

right

key

left

x parent info

color

Add a dummy root (sentinel)

The real root is the left child of the dummy root

T

The dummy root is black and has key

Page 33: Data Structures

34

Red-Black trees - ImplementationReplace all external leaves

by another sentinel, called NULL

NULL

NULL is a black Tree-Node

Writing to NULL.parent is harmless

T

Page 34: Data Structures

35

Transplant – Replace the subtree of x by the subtree of yReplace – Replace x by y

Basic operations

x may be the (real) root

Page 35: Data Structures

36

Left Rotation

All special cases handled automatically!,, may be NULL. x may be the root.

x

y

x

y

Page 36: Data Structures

37

Page 37: Data Structures

38

Case 1

Case 2

Case 3

p parent

See also CLRSChapter 13

Page 38: Data Structures

39

Deletions from Red-Black trees

Similar in nature to insertions

Slightly more complicated

If the node to be deleted has two children,we again delete its successor from the tree and use it to replace the node to be deleted

Let’s see some examples…

Page 39: Data Structures

40

Delete

Page 40: Data Structures

41

Delete

Page 41: Data Structures

42

Delete

Page 42: Data Structures

43

Delete

Page 43: Data Structures

44

Delete (cont.)

Page 44: Data Structures

45

Delete (cont.)

Page 45: Data Structures

46

Delete (cont.)

Page 46: Data Structures

47

Delete (cont.)

Page 47: Data Structures

48

Delete (cont.)

Page 48: Data Structures

49

Delete (cont.)

Page 49: Data Structures

50

Deleting a node from a Red-Black tree

Remove the node from the tree

If it was red or had a red child we are done

Otherwise we get a node that requires “extra blackness”

Apply a transformation that either moves the “extra blackness” up the tree, or gets rid of it

An “extra blackness” at the root may be removed

Page 50: Data Structures

52

Delete: Case 1

B

A Cx

w

C

B

x

A

x’s sibling w is red

“Extra-blackness” went downThis can only happen once

Page 51: Data Structures

53

Delete: Case 2x’s sibling w is black,

and both children of w are black

B

A Dx

C E

w

B

A D

new x

C E

w

If B is red, it is made black and we are done

Page 52: Data Structures

54

Delete: Case 3x’s sibling w is black,

w’s left child is red, and w’s right child is black

B

A Dx

C E

w

B

A Cx

E

D

new w

Page 53: Data Structures

55

Delete: Case 4x’s sibling w is black,

and w’s right child is red

B

A Dx

C E

w

D

EB

CA

“Extra-blackness” disappeared!

Page 54: Data Structures

56

Finish

Deletions from Red-Black TreeCase 1

All casesabove

Case 2b

Case 3

Case 4

Case 2r

Can only loop in Case 2b

Page 55: Data Structures

57

Delete - analysisFind the successor (if needed) – O(log n) time

Delete – O(1) timeFix the tree – O(log n) time

Fixing the tree:At most 3 rotations

O(1) amortized time

Total time spend on fixing the tree while performing a sequence of n insert and delete operations is O(n)

Page 56: Data Structures

58

Delete + Insert – Non-terminal cases

Delete: Case 2

Insert: Case 1a

= #( ) + 2 #( )

Page 57: Data Structures

59

Joining two Red-Black trees

Suppose that all keys in T1 are less than x.keyand that all keys in T2 are greater than x.key

T1T2

x

Simply joining them would create a very unbalanced tree

Join(T1,x,T2)

Black rule may be violated

Page 58: Data Structures

60

Joining two Red-Black trees

Find a black node z on the left spine of T2 with the same black height as the black height of the root of T1

T1

T2

x

Color x red

y

z

Fix red rule as in insertO(log n) time

Join(T1,x,T2)

O(bh(T2)−bh(T1)+1) time, if bh’s maintained explicitly

Assumebh(T1) ≤ bh(T2)

Page 59: Data Structures

61

a

b

c

d

e

f

x

A

B

C

D

E

F

G

H

e

fA

B

C D

c

a

b

dE

F

GH

x

Splitting

Page 60: Data Structures

62

Balanced and Efficient Splitting

y

x

O(log n) time!

Suppose we join T1 ,T2 ,…,Tk

where bh(T1) bh(T2) … bh(Tk)

Time is (exact argument is a little more complicated)

Page 61: Data Structures

63

Additional dictionary operations

Select(D,i) – Return the i-th largest item in D(indices start from 0)

Rank(D,x) – Return the rank of x in D,(i.e., the number of items x is larger than)

Can we still use Red-Black trees?

Keep sub-tree sizes!

Page 62: Data Structures

64

10

3

1 1 3 2

1

6

x.size = x.left.size + x.right.size + 1

1 1

Page 63: Data Structures

65

Selection

Note: 0 i < n

Page 64: Data Structures

66

a

b

c

d

e

f

x

A

B

C

D

E

F

G

H

Rank

Page 65: Data Structures

67

Rank

Assume NULL.size=0

Page 66: Data Structures

68

Easy to maintain sizesy

x

B

C

A

x

y

B

A

C

Right rotate

a b

c

cb

a

y.size = b.size + c.size + 1x.size = a.size + y.size + 1

Page 67: Data Structures

69

Finger Search trees

Maintain a pointer to the minimum element

T

Select(T,k) in O(log k) time

Page 68: Data Structures

70

Lists as Trees

a

e

b

d f

c

b e

d

c fa0 2

53

2

3

1 4

5 4

0

1

[ a b c d e ]

Page 69: Data Structures

71

Lists as TreesMaintain the items in a tree:i-th item in node of rank i

List-Node Tree-Node

Tree-Nodes have no explicit key(Implicitly maintained ranks play the role of keys)

Retrieve(L,i) Select(L,i)

Select, Insert-Fixup and Delete-Fixupdo not use keys

Page 70: Data Structures

72

Lists as Trees: InsertTo insert a node z in the i-th position, where i<n:

Find the current node of rank i.If it has no left child, make z its left child.

Otherwise, find its predecessor and make z its right child.

To insert a node z in the last position (i=n):Find the last node andmake z its right child

Fix the tree (Insert-Fixup)

Page 71: Data Structures

73

Lists as Trees: Delete

Delete a node z in the same waya node is removed from a search tree

Page 72: Data Structures

74

Implementation of lists

Circular arrays

DoublyLinked

lists

Red-Black Trees

Insert/Delete-FirstInsert/Delete-Last O(1) O(1) O(log n)

Insert/Delete(i) O(i+1) O(i+1) O(log n)

Retrieve(i) O(1) O(i+1) O(log n)

Concat O(n+1) O(1) O(log n)

Page 73: Data Structures

75

AVL trees G.M. Adelson-Velskii and E.M. Landis (1962)

k−1 k−2

kThe depth of two siblings differs by

at most 1

Need only one extra bit per node

Page 74: Data Structures

76

AVL trees G.M. Adelson-Velskii and E.M. Landis (1962)

k−1 k−2

k

Sk – minimal number of nodes in an AVL tree of depth k

Balance maintained through rotations

Page 75: Data Structures

77

Splay Trees (Self-adjusting trees) Sleator and Tarjan (1983)

Do not maintain any balance!

When a node is accessed, splay it to the root

A node is splayed using a sequence of zig-zig and zig-zag steps

Amortized cost of each operation is O(log n)

Total cost of n operation is O(n log n)

Many other amazing properties

Page 76: Data Structures

78

Zig-Zig

z

y

x

A

D

C

B

x

y

z

D

A

B

C

Rotate y-z left, then rotate x-y left

Page 77: Data Structures

79

Zig-Zag

z

A

y

x

B

D

C

Rotate x-y right, then rotate x-z left

x

y

DC

z

BA

Page 78: Data Structures

80

Splaying (example)

i

h

H

g

f

e

d

c

b

a

I

J

G

A

B

C

D

E F

i

h

H

g

f

e

d

a

b

c

I

J

G

A

B

F

E

DC

i

h

H

g

f

a

d e

b

c

I

J

G

A

B F

E

DC

Page 79: Data Structures

81

Splaying (example cont)

i

h

H

g

f

a

d e

b

c

I

J

G

A

B F

E

DC

i

h

H

a

f g

d e

b

c

I

J

G

A

BF

E

DC

f

d

b

c

A

B

E

DC

a

h

i

I JH

g

e

GF

Page 80: Data Structures

82

Splay Trees (Self-adjusting trees) Sleator and Tarjan (1983)

Amortized cost of each operation is O(log n)

Total cost of n operation is O(n log n)

Many other amazing properties

Some intriguing open problems

http://webdiis.unizar.es/asignaturas/EDA/AVLTree/avltree.htmlPlay with them yourself: