1 COP 3540 Data Structures with OOP Chapter 9 – Red Black Trees.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

CS16: Introduction to Data Structures & Algorithms
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Binary Trees CSC 220. Your Observations (so far data structures) Array –Unordered Add, delete, search –Ordered Linked List –??
Chapter 4: Trees Part II - AVL Tree
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and black antennas.
Time Complexity of Basic BST Operations Search, Insert, Delete – These operations visit the nodes along a root-to- leaf path – The number of nodes encountered.
CS202 - Fundamental Structures of Computer Science II
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Balanced Binary Search Trees
Balanced Search Trees. 2-3 Trees Trees Red-Black Trees AVL Trees.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Tirgul 10 Rehearsal about Universal Hashing Solving two problems from theoretical exercises: –T2 q. 1 –T3 q. 2.
Tirgul 5 AVL trees.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
B + -Trees (Part 1) COMP171. Slide 2 Main and secondary memories  Secondary storage device is much, much slower than the main RAM  Pages and blocks.
Tirgul 5 This tirgul is about AVL trees. You will implement this in prog-ex2, so pay attention... BTW - prog-ex2 is on the web. Start working on it!
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
Binary Trees Chapter 6.
Balanced Trees Ellen Walker CPSC 201 Data Structures Hiram College.
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
1 COP 3538 Data Structures with OOP Chapter 8 - Part 2 Binary Trees.
Chapter 6 Binary Trees. 6.1 Trees, Binary Trees, and Binary Search Trees Linked lists usually are more flexible than arrays, but it is difficult to use.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of.
CSIT 402 Data Structures II
Beyond (2,4) Trees What do we know about (2,4)Trees? Balanced
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
1 Trees 4: AVL Trees Section 4.4. Motivation When building a binary search tree, what type of trees would we like? Example: 3, 5, 8, 20, 18, 13, 22 2.
11/7/20151 Balanced Trees Data Structures Ananda Gunawardena.
File Organization and Processing Week Tree Tree.
Week 8 - Wednesday.  What did we talk about last time?  Level order traversal  BST delete  2-3 trees.
CS 61B Data Structures and Programming Methodology Aug 7, 2008 David Sun.
CSC 213 – Large Scale Programming Lecture 18: Zen & the Art of O (log n ) Search.
Binary Search Trees (BSTs) 18 February Binary Search Tree (BST) An important special kind of binary tree is the BST Each node stores some information.
Balanced Search Trees Chapter 19 Data Structures and Problem Solving with C++: Walls and Mirrors, Carrano and Henry, © 2013.
AVL Trees and Heaps. AVL Trees So far balancing the tree was done globally Basically every node was involved in the balance operation Tree balancing can.
CS 307 Fundamentals of Computer ScienceRed Black Trees 1 Topic 19 Red Black Trees "People in every direction No words exchanged No time to exchange And.
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
AA Trees.
File Organization and Processing Week 3
Week 7 - Friday CS221.
Red Black Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CSE373: Data Structures & Algorithms Lecture 7: AVL Trees
Red-Black Trees Motivations
(edited by Nadia Al-Ghreimil)
Monday, April 16, 2018 Announcements… For Today…
TCSS 342, Winter 2006 Lecture Notes
CS202 - Fundamental Structures of Computer Science II
Advanced Associative Structures
CS202 - Fundamental Structures of Computer Science II
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and Black.
Algorithms and Data Structures Lecture VIII
2-3-4 Trees Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

1 COP 3540 Data Structures with OOP Chapter 9 – Red Black Trees

2 Introduction to Red Black Trees  Trees offer many many advantages – especially for quick inserting and reasonably quick deleting.  Binary tree searches provide outstanding performance in almost all instances  Big Problem: unbalanced trees   This will significantly reduce search times and more…  Unbalanced trees may easily arise if incoming data is sorted or nearly sorted (up or down!)  We need balanced or nearly-balanced trees.

3 Red Black Trees - RBTrees  We will not cover this entire chapter, but we will cover the most salient parts  Red Black Trees is an approach to  restructure unbalanced trees and moreso, to  maintain balanced trees when we start to insert or delete nodes.

4 Red Black Trees - RBTrees - more  Not trivial to understand …  All nodes will either be ‘red’ or ‘black’ with constraints!!  No code is found in this chapter.  Used in and 2-3 trees next chapter to assist in balancing multi-way trees – especially for external storage.  Strongly encourage exercising the applet, although book provides close coverage.

5 Insertions into Red Black Tree  Top Down Insertions:  Some structural changes (such as rotations, flips, color changes, etc.) may be made as part of the search routine.  Bottom up Insertions:  Look for the proper place to add the node  Then, we proceed up the tree making structural changes.  Less efficient than top-down insertions.

6 Balanced and Unbalanced Trees  Easy to see how a tree becomes unbalanced.  Think all nodes ascending (or descending) and resulting structure.  An unbalanced tree results in the same structure as a linked list.  Still would be going from node to node to node…  Would provide O(n) performance for searching, insertions, etc.  Consider: sequential search (array or linked list) for 1000 elements  average search time of 5000; But: binary search tree: max is 10 if tree is balanced (the only way to go…)

7 Balanced and Unbalanced Trees  Very important to have tree balanced  As it turns out, when input is random, the binary search tree development will often be reasonably ‘okay.’  Must watch out for a key values that represent extremes.  We need trees that have approximately the same number of descendents on both sides of all nodes.  In RBTrees, we constantly balance during the insertion and deletion process.  That is, as we insert and delete, we will balance as part of the procedure.  During these processes, certain RB characteristics must be maintained. If not, structural changes take place as part of insertion and deletion.

8 Red Black Tree Characteristics  Lots of rules and characteristics….  All nodes are colored (red or black)  (Nice for University of Georgia fans!)  During insertions and deletions all the rules (coming) are checked to preserve the balance of the tree.  This means that all the Red Black rules must be verified along the way.

9 Red Black Rules – The Bigees…  These are essential in understanding the RB Tree concepts and how the algorithms work (and hence how the applet works). 1. Every node is either red or black 2. The root is always black 3.If a node is red, its children must be black (converse not true). 4. Every path from root to a leaf (or to a null child – important later) must contain the same number of black nodes.  (number of black nodes on a path from root to node is called the black height.)

10 Violations as We Process a Tree  It is permissible (actually REQUIRED) to fix the violations as we insert (and delete) – if we want to keep the tree balanced!  We can flip (change colors… more coming)  We can rotate left and rotate right (RoL, RoR)  Running the Applet Book as description of the buttons (several)  Start, Insert, Delete, Flip, RoL, RoR, R/B.  There are also text messages telling the applet user when rules have been violated.

11 Actions  The Flip button:  When arrow is placed on a parent and Flip is depressed, color of root is flipped; color of children is flipped (all children will be same). If node is black and we flip to red, then its children must be black. (rule 3)  Changes colors of at most three nodes at once  Rotate Left:  Position yourself on the topmost node of group to be rotated.  RoL – topmost node MUST have a right child.  RoR – topmost node MUST have a left child.  R/B – Changes a node’s color (single node).

12 Experiments  Part of the process of adding a node is to verify that the RB rules are accommodated.  When we insert a node, we will start with a red node because this will cause fewer problems later.  E.g. doesn’t change black height…and more.

13 RoR Remember: RoR requires the node to have a non-null left subtree. RoL Remember: RoL requires the node to have a non-null right subtree. Remember: the root node must be Black!!

14 Color Flips Cannot do here because root cannot be red. Assumption above: we are looking at the root. Otherwise, it’d be okay… Message here: Can’t Insert. Again, normally okay, unless node is the tree root.

