1 / 41

Lecture 22: The Rolling Stones, Masters of the Balanced Tree

CSC 213 – Large Scale Programming. Lecture 22: The Rolling Stones, Masters of the Balanced Tree. Red-Black Tree Properties. Root Property: Root node painted black External Property: Leaves are painted black Internal Property: Red node s’ children are black

donoma
Download Presentation

Lecture 22: The Rolling Stones, Masters of the Balanced Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 213 – Large Scale Programming Lecture 22:The Rolling Stones, Masters of the Balanced Tree

  2. Red-Black Tree Properties • Root Property:Root node painted black • External Property: Leaves are painted black • Internal Property: Red nodes’ children are black • Depth Property: Leaves have identical blackdepth • Number of blackancestorsfor the node 9 4 15 21 2 6 12 7

  3. Insertion • Begins as BST insertion (just like splay & AVL) • New node’s initial color set by where it is • 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 • 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 • If node is root, paint it black • Other nodes colored red when insertion completes • Example: insert(3) 6 3 8

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

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

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

  9. Double Red With Red Aunt • Double red represents creation of 5-node • Perform recoloringto find if balance really off 6 8 3 4

  10. Double Red With Red Aunt • Parent & uncle painted black, grandparent red • When grandparent is root, must paint it black • When easier, promote 2nd Entryto parent • Must continue check, double red can propagate 4 7 2 6

  11. Double Red With Red Aunt • Parent & uncle painted black, grandparent red • When grandparent is root, must paint it black • When easier, promote 2nd Entryto parent • Must continue check, double red can propagate 4 4 7 7 2 2 6 6

  12. Double Red With Red Aunt • Parent & uncle painted black, grandparent red • When grandparent is root, must paint it black • When easier, promote 2nd Entryto parent • Must continue check, double red can propagate 4 4 7 7 2 2 6 6

  13. Double Red With BlackAunt • Poorly balanced tree causes this double red • Restore tree balance to use AVL tree restructuring • Preserves overall balance of the tree 6 8 3 4

  14. Double Red With BlackAunt • Rebalance tree using AVL tree restructuring • Recolors nodes also, but changes are very small 4 7 2 6

  15. Double Red With BlackAunt • Rebalance tree using AVL tree restructuring • Recolors nodes also, but changes are very small 6 4 7 7 2 4 2 6

  16. Double Red Restructuring • 4 different restructuresneeded to remedy • Differ in how node, parent, & grandparent related • Identical result no matter where we start 7 4 7 4 7 4 6 6 6 6 4 7

  17. Double Red Restructuring • 4 different restructuresneeded to remedy • Differ in how node, parent, & grandparent related • Identical result no matter where we start 7 4 7 4 7 4 6 6 6 6 4 7 6 4 7

  18. Deletion • Start with normal BST deletion • If Entry inred node or leafs’ssibling red • Leaf’s sibling is paintedblack • Example: remove(1) 6 8 3 1 4

  19. Deletion • Start with normal BST deletion • If Entry inred node or leafs’ssibling red • Leaf’s sibling is paintedblack • Example: remove(1) 6 8 3 1 4

  20. Deletion • Start with normal BST deletion • If Entry inred node or leafs’ssibling red • Leaf’s sibling is paintedblack • Example: remove(1) 6 8 3 4

  21. Deletion • Start with normal BST deletion • If Entry inred node or leafs’ssibling red • Leaf’s sibling is paintedblack • Example: remove(1) 6 8 3 4

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

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

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

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

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

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

  28. Black Sibling With Red Niece • Solve double black using AVL restructuring 9 10 6 8

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

  30. Black Sibling With Red Niece • Solve double black using AVL restructuring 8 9 9 6 6 8

  31. Sibling & Children are Black • Solve double black recoloring parent & sibling 5 9 … 6 10

  32. Sibling & Children are Black • Solve double black recoloring parent & sibling 5 9 … 6

  33. Sibling & Children are Black • Solve double black recoloring parent & sibling 5 5 9 9 … … 6 6

  34. Sibling & Children are Black • Solve double black recoloring parent & sibling 5 5 9 9 … … 6 6

  35. Sibling is Red • Adjustingdouble blackstalls for time • Transforms situation into something we can fix 9 10 5 4

  36. Sibling is Red • Adjustingdouble blackstalls for time • Transforms situation into something we can fix 9 10 5 4

  37. Sibling is Red • Adjustingdouble blackstalls for time • Transforms situation into something we can fix 9 5 4

  38. Sibling is Red • Adjustingdouble blackstalls for time • Transforms situation into something we can fix 9 5 4

  39. Sibling is Red • Adjustingdouble blackstalls for time • Transforms situation into something we can fix • Once completed, re-examine double blacknode 9 5 5 4 9 4

  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 Sets? 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

More Related