attendance question 1
play

Attendance Question 1 Red Black Trees Red Black Trees 2000 - PowerPoint PPT Presentation

Topic 19 Attendance Question 1 Red Black Trees Red Black Trees 2000 elements are inserted one at a time into an initially empty binary search tree "People in every direction p y using the traditional algorithm. What is the No words


  1. Topic 19 Attendance Question 1 Red Black Trees Red Black Trees � 2000 elements are inserted one at a time into an initially empty binary search tree "People in every direction p y using the traditional algorithm. What is the No words exchanged No time to exchange maximum possible height of the resulting And all the little ants are marching g 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 designed algorithms to predict traffic conditions, and thereby control traffic g g p , y C 1000 C. 1000 lights. So all I did was come up with my own... kick ass algorithm to sneak D. 1999 in, and now we own the place." -Lyle, the Napster, (Seth Green), The Italian Job y , p , ( ), E 4000 E. 4000 CS 307 Fundamentals of CS 307 Fundamentals of 1 2 Computer Science Red Black Trees Computer Science 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 � Each node is labeled as Red or Black. – deletion – access � Path: A unique series of links (edges) at u que se es o s (edges) � Balance is important. Unbalanced trees give traverses from the root to each node. worse than log N times for the basic tree g – The number of edges (links) that must be The number of edges (links) that must be operations followed is the path length � Can balance be guaranteed? � In Red Black trees paths from the root to Can balance be guaranteed? In Red Black trees paths from the root to elements with 0 or 1 child are of particular interest interest CS 307 Fundamentals of 3 CS 307 Fundamentals of 4 Computer Science Red Black Trees Computer Science Red Black Trees

  2. Paths to Single or Zero Child Red Black Tree Rules Nodes 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 3 If d i d it hild t 35 12 be black. (a.k.a. the red rule) 21 21 16 16 3 56 4. Every path from a node to a null link must contain the same link must contain the same 1 number of black nodes (a.k.a. the path rule) the path rule) CS 307 Fundamentals of CS 307 Fundamentals of 5 6 Computer Science Red Black Trees Computer Science Red Black Trees Example of a Red Black Tree Red Black Tree? 19 � The root of a Red Black tree is black 35 12 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 Rule 4: The number of Black nodes must be the same in all paths 75 from the root node to null nodes -10 135 135 19 19 -5 5 100 35 12 -8 -6 80 21 16 3 56 30 CS 307 Fundamentals of 7 CS 307 Fundamentals of 8 Computer Science Red Black Trees Computer Science Red Black Trees

  3. Attendance Question 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 16 3 3 B. No Yes 0 C C. Yes Yes No No D. Yes Yes Perfect? Full? F ll? Complete? CS 307 Fundamentals of CS 307 Fundamentals of 9 10 Computer Science Red Black Trees Computer Science Red Black Trees Attendance Question 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?) � Due to the rules there are limits on how B. No Yes u ba a ced a unbalanced a Red Black tree may become. ed ac ee ay beco e C. C Yes Yes No No – on the previous example may we hang a new D. Yes Yes node off of the leaf node that contains 0? CS 307 Fundamentals of 11 CS 307 Fundamentals of 12 Computer Science Red Black Trees Computer Science Red Black Trees

  4. 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 12 35 35 lowest level the height will be minimal, ~log N � To get the max height for N elements there 21 21 13 13 56 56 1 should be as many Red nodes as possible 43 99 down one path and all other nodes are Black 15 15 25 25 – This means the max height would be < 2 * log N – see example on next slide p 100 80 70 CS 307 Fundamentals of CS 307 Fundamentals of 13 14 Computer Science Red Black Trees Computer Science Red Black Trees Maintaining the Red Black Insertions with Red Parent - Child Properties in a Tree Properties in a Tree Must modify tree when insertion would result in � Insertions Red Parent Child pair using color changes and Red Parent - Child pair using color changes and � Must maintain rules of Red Black Tree. rotations. 30 � New Node always a leaf New Node always a leaf – can't be black or we will violate rule 4 70 15 – therefore the new leaf must be red – 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 if parent red things get interesting because a red 80 90 50 65 leaf with a red parent violates rule 3 5 40 55 CS 307 Fundamentals of 15 CS 307 Fundamentals of 16 Computer Science Red Black Trees Computer Science Red Black Trees

  5. 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 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 S s parent and X s grandparent 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 – What color is G? Inside -> left right or right left moves g g CS 307 Fundamentals of CS 307 Fundamentals of 17 18 Computer Science Red Black Trees Computer Science Red Black Trees Fixing the Problem Single Rotation G P P S X G D C C A E B X S B A If X is an outside node a single E D rotation between P and G fixes the problem. p Apparent rule violation? pp 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 CS 307 Fundamentals of 20 Computer Science Red Black Trees Computer Science Red Black Trees

  6. Case 2 After Double Rotation � What if X is an inside node relative to G? X – a single rotation will not work G P � Must perform a double rotation – rotate X and P C C A A B B G S – rotate X and G S S P P E D Apparent rule violation? D E E A A X X C B CS 307 Fundamentals of CS 307 Fundamentals of 21 22 Computer Science Red Black Trees Computer Science Red Black Trees Fixing Tree when S is Red Case 3 Sibli Sibling is Red, not Black i R d t Bl k � Must perform single rotation between parent, P and grandparent, G, and then make appropriate color changes G P P P S S G X D E C C X X C S B A A A B B Any problems? D E CS 307 Fundamentals of 23 CS 307 Fundamentals of 24 Computer Science Red Black Trees Computer Science Red Black Trees

  7. More on Insert Example of Inserting Sorted Numbers � Problem: What if on the previous example � P bl Wh t if th i l � 1 2 3 4 5 6 7 8 9 10 G's parent had been red? Insert 1. A leaf so � Easier to never let Case 3 ever occur! � E i t l t C 3 ! red. Realize it is � On the way down the tree, if we see a node X that 1 root so recolor has 2 Red children, we make X Red and its two has 2 Red children we make X Red and its two to black. children black. – if recolor the root, recolor it to black – the number of black nodes on paths below X remains 1 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 CS 307 Fundamentals of 25 26 Computer Science Red Black Trees Computer Science Red Black Trees Insert 2 Insert 3 make 2 red. Parent 1 1 Insert 3. Parent is red. Insert 3. Parent is red. is black so done is black so done. Parent's sibling is black 2 (null) 3 is outside relative 2 to grandparent. Rotate 3 parent and grandparent 2 3 1 CS 307 Fundamentals of 27 CS 307 Fundamentals of 28 Computer Science Red Black Trees 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