topic 19 red black trees red black trees
play

Topic 19 Red Black Trees Red Black Trees "People in every - PowerPoint PPT Presentation

Topic 19 Red Black Trees Red Black Trees "People in every direction p y No words exchanged No time to exchange And all the little ants are marching g Red and black antennas waving" - Ants Marching, Dave Matthew's Band


  1. Topic 19 Red Black Trees Red Black Trees "People in every direction p y No words exchanged No time to exchange And all the little ants are marching g Red and black antennas waving" - Ants Marching, Dave Matthew's Band "Welcome to L.A.'s Automated Traffic Surveillance and Control Operations Center. See, they use video feeds from intersections and specifically designed algorithms to predict traffic conditions, and thereby control traffic g g p , y lights. So all I did was come up with my own... kick ass algorithm to sneak in, and now we own the place." -Lyle, the Napster, (Seth Green), The Italian Job y , p , ( ), CS 307 Fundamentals of 1 Computer Science Red Black Trees

  2. Attendance Question 1 8 2000 elements are inserted one at a time into an initially empty binary search tree using the traditional algorithm. What is the maximum possible height of the resulting tree? ? A. 1 B. 11 C 1000 C. 1000 D. 1999 E 4000 E. 4000 CS 307 Fundamentals of 2 Computer Science Red Black Trees

  3. Binary Search Trees 8 Average case and worst case Big O for – insertion – deletion – access 8 Balance is important. Unbalanced trees give worse than log N times for the basic tree g operations 8 Can balance be guaranteed? Can balance be guaranteed? CS 307 Fundamentals of 3 Computer Science Red Black Trees

  4. Red Black Trees 8 A BST with more complex algorithms to ensure balance 8 Each node is labeled as Red or Black. 8 Path: A unique series of links (edges) at u que se es o s (edges) traverses from the root to each node. – The number of edges (links) that must be The number of edges (links) that must be followed is the path length 8 In Red Black trees paths from the root to In Red Black trees paths from the root to elements with 0 or 1 child are of particular interest interest CS 307 Fundamentals of 4 Computer Science Red Black Trees

  5. Paths to Single or Zero Child Nodes Nodes 8 How many? 19 35 12 21 21 16 16 3 56 1 CS 307 Fundamentals of 5 Computer Science Red Black Trees

  6. Red Black Tree Rules 1. Every node is colored either Red or black 2. The root is black 3 If 3. If a node is red its children must d i d it hild t be black. (a.k.a. the red rule) 4. Every path from a node to a null link must contain the same link must contain the same number of black nodes (a.k.a. the path rule) the path rule) CS 307 Fundamentals of 6 Computer Science Red Black Trees

  7. Example of a Red Black Tree 8 The root of a Red Black tree is black 8 Every other node in the tree follows these rules: – Rule 3: If a node is Red, all of its children are Black – Rule 4: The number of Black nodes must be the same in all paths Rule 4: The number of Black nodes must be the same in all paths from the root node to null nodes 19 19 35 12 21 16 3 56 30 CS 307 Fundamentals of 7 Computer Science Red Black Trees

  8. Red Black Tree? 19 35 12 12 50 0 75 -10 135 135 -5 5 100 -8 -6 80 CS 307 Fundamentals of 8 Computer Science Red Black Trees

  9. Attendance Question 2 8 Is the tree on the previous slide a binary search tree? Is it a red black tree? BST? Red-Black? A. No No B. No Yes C. C Yes Yes No No D. Yes Yes CS 307 Fundamentals of 9 Computer Science Red Black Trees

  10. Red Black Tree? 19 35 12 16 16 3 3 0 Perfect? F ll? Full? Complete? CS 307 Fundamentals of 10 Computer Science Red Black Trees

  11. Attendance Question 3 8 Is the tree on the previous slide a binary search tree? Is it a red black tree? BST? Red-Black? A. No No B. No Yes C. C Yes Yes No No D. Yes Yes CS 307 Fundamentals of 11 Computer Science Red Black Trees

  12. Implications of the Rules 8 If a Red node has any children, it must have two children and they must be Black. (Why?) 8 If a Black node has only one child that child must be a Red leaf. (Why?) 8 Due to the rules there are limits on how unbalanced a Red Black tree may become. u ba a ced a ed ac ee ay beco e – on the previous example may we hang a new node off of the leaf node that contains 0? CS 307 Fundamentals of 12 Computer Science Red Black Trees

  13. Properties of Red Black Trees 8 If a Red Black Tree is complete, with all Black nodes except for Red leaves at the lowest level the height will be minimal, ~log N 8 To get the max height for N elements there should be as many Red nodes as possible down one path and all other nodes are Black – This means the max height would be < 2 * log N – see example on next slide p CS 307 Fundamentals of 13 Computer Science Red Black Trees

  14. Max Height Red Black Tree 14 35 35 12 12 21 21 13 13 56 56 1 43 99 15 15 25 25 100 80 70 CS 307 Fundamentals of 14 Computer Science Red Black Trees

  15. Maintaining the Red Black Properties in a Tree Properties in a Tree 8 Insertions 8 Must maintain rules of Red Black Tree. 8 New Node always a leaf New Node always a leaf – can't be black or we will violate rule 4 – therefore the new leaf must be red – therefore the new leaf must be red – If parent is black, done (trivial case) – if parent red, things get interesting because a red if parent red things get interesting because a red leaf with a red parent violates rule 3 CS 307 Fundamentals of 15 Computer Science Red Black Trees

  16. Insertions with Red Parent - Child Must modify tree when insertion would result in Red Parent - Child pair using color changes and Red Parent Child pair using color changes and rotations. 30 70 15 60 20 85 10 80 90 50 65 5 40 55 CS 307 Fundamentals of 16 Computer Science Red Black Trees

  17. Case 1 8 Suppose sibling of parent is Black. – by convention null nodes are black 8 In the previous tree, true if we are inserting a 3 or an 8. – What about inserting a 99? Same case? 8 Let X be the new leaf Node P be its Red Let X be the new leaf Node, P be its Red Parent, S the Black sibling and G, P's and S's parent and X's grandparent S s parent and X s grandparent – What color is G? CS 307 Fundamentals of 17 Computer Science Red Black Trees

  18. Case 1 - The Picture G S P D C E X B A Relative to G X could be an inside or outside node Relative to G, X could be an inside or outside node. Outside -> left left or right right moves Inside -> left right or right left moves g g CS 307 Fundamentals of 18 Computer Science Red Black Trees

  19. Fixing the Problem G S P D C E X B A If X is an outside node a single rotation between P and G fixes the problem. p A rotation is an exchange of roles between a parent and child node. So P becomes G's parent. Also must recolor P and G. l P d G CS 307 Fundamentals of 19 Computer Science Red Black Trees

  20. Single Rotation P G X C A B S E D Apparent rule violation? pp CS 307 Fundamentals of 20 Computer Science Red Black Trees

  21. Case 2 8 What if X is an inside node relative to G? – a single rotation will not work 8 Must perform a double rotation – rotate X and P G – rotate X and G S S P P D E E A A X X C B CS 307 Fundamentals of 21 Computer Science Red Black Trees

  22. After Double Rotation X G P C C A A B B S E D Apparent rule violation? CS 307 Fundamentals of 22 Computer Science Red Black Trees

  23. Case 3 Sibling is Red, not Black Sibli i R d t Bl k G S S P P D E C C X X A A B B Any problems? CS 307 Fundamentals of 23 Computer Science Red Black Trees

  24. Fixing Tree when S is Red 8 Must perform single rotation between parent, P and grandparent, G, and then make appropriate color changes P G X C S B A D E CS 307 Fundamentals of 24 Computer Science Red Black Trees

  25. More on Insert 8 P 8 Problem: What if on the previous example bl Wh t if th i l G's parent had been red? 8 Easier to never let Case 3 ever occur! 8 E i t l t C 3 ! 8 On the way down the tree, if we see a node X that has 2 Red children we make X Red and its two has 2 Red children, we make X Red and its two children black. – if recolor the root, recolor it to black – the number of black nodes on paths below X remains unchanged – If X s parent was Red then we have introduced 2 If X's parent was Red then we have introduced 2 consecutive Red nodes.(violation of rule) – to fix, apply rotations to the tree, same as inserting node CS 307 Fundamentals of 25 Computer Science Red Black Trees

  26. Example of Inserting Sorted Numbers 8 1 2 3 4 5 6 7 8 9 10 Insert 1. A leaf so red. Realize it is 1 root so recolor to black. 1 CS 307 Fundamentals of 26 Computer Science Red Black Trees

  27. Insert 2 make 2 red. Parent 1 is black so done is black so done. 2 CS 307 Fundamentals of 27 Computer Science Red Black Trees

  28. Insert 3 1 Insert 3. Parent is red. Insert 3. Parent is red. Parent's sibling is black 2 (null) 3 is outside relative to grandparent. Rotate 3 parent and grandparent 2 3 1 CS 307 Fundamentals of 28 Computer Science Red Black Trees

  29. Insert 4 On way down see 2 with 2 red children. 2 2 Recolor 2 red and children black. 3 1 1 Realize 2 is root Realize 2 is root so color back to black 2 When adding 4 parent is black parent is black 3 3 1 1 so done. 4 4 CS 307 Fundamentals of 29 Computer Science Red Black Trees

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend