introduction to algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046jfall... · 2019. 9....

40
Introduction to Algorithms 6.046J/18.401J Lecture 9 Prof. Piotr Indyk

Upload: others

Post on 12-Sep-2021

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Introduction to Algorithms6.046J/18.401J

Lecture 9Prof. Piotr Indyk

Page 2: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Today

• or how to avoid this Balanced search trees,

even in the worst case 1

2 3

4 5

6

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.2

Page 3: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

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

• AVL trees

Examples: • 2-3 trees • 2-3-4 trees • B-trees • Red-black trees

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.3

Page 4: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Red-black trees

BSTs with an extra one-bit color field in each node. Red-black properties: 1. Every node is either red or black. 2. The root and leaves (NIL’s) are black. 3. If a node is red, then its parent is black.4. All simple paths from any node x to a

descendant leaf have the same number of black nodes.

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.4

Page 5: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Example of a red-black tree

88 1111

1010

1818

2626

2222

33

77

NIL NIL

NIL

NIL NIL NIL NIL NIL NIL

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.5

Page 6: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Use of red-black trees

• What properties would we like to prove about red-black trees ? – They always have O(log n) height– There is an O(log n)–time insertion

procedure which preserves the red-black properties

• Is it true that, after we add a new element to a tree (as in the previous lecture), we can always recolor the tree to keep it red-black ?

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.6

Page 7: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Example of a red-black tree

88 1111

1010

1818

2626

2222

33

77

7.57.5

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.7

Page 8: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Use of red-black trees

• What properties would we like to prove about red-black trees ?– They always have O(log n) height– There is an O(log n)–time insertion procedure

which preserves the red-black properties • Is it true that, after we add a new element to a tree (as

in the previous lecture), we can always recolor thetree to keep it red-black ?

• NO • After insertions, sometimes we need to juggle nodes

around

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.8

Page 9: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Rotations

AABB

αα ββ γγ

RIGHT-ROTATE(B)

BBAA

γγββ αα

LEFT-ROTATE(A)

Rotations maintain the inorder ordering of keys:• a ∈ α, b ∈ β, c ∈ γ ⇒ a ≤ A ≤ b ≤ B ≤ c.A rotation can be performed in O(1) time.

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.9

Page 10: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Rotations can reduce height

B A

A

1 2

31

LEFT-ROTATE(A) B2 3 γ γ

AA

BB

αα ββ γγ

BB

AA

γγββ αα

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.10

Page 11: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Red-black tree wrap-up

• Can show how

– O(log n) re-colorings

– 1 rotation

can restore red-black properties after an insertion

• Instead, we will see 2-3 trees (but will come back to red-black trees at the end)

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.11

Page 12: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

2-3 Trees

• The simplest balanced trees on the planet! • Although a little bit more wasteful

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.12

Page 13: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

2-3 Trees

• either 2 or 3

••

depth • Leaves are sorted

Degree of each node is

Keys are in the leaves All leaves have equal

9 1251 6 7 8

6 8 12

12

• Each node x contains maximum key in thesub-tree, denoted x.max

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.13

Page 14: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Internal nodes

• Internal nodes:– Values:

• x.max: maximum key in the sub-tree – Pointers:

• left[x] • mid[x] • right[x] : can be null • p[x] : can be null for the root • …

• Leaves: – x.max : the key

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.14

Page 15: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Height of 2-3 tree

• What is the maximum height h of a 2-3 tree with n nodes ?

• Alternatively, what is the minimum number of nodes in a 2-3 tree of height h ?

• It is 1+2+22+23+…+2h =2h+1-1 • n ≥ 2h+1-1 ⇒ h = O(log n) • Full binary tree is the worst-case example!

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.15

Page 16: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Searching

• How can we search for a key k ?

Search(x,k): • If x=NIL then return NIL • Else if x is a leaf then

– If x.max=k then return x – Else return NIL

• Else 9 1251 6 7 8

6 8 12

12

– If k ≤ left[x].max then Search(left[x],k)

– Else if k ≤ mid[x].max Search(8)then Search(mid[x],k) Search(13)

– Else Search(right[x],k)

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.16

Page 17: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

12

Insertion

• How to insert x ? • Perform Search for the

key of x• Let y be the last internal

node • Insert x into y in a

sorted order• At the end, update the

max values on the pathto root

9 1251 6 7 8

6 8

7.55.5

13

13

Insert(7.5)(continued on the next Insert(13)slide) Insert(5.5)

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.17

13

Page 18: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

12

Insertion, ctd.

(continued from the previous slide)

• If y has 4 children, then Split(y)

x

