File Organization and Processing Week 3

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
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:
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
CSIT 402 Data Structures II
Beyond (2,4) Trees What do we know about (2,4)Trees? Balanced
Lecture 2 Red-Black Trees. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 2 Red-Black Trees Definition: A red-black tree is a binary search tree in which: 
CS 367 Introduction to Data Structures Lecture 9.
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.
Red-Black Trees Definitions and Bottom-Up Insertion.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
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.
AVL Trees AVL (Adel`son-Vel`skii and Landis) tree = – A BST – With the property: For every node, the heights of the left and right subtrees differ at most.
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 β.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Lecture 23 Red Black Tree Chapter 10 of textbook
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Lec 13 Oct 17, 2011 AVL tree – height-balanced tree Other options:
BCA-II Data Structure Using C
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Balancing Binary Search Trees
Red Black Trees
Lecture 17 Red-Black Trees
Balanced Trees (AVL and RedBlack)
Red-Black Trees.
AVL Tree.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Summary of General Binary search tree
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Lecture 25 Splay Tree Chapter 10 of textbook
Red-Black Trees Motivations
Red-Black Trees Bottom-Up Deletion.
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
Multi-Way Search Trees
Red-Black Trees Bottom-Up Deletion.
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.
Red-Black Trees.
Red Black Trees Top-Down Deletion.
Algorithms and Data Structures Lecture VIII
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
AVL-Trees (Part 1).
Red-Black Trees Bottom-Up Deletion.
Lecture 10 Oct 1, 2012 Complete BST deletion Height-balanced BST
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Red-black tree properties
Red Black Trees Top-Down Deletion.
Binary Search Trees < > = Dictionaries
Red-Black Trees v z /6/ :10 PM Red-Black Trees
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

1431227-3 File Organization and Processing Week 3 Red-Black Tree

Operations on a RB Tree As with the binary search tree, the following operations are performed on red-black trees: Insert a key value (insert) Determine whether a key value is in the tree (lookup) Remove key value from the tree (delete) Print all of the key values in sorted order (print)

Inserting a New Node The goal of the insert operation is to insert key K into tree T, maintaining T’s red-black tree properties. If T is empty, replace it with a single black node containing K. This ensures that the root property is satisfied. If T is a non-empty tree, then we do the following: Use the BST insert algorithm to add K to the tree Color the node containing K red Restore red-black tree properties (if necessary)

Inserting a New Node (Cont’d) BST insert algorithm always adds a leaf node. Because we are dealing with a non-empty red-black tree, adding a leaf node will not affect T’s satisfaction of the root property. Moreover, adding a red leaf node will not affect T’s satisfaction of the black height property. However, adding a red leaf node may affect T’s satisfaction of the red property, so we will need to check if that is the case and, if so, fix it. In fixing a red property violation, we will need to make sure that we don’t end up with a tree that violates the root or black height properties.

Steps to Insert a Node Inserting into a non-empty tree, what we need to do will depend on the color of K’s parent. Let P be K’s parent. We need to consider two cases: Case 1: K’s parent P is black If K’s parent P is black, then the addition of K did not result in the red property being violated, so there’s nothing more to do. Case 2: K’s parent P is red If K’s parent P is red, then P now has a red child, which violates the red property. Note that P’s parent, G, (K’s grandparent) must be black. In order to handle this double-red situation, we will need to consider the color of G’s other child, that is, P’s sibling, S. (Note that S might be null, i.e., G only has one child and that child is P.)

Steps to Insert a Node – Case 2 Case 2: K’s parent P is red. We have two cases: Case 2a: P’s sibling S is black or null If P’s sibling S is black or null, then we will do a trinode restructuring of K (the newly added node), P (K’s parent), and G (K’s grandparent). To do a restructuring, we first put K, P, and G in order; let’s call this order A, B, and C. We then make B the parent of A and C, color B black, and color A and C red. (Single Rotation Right) We also need to make sure that any subtrees of P and S (if S is not null) end up in the appropriate place once the restructuring is done. There are four possibilities for the relative ordering of K, P, and G.

First 2 Possibilities (Rotation) Switch color of K’s grandparent G 2. Switch color of K’s parent P 3. Rotate with K’s grandparent G at the top, in the direction that raises K (1 rotation) K- Outside grandchild Switch color of K’s grandparent G 2. Switch color of K (Not it’s parent P) 3. Rotate with K’s parent at the top (not G), in the direction that raises K (left rotation) 4. Rotate again with K’s grandparent G at the top (not G), in the direction that raises K (right rotation) K- Inside grandchild

Second 2 Possibilities (Rotation) Fix changes from inside grandchild to outside grandchild, here rotate right (1 rotation) Rotate right with the grandparent at the top (2nd rotation)

Case 2: P’s Sibling is Red If P’s sibling S is red, then we will do a recoloring of P, S, and G: the color of P and S is changed to black and the color of G is changed to red (unless G is the root, in which case we leave G black to preserve the root property). (Color Flip) Recoloring does not affect the black property of a tree: the number of black nodes on any path that goes through P and G is unchanged when P and G switch colors (similarly for S and G).

Case 2: P’s Sibling is Red Recoloring may have introduced a double-red situation between G and G’s parent. If that is the case, then we recursively handle the double-red situation starting at G and G’s parent (instead of K and K’s parent).

Time Complexity for Insertion Inserting a key into a non-empty tree has three steps. In the first step, the BST insert operation is performed. The BST insert operation is O(height of tree) which is O(log N) because a red-black tree is balanced. The second step is to color the new node red. This step is O(1) since it just requires setting the value of one node’s color field. In the third step, we restore any violated red-black properties. Restructuring is O(1) since it involves changing at most five pointers to tree nodes. Once a restructuring is done, the insert algorithm is done, so at most 1 restructuring is done in step 3. So, in the worst-case, the restructuring that is done during insert is O(1).

Time Complexity for Insertion Changing the colors of nodes during recoloring is O(1). However, we might then need to handle a double-red situation further up the path from the added node to the root. In the worst-case, we end up fixing a double-red situation along the entire path from the added node to the root. So, in the worst-case, the recoloring that is done during insert is O(log N) ( = time for one recoloring * max number of recolorings done = O(1) * O(log N) ). Thus, the third step (restoration of red-black properties) is O(log N) and the total time for insert is O(log N).

Summary of RB Tree Balanced search trees have a height that is always O(log N). One consequence of this is that lookup, insert, and delete on a balanced search tree can be done in O(log N) worst-case time. In constrast, binary search trees have a worst-case height of O(N) and lookup, insert, and delete are O(N) in the worst-case. Red-black trees are just one example of a balanced search tree. Red-black trees are binary search trees that store one additional piece of information in each node (the node’s color) and satisfy three properties. These properties deal with the way nodes can be colored (the root property and the red property) and the number of black nodes along paths from the root node to a null child pointer (the black property). Although it is not intuitively obvious, the algorithms for restoring these properties after a node has been added or removed result in a tree that stays balanced.

Summary of RB Tree While the lookup method is identical for binary search trees and red-black trees, the insert and delete methods are more complicated for red-black trees. The insert method initially performs the same insert algorithm as is done in binary search trees and then must perform steps to restore the red-black tree properties through restructuring and recoloring. The delete method for a red-black tree is more complicated than the insert method.

Practice Problem Insert the following sequence, show step by step: 50, 25, 75, 12, 6, 65, 89 50, 25, 75, 87, 93, 12, 30 50, 25, 75, 12, 18, 67, 88 4, 7, 12, 15, 3, 5, 14, 18, 16, 17