Top Banner
CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees
20

CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Jan 14, 2016

Download

Documents

Scott Bailey
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: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

CSC 213 –Large Scale

Programming

Lecture 21:

Red-Black Trees

Page 2: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Today’s Goal

Implement (2,4)-trees with a binary tree Colors nodes red & black Using colors, group Entrys into (2,4) nodes Provides O(log n) time for insert, remove, & find

Page 3: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

(2,4) Trees: Pro & Con

Cons: Cannot use existing BST code

Pros: (2,4) Trees balance without rotations Fewer balancing cases than AVL or splay trees

Just sick, twisted, & wrong: n-node naming scheme is crime against humanity

Page 4: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Red-Black Trees

Binary tree representation of (2,4) tree Red node is part of parent’s node in (2,4) tree Black node is also child of parent’s node in (2,4) tree

As a BST, maximizes reuse of code2 6 73 54

4 6

2 7

5

3

3

5OR

Page 5: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Red-Black Tree Properties

Root Property: Root node is black External Property: Leaves are black Internal Property: Red nodes’ children are black Depth Property: Leaves have identical black depth

Black depth = number of black ancestors a node has

9

154

62 12

7

21

Page 6: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Insertion

Insert Entry into BST as usual If new node is leaf, color it red

If new Node is root, paint it black If node’s parent is red, violates internal property

Must reorganize tree to remove double reddouble red Example: insert(3)

6

83

Page 7: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Insertion

Insert Entry into BST as usual If new node is leaf, color it red

If new Node is root, paint it black

If node’s parent is red, violates internal property Must reorganize tree to remove double reddouble red

Example: insert(4) needs balancing

6

83

4

Page 8: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Remedying Double Red

If aunt of node is red Double redDouble red denotes creation of 5-node Perform recoloring (equivalent to (2,4) tree split)

3 4 6 86

83

4

Page 9: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Recoloring

Remedies double reddouble red when uncle is red Paint parent & uncle black, grandparent red

If grandparent is root, must also paint it black As in splitting, double reddouble red may propagate up

2 4 6 76 7

… 4 …

2

4

6

72

4

6

72

Page 10: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Remedying a Double Red

Otherwise, node’s aunt is black Double redDouble red is poorly balanced 4-node Restructure the 3 nodes like an AVL tree Preserves overall balance of the tree

3 4 66

83

48

Page 11: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Restructuring

Remedies double reddouble red when uncle is black No further propagation -- just an AVL balance

4

6

72

4 6 7

.. 2 ..

4

6

7

2

4 6 7

.. 2 ..

Page 12: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Restructuring (cont.)

Four ways to perform restructuring Differ only in 3 nodes relationships All end with identical result!

4

6

7

7

4

6

7

6

4

4

7

6

4 7

6

Page 13: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Deletion

Start with normal BST deletion If removed Entry or external node’s sibling

red Paint external node’s sibling black

Example: remove(1)6

3 8

41

Page 14: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Deletion

If removed Entry & external node’s sibling are black Paint sibling double blackdouble black This causes violation of internal property

Example: remove(8) causes double blackdouble black

6

3 8

4

Page 15: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Remedying Double BlackDouble Black

Solution depends on sibling Case 1: sibling is black with red child

Restructure nodes like in an AVL tree Case 2: sibling and its children are black

Equal to (2,4) tree underflow; perform recoloring Case 3: sibling is red

adjust so as to represent 3-node better After adjustment, can then apply case 1 or case 2

Page 16: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

(2,4) Tree Transfer

Restructuring double blackdouble black is like transfer

9

6 8 10

8

6 9

9

6 10

8

8

6 9

Page 17: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

(2,4) Tree Fusion

Recoloring double blackdouble black is like fusion

5 9

6 10

5

… 6 9

5

10

9

6

5

9

6

Page 18: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

(2,4) Tree Fusion

Recoloring double blackdouble black is like fusion

4

1 7 1 4

4

1 7

4

1

44

Page 19: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

Adjustment

Adjusting double blackdouble black stalls for time Transforms into situation we can fix

9

5 10

6

9

5

6

Page 20: CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.

For Friday

Friday is BST problem day Bring any questions you may have to answer Will have variety of BST-related problems Gives you last chance to work on these ideas