Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf ·...

23
1 Red-Black Trees Chapters 13 10/21/13 CS380 Algorithm Design and Analysis

Transcript of Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf ·...

Page 1: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

1

Red-Black Trees

Chapters 13

10/21/13 CS380 Algorithm Design and Analysis

Page 2: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

2

Binary Search Trees Review

Page 3: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

3

Balanced Trees

• Why do we want to balance trees?

• Red-Black Trees are an example of balanced trees

• Other balanced trees:o AVL trees

o B-trees

o 2-3 trees

10/21/13 CS380 Algorithm Design and Analysis

Page 4: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

4

Red-Black Tree

• BST data structure with extra color field for each node, satisfying the red-black properties:

1. Every node is either red or black.

2. The root is black.

3. Every leaf is black.

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

5. Every path from node to descendent leaf contain the same number of black nodes.

10/21/13 CS380 Algorithm Design and Analysis

Page 5: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

5

Example

• Attributes of nodes:o key

o left

o right

o p (parent)

o color

• Note the use of the sentinel T.nilo Parent of the root is T.nil

o All leaves are T.nil – no data in the leaves!10/21/13 CS380 Algorithm Design and Analysis

Page 6: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

6

Properties of RB-Trees

• Black-height of a node:o Number of black nodes on any simple path from,

but not including, a node x down to a leaf

• A red-black tree with n internal nodes has height at most 2lg(n+1)

10/21/13 CS380 Algorithm Design and Analysis

Page 7: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

7

Rotations

• Why are rotations necessary in red-black trees?

• How are rotations performed?

• What is the running time of rotation?

10/21/13 CS380 Algorithm Design and Analysis

Page 8: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

8

Example

• Color this tree

• Insert 8

• Insert 11

• Insert 10

10/21/13 CS380 Algorithm Design and Analysis

7

5 9

1212

5 9

7

Properties of RB-Trees1. Every node is either red or black.2. The root is black.3. Every leaf is black.4. If a node is red, both children are black.5. Every path from node to descendent leaf contain the same number of

black nodes.

Page 9: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

9

Left-Rotate

10/21/13 CS380 Algorithm Design and Analysis

Page 10: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

10

Example

• Rotate left about 9

10/21/13 CS380 Algorithm Design and Analysis

12

5 9

7

8

11

Page 11: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

11

Inserting into a RB-Tree

10/21/13 CS380 Algorithm Design and Analysis

• This is regular binary search tree insertion

• Which RB-Tree property could have been violated?

Properties of RB-Trees1. Every node is either red or black.2. The root is black.3. Every leaf is black.4. If a node is red, both children are black.5. Every path from node to descendent leaf

contain the same number of black nodes.

Page 12: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

12

RB-Insert-Fixup

10/21/13 CS380 Algorithm Design and Analysis

Page 13: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

13

Cases

10/21/13 CS380 Algorithm Design and Analysis

Page 14: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

14

Cases

10/21/13 CS380 Algorithm Design and Analysis

Page 15: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

15

Example

• Insert 10

10/21/13 CS380 Algorithm Design and Analysis

12

5 9

7

8

11

10

Page 16: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

16

Example

• Insert 15

10/21/13 CS380 Algorithm Design and Analysis

22

3 18

7

10

11

15

826

Page 17: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

17

Delete BST, p 295 - 298

• BST:delete x, 3 cases:o x has no children

o x has 1 child

o x has 2 children

• helper function transplant(T, u, v)o

Page 18: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

18

p 295 p 323

BST- Transplant(T, u, v)

1 if u.p == NIL

2 T.root = v

3 elseif u == u.p.left

4 u.p.left = v

5 else u.p.right = v

6 if v != Nil

7 v.p = u.p

RB-Transplant(T, u, v)

1 if u.p == T.NIL

2 T.root = v

3 elseif u == u.p.left

4 u.p.left = v

5 else u.p.right = v

6 v.p = u.p

Page 19: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

19

Delete, RB Tree, p 323

• RB-Transplant

• RB-Delete

• RB-DeleteFixup

Page 20: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

p324

Page 21: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

p326

Page 22: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

22

Examples

10/21/13 CS380 Algorithm Design and Analysis

22

3 18

7

10

11826

Delete each of these from the original

Delete 26 Delete 22

Delete 10

Delete 18

Delete 3

Page 23: Red-Black Trees - Pacific Universityzeus.cs.pacificu.edu/chadd/cs380f13/Lectures/08Lecture.pdf · Red-Black Tree •BST data structure with extra color field for each node, satisfying

23

Notes