Red-Black Trees Motivations

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Introduction to Algorithms Red-Black Trees
Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
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.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
Balanced Trees Balanced trees have height O(lg n).
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
Red-Black Trees Lecture 10 Nawazish Naveed. Red-Black Trees (Intro) BSTs perform dynamic set operations such as SEARCH, INSERT, DELETE etc in O(h) time.
Red-Black Trees CS302 Data Structures Dr. George Bebis.
Introduction to Algorithms Jiafen Liu Sept
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
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: 
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
1 Algorithms CSCI 235, Fall 2015 Lecture 25 Red Black Trees II.
Data StructuresData Structures Red Black Trees. Red-black trees: Overview Red-black trees are a variation of binary search trees to ensure that the tree.
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
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Sept Red-Black Trees What is a red-black tree? -node color: red or black - nil[T] and black height Subtree rotation Node insertion Node deletion.
Red-Black Trees Bottom-Up Deletion. Recall “ordinary” BST Delete 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has just.
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.
Binary Search Trees What is a binary search tree?
File Organization and Processing Week 3
Balanced Search Trees Modified from authors’ slides.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Red-Black Trees.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Summary of General Binary search tree
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Design and Analysis of Algorithms
Slide Sources: CLRS “Intro. To Algorithms” book website
CS200: Algorithms Analysis
Red-Black Trees Bottom-Up Deletion.
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
CMSC 341 (Data Structures)
Lecture 9 Algorithm Analysis
Red-Black Trees Bottom-Up Deletion.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees.
Red Black Trees Top-Down Deletion.
Algorithms and Data Structures Lecture VIII
CS 583 Analysis of Algorithms
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
Red-Black Trees Bottom-Up Deletion.
Analysis of Algorithms CS 477/677
Algorithms CSCI 235, Spring 2019 Lecture 24 Red Black Trees II
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Announcements Midterm will be given on 10/21/99
Chapter 12&13: Binary Search Trees (BSTs)
Red Black Trees Top-Down Deletion.
Red-Black Trees CS302 Data Structures
Presentation transcript:

Red-Black Trees Motivations worst case search in a binary search tree is O(n) if the tree is degenerate (that is, linear) Formally specify the characteristics of a degenerate tree if the data is entered in sorted order, this case will occur; even partially sorted order is bad we want to find a way to keep the tree approximately balanced no matter what the order of insertion is an approach called AVL trees that used local rotations was proposed in 1960 red-black trees also use local rotations but have slightly better performance (fewer rotations) than AVL trees

Red-Black Tree Properties Every node is either red or black; all nil leaves are black If a node is red, then both children are black Every simple path from a node to any descendant leaf contains the same number of black nodes; we call this number bh(x), the black height from node x

Red-Black Tree Performance a red-black tree with n internal nodes has height at most 2 lg(n+1), proof is given on page 274 the operations search, minimum, maximum, successor and predecessor can all be implemented in O(lg n) time we will show that the insert and delete operations that preserve the red-black tree property can also be executed in O(lg n) time

Rotations There are two rotations: left and right; they are local operations on a node that preserve the tree ordering In either configuration the ordering is: a x b y g Each node contains a color (1 bit), key, left, right, and p (parent) Rotations only change the pointer values

An Example of Left-Rotate What happened to the height of this tree? Both rotations can be accomplished in O(1) time

The Insertion Operation Initially the node will be inserted as in a binary search tree and colored red Rotations and recoloring will restore the red-black tree property The initial insertion can only violate the property requiring a red node having two black children a while loop will move the point of attention up the tree restoring this property while maintaining the black height property as invariant at some point, rotations will be performed and the loop will be exited

The Big Picture x and p[x] are red x’s uncle y is also red, so p[x] and y are colored black and x moves up the tree x and p[x] are red, x is a right child, so a left rotation makes it a left child with p[x] red a right rotation and color changes completes the process

Code Overview The loop continues while x and p[x] are both red The only case shown is when p[x] is a left child, the other cases are symetric Case 1 is applied as long as x’s uncle y is red In cases 2/3, x’s uncle is black; case 2 makes x a left child through rotation; case 3 uses recoloring and a right rotation to restore the red-black property

Case 1 Details x can either be a left or right child of a red parent assuming uncle y is red, p[x] and y are colored black, and p[p[x]] is colored red; property 4 is preserved and p[p[x]] becomes the new x it is assumed the root is black (see line 18 in the code) so that p[p[x]] is known to exist

Cases 2/3 Details if x is a right child, case 2 makes it a left child each of the subtrees, a, b, g, d, has a black root and the same black-height case 3 causes a right rotation and color changes, all preserving property 4 since property 3 is no longer violated, the while loop terminates

Complexity of Insertions Case 1 can be applied at most O(lg n) times since the root, which is black, will eventually be reached if cases 2 and 3 never apply If case 1 does not apply, then there is either only one rotation (case 3 only) or two rotations (case 2 followed by case 3) before property 3 is restored and the loop is exited Thus the running time is no more than O(lg n) and at most there are two rotations; often times the tree requires no adjustments at all

The Deletion Operation Deletion is only slightly more complex than insertion; it has complexity of O(lg n) The algorithm is easier to code if the empty tree is represented by an object nil[T] that has the same data fields as an ordinary node; its color is black Deletion is similar to deletion from a BST but we must call a separate routine called fixup to restore the four red-black properties

Deletion from a Binary Search Tree Recall the cases that must be considered when deleting a node from a binary search tree; in the case of two children assume the successor node is involved

RB-Delete The code is similar to the delete operation for a BST References to nil are replaced with the sentinnel nil[T] This allows the assignment in line 7 to be unconditional since the nil is like other nodes If the node y that is removed is black, the fixup routine restores the black height of the tree

Fixup - 1 Parameter x was the removed node’s sole child (may be nil) which has a double black count The loop moves x with its double count up the tree until: x points to a red node which is colored black x points to the root and the extra black count is discarded rotations and recoloring can solve the problem

Fixup - 2 line 2 determines x is a left child; symmetric code holds if it is a right child w points to x’s sibling which cannot be nil each of the four transformations preserves the number of black nodes (including the count of 2 for x) from the root to each subtree case 1 occurs when w is red; after the transformation it is black and case 2/3/4 holds case 2/3/4 occurs when w is black, cases are distinguished by the color of w’s children

Fixup - Cases 1 and 2 case 1: w is red and has black children; switch colors of w and p[x] then perform left rotation; now perform one of the other cases as appropriate case 2: w and two children are black, remove black from x (only has one black now) and w (becomes red); add the extra black to p[x] and repeat loop if we entered case 2 via case 1, the loop will terminate

Fixup - Cases 3 and 4 case 3: w is black, left child is red, right child is black; switch colors of w and left child then perform a right rotation on w; we have converted case 3 into case 4 case 4: w is black, right child is red; make color changes indicated and left rotate on p[x] set x’s new value to be the root to force loop termination

Complexity of Deletions The cost of RB-Delete without fixup is at most O(lg n) since the tree is at most lg n high RB-Fixup cases 1, 3, 4 do not repeat the loop, so the amount of work is constant with at most three rotations case 2 moves x up to the root and can occur at most lg n times and with no rotations Fixup at most has lg n time and 3 rotations Therefore the overall complexity of RB-Delete with fixup is still O(lg n)