15 Unbalanced Tree  Definition of Unbalanced Tree: when one path (from root to leaf nodes) differs from another by more than one level. (two or more levels).  If we insert a node and the tree becomes unbalanced, we need to balance the tree.  Since we traditionally add Red nodes, the typical message will be parent and child are both red.  (Rule 3 violated - If node is red, children must be black.  But sometimes just flipping might result in black heights differing!  Can become complicated as we often may need to both rotate and flip…

16 Nice to Knows…  One canNOT create an unbalanced tree if all of the red-black rules are accommodated.  Equivalently, following the four Red Black rules ensures that the tree will be balanced.  “If one path is more than one node longer than another, it must either have more black nodes, violating Rule 4, or it must have two adjacent red nodes, violating Rule 3.”  Rule 4: Every path from root to a leaf (or to a null child – important later) must contain the same number of black nodes.  Rule 3: If a node is red, its children must be black (converse not true).

17 Rotations  These can become complicated.  Rotations:  1. must raise some nodes (that is, raise to a higher level) and lower other nodes to balance the tree, and  2. must ensure that the characteristics of a binary search tree are not violated.  We will look at  1. Simple Rotations  2. Cross Over Nodes  3. Moving subtrees

18 1. Simple Rotations  It is actually the relationship between nodes that changes: a parent becomes a child…  Rotations don’t necessarily involve the root.  Any node can be the center of rotation; that is, any node of any sub-tree can be the center of rotation.  Just remember: a RoR requires the top node to have a left child and conversely.  Simple Rotation: a repeat slide – but for completeness:

19 RoR Remember: RoR requires the node to have a non-null left subtree. Of course, this cannot remain this way! Why not?? RoL Remember: RoL requires the node to have a non-null right subtree

20 2. Cross-Over Rotations – a bit more complicated: Null nodes Two actions here: 1. Insert 12; 12 (new) is red. Get message: Needs color flip (25 originally is red; Click Flip button; Parent and child swap colors. OK 2.Insert 37. Successful. We end up with the arrangement in the center. 1. Every node is either red or black 2. The root is always black; 3. If a node is red, its children must be black (converse not true). 4. Every path from root to leaf (or to a null child – important later) must contain the same number of black nodes. Now try a rotation right (placing arrow in applet on root). Note: 37 moved across from right child of 25 to left child o 50!! Had to be done in keeping this a binary search tree.! But Rule 4 (Every path from root to leaf (or to a null child) must contain = number black nodes.) For now, in middle figure, 37 is called an inside grandchild (and 12 is called an outside grandchild)!! If an inside grandchild is a child of a node that is ‘going up,’ it is always disconnected from parent and reconnected to its former grandparent! How ‘bout that!! RB Tree: from last slide

21 Subtrees Moved – Entire Subtrees can be moved! With 50 as the root, we will insert (in order) 25, 75, 12, 37, 62, 87, 6, 18, 31, and 43. We must click Flip whenever we get a Can’t Insert Needs Color Flip. We arrive at the figure on the left. (All rules okay?) Verify!!!! Rotate Right, and we get the second figure. Note: 50 went to the right; root’s left child is now root. Entire subtree with 12 moved up.  Entire subtree of 37 crossed over! (inside grandchild!!) But there are problems (look at root…) (root must be black (Rule 1) and it is not, and for a red node (like 50), children must be black (Rule 2)) Detailed fix is found ahead in chapter…  But note the relation between the nodes in the sub-trees is unchanged by the rotation. Entire sub-tree moves in a rotation no matter the size of the sub-tree. Note: this left RB Tree is great Go through four rules!!

22 Insertions  I will not cover the section on Inserting a New Node.  We are not using RBTrees in our projects, but I DO want you to know the rules and a number of their characteristics!  But flips and rotations are encountered algorithmically once a place for the node is determined (via standard binary search tree insert procedures).  There is more information on the inside and outside grandchildren.  Sometimes they rotate left; sometimes right; sometimes they change color.  These structural changes are implemented all the way down to the new node you have inserted to ensure the RB rules are accommodated and hence the tree is balanced!

23 Deletions  Deletions in binary trees are quite involved.  Normally, in practice, designers and programmers merely ‘mark’ the node as logically deleted (but physically it is still present).  OK for small number of deletions  Can then later delete, if needed, ‘off line.’

24 Efficiency of Red Black Trees  Search is very fast, of course, because the resulting tree is balanced.  All O(log 2 n) time! Super.  Small storage issue per node to include an isBlack() or isRed() boolean method. No big deal.  Insertions and Deletions require additional time due to requirements to do color flips and rotations all the way down to the insertion point.  Because most insertions require an average of a single rotation, insertion still takes about O(log 2 n) time but a bit slower than insertion in the ordinary binary tree.

25 Implementation of Red Black Trees  Algorithm is complex.  Steps are outlined for the ‘interested’ implementer.  The really nice thing about Red Black Trees is that we can be assured they will be balanced!  This is essential for good performance!!

26 AVL Trees  Another kind of balanced tree.  AVL trees also store the difference between the heights of a node’s left and right subtree.  (cannot be > one, of course. Won’t be balanced.)  AVL trees do require some of the same operations (rotations and moves…)  But: AVL trees are guaranteed to be balanced!  Now, on to Multi-Way trees!!

27 Study:  Chapter Summary  Questions (answers are in Appendix C)  Know the four RB Rules. If you go over the materials a few times, they become automatic.