Download presentation

Presentation is loading. Please wait.

Published byEzra Wisdom Modified about 1 year ago

1
CSC 213 – Large Scale Programming

2
Red-Black Tree Properties black Root Property: Root node painted black black External Property: Leaves are painted black black Internal Property: Red nodes’ children are black black Depth Property: Leaves have identical black depth black Number of black ancestors for the node

3
Insertion Begins as BST insertion (just like splay & AVL) New node’s initial color set by where it is black If node is root, paint it black Other nodes colored red when insertion completes

4
Insertion Begins as BST insertion (just like splay & AVL) New node’s initial color set by where it is black If node is root, paint it black Other nodes colored red when insertion completes Example: insert(3) 6 8

5
Insertion Begins as BST insertion (just like splay & AVL) New node’s initial color set by where it is black If node is root, paint it black Other nodes colored red when insertion completes Example: insert(3) 6 83

6
Insertion Begins as BST insertion (just like splay & AVL) New node’s initial color set by where it is black If node is root, paint it black Other nodes colored red when insertion completes Example: insert(3) 6 83

7
Insertion New node’s initial color set by where it is black If node is root, paint it black Other nodes colored red when insertion completes If node’s parent is red, violates internal property double red Must reorganize tree to remove double red Example: insert(4)

8
Insertion New node’s initial color set by where it is black If node is root, paint it black Other nodes colored red when insertion completes If node’s parent is red, violates internal property double red Must reorganize tree to remove double red Example: insert(4) makes tree unbalanced

9
Double Red Double Red With Red Aunt Double red Double red represents creation of 5-node Perform recoloring to find if balance really off

10
Double Red Double Red With Red Aunt black Parent & uncle painted black, grandparent red black When grandparent is root, must paint it black When easier, promote 2nd Entry to parent double red Must continue check, double red can propagate

11
Double Red Double Red With Red Aunt black Parent & uncle painted black, grandparent red black When grandparent is root, must paint it black When easier, promote 2nd Entry to parent double red Must continue check, double red can propagate

12
Double Red Double Red With Red Aunt black Parent & uncle painted black, grandparent red black When grandparent is root, must paint it black When easier, promote 2nd Entry to parent double red Must continue check, double red can propagate

13
Double Red Black Double Red With Black Aunt double red Poorly balanced tree causes this double red Restore tree balance to use AVL tree restructuring Preserves overall balance of the tree

14
Double Red Black Double Red With Black Aunt Rebalance tree using AVL tree restructuring Recolors nodes also, but changes are very small

15
Double Red Black Double Red With Black Aunt Rebalance tree using AVL tree restructuring Recolors nodes also, but changes are very small

16
Double Red Double Red Restructuring 4 different restructures needed to remedy Differ in how node, parent, & grandparent related Identical result no matter where we start

17
Double Red Double Red Restructuring 4 different restructures needed to remedy Differ in how node, parent, & grandparent related Identical result no matter where we start

18
Deletion Start with normal BST deletion If Entry in red node or leafs’s sibling red black Leaf’s sibling is painted black Example: remove(1)

19
Deletion Start with normal BST deletion If Entry in red node or leafs’s sibling red black Leaf’s sibling is painted black Example: remove(1)

20
Deletion Start with normal BST deletion If Entry in red node or leafs’s sibling red black Leaf’s sibling is painted black Example: remove(1)

21
Deletion Start with normal BST deletion If Entry in red node or leafs’s sibling red black Leaf’s sibling is painted black Example: remove(1)

22
Blacker Black What’s Blacker Than Black? black If removed Entry & leaf’s sibling already black double black Paint sibling double black This is an illegal state – violates internal property Example: remove(8)

23
Blacker Black What’s Blacker Than Black? black If removed Entry & leaf’s sibling already black double black Paint sibling double black This is an illegal state – violates internal property Example: remove(8)

24
Blacker Black What’s Blacker Than Black? black If removed Entry & leaf’s sibling already black double black Paint sibling double black This is an illegal state – violates internal property Example: remove(8) 6 3 4

25
Blacker Black What’s Blacker Than Black? black If removed Entry & leaf’s sibling already black double black Paint sibling double black This is an illegal state – violates internal property double black Example: remove(8) causes double black 6 3 4

26
Double Black Remedying Double Black black Case 1: sibling is black with red child Reorder nodes using AVL tree restructure black Case 2: sibling and its children are black Equal to (2,4) tree underflow, so recolor nodes

27
Double Black Remedying Double Black black Case 1: sibling is black with red child Reorder nodes using AVL tree restructure black Case 2: sibling and its children are black Just a bad balance, so just recolor nodes Case 3: sibling is red Adjust subtree to better balance tree Once complete apply case 1 or case 2

28
Black Black Sibling With Red Niece double black Solve double black using AVL restructuring

29
Black Black Sibling With Red Niece double black Solve double black using AVL restructuring 9 6 8

30
Black Black Sibling With Red Niece double black Solve double black using AVL restructuring

31
Black Sibling & Children are Black double black Solve double black recoloring parent & sibling …

32
Black Sibling & Children are Black double black Solve double black recoloring parent & sibling …

33
Black Sibling & Children are Black double black Solve double black recoloring parent & sibling … …

34
Black Sibling & Children are Black double black Solve double black recoloring parent & sibling … …

35
Sibling is Red double black Adjusting double black stalls for time Transforms situation into something we can fix

36
Sibling is Red double black Adjusting double black stalls for time Transforms situation into something we can fix

37
Sibling is Red double black Adjusting double black stalls for time Transforms situation into something we can fix 9 5 4

38
Sibling is Red double black Adjusting double black stalls for time Transforms situation into something we can fix 9 5 4

39
Sibling is Red double black Adjusting double black stalls for time Transforms situation into something we can fix double black Once completed, re-examine double black node

40
Do the Activity

41
For Next Lecture Weekly assignment available to test skills Due at regular time Tuesday after break Talk to me if struggling on problems Last idea from Map / Dictionary after break What are Set s? Why are we getting to them now? It may not appear related, but how are they used? Reminder: lab phase #2 due Monday after break

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google