csc 213 – large scale programming lecture 21: red-black trees

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

Upload: scott-bailey

Post on 14-Jan-2016

214 views

Category:

Documents


0 download

TRANSCRIPT

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

(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

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

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

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

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

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

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

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

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 ..

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

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

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

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

(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

(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

(2,4) Tree Fusion

Recoloring double blackdouble black is like fusion

4

1 7 1 4

4

1 7

4

1

44

Adjustment

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

9

5 10

6

9

5

6

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