9 1251 6 7 8

6 8

7.55.5

13

13

y

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.18

13

Page 19: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Split

• Split y into two nodes y1, y2

• Both are linked to *z=parent(y)

• If z has 4 children, split z

*If y is a root, then create new parent(y)=new root

ba d

y

c

ba d

y1

c

y2

z

z

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.19

Page 20: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

12

Split

9 1251 6 7 8

6 8

5.5

13

13

7.5 13

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.20

Page 21: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

12

Split

9 1251 6 7 8

5.5 8

5.5

13

13

6

7.5 13

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.21

Page 22: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

Split

9 1251 6 7 8

5.5 8

5.5

13

6

6

13

13

7.5 13 • Insert and Split preserve heights, unless new root is created, in which case all heights are

increased by 1• After Split, all nodes have 2 or 3 children • Everything takes O(log n) time

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.22

Page 23: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

Delete

• How to delete x ? • Let y=p(x) • Remove x from y • If y has 1 child:

– Remove y 9 1251 6 7 8

5.5 8

5.5

12

6

6

12

12

y z

– Attach x to y’s sibling z x

Delete(8)

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.23

Page 24: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

Delete

• How to delete x ? • Let y=p(x) • Remove x from y • If y has 1 child:

– Remove y 9 1251 6 7

5.5

5.5

12

6

6

12

12

z

– Attach x to y’s sibling z • If z has 4 children, then

Split(z) Delete(8)INCOMPLETE – SEE THE END FOR FULL VERSION© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.24

Page 25: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Summing up

• 2-3 Trees: – O(log n) depth ⇒ Search in O(log n) – Insert, Delete (and Split) in O(log n)

• We will now see 2-3-4 trees – Same idea, but:

• Each parent has 2,3 or 4 children • Keys in the inner nodes • More complicated procedures

timetime

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.25

Page 26: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

2-3-4 Trees

5 9

1 2 4 7 8 10 12

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.26

Page 27: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Height of a red-black tree

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

INTUITION:• Merge red nodes

into their black parents.

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.27

Page 28: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Height of a red-black tree

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

INTUITION:• Merge red nodes

into their black parents.

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.28

Page 29: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Height of a red-black tree

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

INTUITION:• Merge red nodes

into their black parents.

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.29

Page 30: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Height of a red-black tree

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

INTUITION:• Merge red nodes

into their black parents.

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.30

Page 31: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Height of a red-black tree

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

INTUITION:• Merge red nodes

into their black parents.

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.31

Page 32: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Height of a red-black tree

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

INTUITION:• Merge red nodes

into their black parents.

h′

• 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. © Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.32

Page 33: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Summing up

• We have seen: – Red-black trees – 2-3 trees (in detail) – 2-3-4 trees

• Red-black trees are undercover 2-3-4 trees • In most cases, does not matter what you use

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.33

Page 34: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

2-3 Trees: Deletions

• Problem: there is an internal node that has only 1 child

9 1251 6 7

5.5

5.5

12

6

6

12

12

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.34

Page 35: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Full procedure for Delete(x)

• Special case: x is the only element in the tree: delete everything

x NIL• Not-so-special case: x is one of two elements

in the tree. In this case, the procedure on thenext slide will delete x

x y

y

• Both NIL and y are special 2-3 trees © Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.35

Page 36: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

Procedure for Delete(x)• Let y=p(x) • Remove x • If y≠root then

– Let z be the sibling of y. – Assume z is the right sibling of y, otherwise the code is

symmetric.– If y has only 1 child w left

Case 1: z has 3 children • Attach left[z] as the rightmost child of y • Update y.max and z.max Case 2: z has 2 children: • Attach the child w of y as the leftmost child of z • Update z.max • Delete(y) (recursively*)

– Else • Update max of y, p(y), p(p(y)) and so on until root

• Else – If root has only one child u

• Remove root • Make u the new root

*Note that the input of Delete does not have to be a leaf

x

zy

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.36

Page 37: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

Example

9 1251 6 7 8

5.5 8

5.5

12

6

6

12

12

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.37

Page 38: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

Example, ctd.

9 1251 6 7

5.5 8

5.5

12

6

6

12

12

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.38

Page 39: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

Example, ctd.

9 1251 6 7

5.5

5.5

12

12

6

12

12

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.39

Page 40: Introduction to Algorithmsdspace.mit.edu/bitstream/handle/1721.1/37150/6-046JFall... · 2019. 9. 13. · Red-black trees BSTs with an extra one-bit color field in each node. Red-black

12

Example, ctd.

9 1251 6 7

5.5

5.5

12

12

6

© Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.40