Clicker 1 Topic 23 Red Black Trees 2000 elements are inserted one at a time into an initially empty binary search tree "People in every direction using the traditional, naive algorithm. What is No words exchanged No time to exchange the maximum possible height of the resulting And all the little ants are marching tree? Red and Black antennas waving" - Ants Marching, Dave Matthew's Band A. 1 "Welcome to L.A.'s Automated Traffic Surveillance and Control Operations B. 11 Center. See, they use video feeds from intersections and specifically C. 21 designed algorithms to predict traffic conditions, and thereby control traffic lights. So all I did was come up with my own... kick ass algorithm to D. 500 sneak in, and now we own the place." -Lyle, the Napster, (Seth Green), The Italian Job E. 1999 CS314 2 Red Black Trees Binary Search Trees Red Black Trees Average case and worst case Big O for A BST with more complex algorithms to ensure balance insertion deletion Each node is labeled as Red or Black. access Path: A unique series of links (edges) Balance is important. Unbalanced trees give traverses from the root to each node. worse than log N times for the basic tree The number of edges (links) that must be operations followed is the path length In Red Black trees paths from the root to Can balance be guaranteed? elements with 0 or 1 child are of particular interest CS314 3 CS314 4 Red Black Trees Red Black Trees
Paths to Single or Zero Child Red Black Tree Rules Nodes 1. Every node is colored either red or black How many? 2. The root is black 19 3. If a node is red its children must 12 35 be black. (a.k.a. the red rule) 21 16 3 56 4. Every path from a node to a null link must contain the same 1 number of black nodes (a.k.a. the path rule) CS314 5 CS314 6 Red Black Trees Red Black Trees Example of a Red Black Tree Red Black Tree? 19 The root of a Red Black tree is black 35 12 Every other node in the tree follows these rules: 50 Rule 3: If a node is Red, all of its children are Black 0 Rule 4: The number of Black nodes must be the same in all paths 75 from the root node to null nodes -10 135 19 -5 12 35 21 16 3 56 30 CS314 7 CS314 8 Red Black Trees Red Black Trees
Clicker 2 Red Black Tree? Is the tree on the previous slide a binary 19 search tree? Is it a red black tree? BST? Red-Black? 35 12 A. No No 16 3 B. No Yes 0 C. Yes No D. Yes Yes Perfect? Full? Complete? CS314 9 CS314 10 Red Black Trees Red Black Trees Clicker 3 Implications of the Rules Is the tree on the previous slide a binary If a Red node has any children, it must have search tree? Is it a red black tree? two children and they must be Black. (Why?) BST? Red-Black? If a Black node has only one child that child A. No No must be a Red leaf. (Why?) B. No Yes Due to the rules there are limits on how unbalanced a Red Black tree may become. C. Yes No on the previous example may we hang a new D. Yes Yes node off of the leaf node that contains 0? CS314 11 CS314 12 Red Black Trees Red Black Trees
Properties of Red Black Trees Max Height Red Black Tree If a Red Black Tree is complete, with all 14 Black nodes except for Red leaves at the 12 35 lowest level the height will be minimal, ~log N To get the max height for N elements there 21 13 56 1 should be as many Red nodes as possible down one path and all other nodes are Black 43 99 15 25 This means the max height would b approximately 2 * log N (don't use this as a formula) 80 100 typically less than this 14 nodes, height 5 see example on next slide 70 interesting exercise, draw max height tree with N nodes CS314 14 Red Black Trees Maintaining the Red Black Insertions with Red Parent - Child Properties in a Tree Must modify tree when insertion would result in Insertions Red Parent - Child pair using color changes and Must maintain rules of Red Black Tree. rotations. 30 New Node always a leaf can't be black or we will violate rule 4 70 15 therefore the new leaf must be red 60 20 85 If parent is black, done (trivial case) 10 if parent red, things get interesting because a red 90 80 50 65 leaf with a red parent violates rule 3 5 40 55 CS314 15 CS314 16 Red Black Trees Red Black Trees
Case 1 Case 1 - The Picture Suppose sibling of parent is Black. G by convention null nodes are black In the previous tree, true if we are inserting a S P 3 or an 8. D C E What about inserting a 99? Same case? X Let X be the new leaf Node, P be its Red B A Parent, S the Black sibling and G, P's and S's parent and X's grandparent Relative to G, X could be an inside or outside node. Outside -> left left or right right moves What color is G? Inside -> left right or right left moves CS314 17 CS314 18 Red Black Trees Red Black Trees Fixing the Problem Single Rotation P G X G S P C A B S D C E X E B A D If X is an outside node a single Apparent rule violation? rotation between P and G fixes the problem. Recall, S is null if X is a leaf, so no problem A rotation is an exchange of roles between a parent If this occurs higher in the tree (why?) subtrees A, B, and child node. So P becomes G's parent. Also must and C will have one more black node than D and E. recolor P and G. CS314 19 CS314 20 Red Black Trees Red Black Trees
Case 2 First Rotation What if X is an inside node relative to G? Rotate P and X, no color change a single rotation will not work G Must perform a double rotation rotate X and P S X G rotate X and G P C D E S P B A D E A X C B What does this actually do? CS314 21 CS314 22 Red Black Trees Red Black Trees After Double Rotation Case 3 Sibling is Red, not Black X P G G S P C A B S D E C X E D A B Any problems? CS314 23 CS314 24 Red Black Trees Red Black Trees
Fixing Tree when S is Red More on Insert Problem: What if on the previous example Must perform single rotation between parent, G's parent (GG!) had been red? P and grandparent, G, and then make Easier to never let Case 3 ever occur! appropriate color changes On the way down the tree, if we see a node X that P has 2 Red children, we make X Red and its two children black. X G if recolor the root, recolor it to black the number of black nodes on paths below X remains C S B unchanged A If X's parent was Red then we have introduced 2 consecutive Red nodes.(violation of rule) D E to fix, apply rotations to the tree, same as inserting node CS314 25 CS314 26 Red Black Trees Red Black Trees Insert 2 Example of Inserting Sorted Numbers 1 2 3 4 5 6 7 8 9 10 make 2 red. Parent 1 Insert 1. A leaf so is black so done. red. Realize it is 1 2 root so recolor to black. 1 CS314 27 CS314 28 Red Black Trees Red Black Trees
Insert 3 Insert 4 On way down see 2 with 2 red children. 1 Insert 3. Parent is red. 2 Recolor 2 red and Parent's sibling is black children black. (null) 3 is outside relative 2 2 3 1 to grandparent. Rotate 3 parent and grandparent 3 1 2 2 When adding 4 parent is black 3 1 3 1 so done. 4 Set root to black! CS314 29 CS314 30 Red Black Trees Red Black Trees Insert 5 Finish insert of 5 5's parent is red. 2 2 Parent's sibling is black (null). 5 is 3 4 1 1 outside relative to grandparent (3) so rotate 4 3 5 parent and grandparent then recolor 5 CS314 31 CS314 32 Red Black Trees Red Black Trees
Insert 6 Finishing insert of 6 On way down see 2 2 6's parent is black 4 with 2 red children. Make so done. 4 4 4 red and children 1 1 black. 4's parent is black so no problem. 3 3 5 5 6 CS314 33 CS314 34 Red Black Trees Red Black Trees Insert 7 Finish insert of 7 2 2 7's parent is red. Parent's sibling is 4 4 black (null). 7 is 1 1 outside relative to grandparent (5) so 3 3 5 6 rotate parent and grandparent then recolor 6 7 5 7 CS314 35 CS314 36 Red Black Trees Red Black Trees
Insert 8 Still Inserting 8 The caveat!!! 2 2 Recolored now On way down see 6 need to 4 1 4 1 with 2 red children. rotate. Make 6 red and 3 children black. This 6 3 6 Recall, the subtrees creates a problem and the one extra because 6's parent, 4, is 7 5 7 5 black node. also red. Must perform rotation. CS314 37 CS314 38 Red Black Trees Red Black Trees Finish inserting 8 Insert 9 4 4 Recolored now need to 2 2 6 6 rotate 7 7 1 3 1 3 5 5 On way down see 4 has two red children 8 8 so recolor 4 red and children black. Realize 4 is the root so recolor black 9 CS314 39 CS314 40 Red Black Trees Red Black Trees
Finish Inserting 9 Insert 10 4 4 2 2 6 6 8 8 1 3 1 3 5 5 On way down see 8 has two 7 9 7 9 After rotations and recoloring red children so change 8 to red and children black 10 CS314 41 CS314 42 Red Black Trees Red Black Trees Insert 11 Finish inserting 11 4 4 2 2 6 6 8 8 1 3 1 3 5 5 7 9 7 10 Again a rotation is 11 10 9 needed. 11 CS314 43 CS314 44 Red Black Trees Red Black Trees
Recommend
More recommend