Red-Black Trees Bottom-Up Deletion.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Definitions and Bottom-Up Insertion
CMPT 225 Red–black trees. Red-black Tree Structure A red-black tree is a BST! Each node in a red-black tree has an extra color field which is red or black.
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.
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.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
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.
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State 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.
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Binary Trees Chapter 6.
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.
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:
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
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
Balanced Search Trees 3.4 – 3.7 Team names Majed Suhaim Ahmed Sulaiman M Alharbi.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
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: 
Red Black Trees Top-Down Insertion. Review of Bottom-Up Insertion In B-Up insertion, “ordinary” BST insertion was used, followed by correction of the.
Red-Black Trees Definitions and Bottom-Up Insertion.
Red Black Trees Top-Down Deletion. Recall the rules for BST deletion 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has.
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
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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.
Definitions and Bottom-Up Insertion
AA Trees.
File Organization and Processing Week 3
BCA-II Data Structure Using C
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Lecture 17 Red-Black Trees
Red-Black Trees.
AVL Tree.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Lecture 25 Splay Tree Chapter 10 of textbook
Red-Black Trees Motivations
Data Structures Balanced Trees CSCI
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Lecture 9 Algorithm Analysis
Red-Black Trees Bottom-Up Deletion.
B- Trees D. Frey with apologies to Tom Anastasio
Lecture 9 Algorithm Analysis
UMBC CSMC 341 Red-Black-Trees-1
B- Trees D. Frey with apologies to Tom Anastasio
Lecture 9 Algorithm Analysis
Red-Black Trees.
Red Black Trees Top-Down Deletion.
CMSC 202 Trees.
2-3-4 Trees Red-Black Trees
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
Red-Black Trees Bottom-Up Deletion.
Algorithms CSCI 235, Spring 2019 Lecture 24 Red Black Trees II
Red-black tree properties
Red-Black Tree Rotations
Red-Black Tree Rotations
Red Black Trees Top-Down Deletion.
Presentation transcript:

Red-Black Trees Bottom-Up Deletion

Recall “ordinary” BST Delete If node to be deleted is a leaf, just delete it. If node to be deleted has just one child, replace it with that child (splice) If node to be deleted has two children, replace the value in the node by its in-order predecessor/successor’s value then delete the in-order predecessor/successor (a recursive step) 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

UMBC CSMC 341 Red-Black-Trees-2 Bottom-Up Deletion Do ordinary BST deletion. Eventually a “case 1” or “case 2” deletion will be done (leaf or just one child). -- If deleted node, U, is a leaf, think of deletion as replacing U with the NULL pointer, V. -- If U had one child, V, think of deletion as replacing U with V. What can go wrong?? 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

Which RB Property may be violated after deletion? If U is Red? Not a problem – no RB properties violated If U is Black? If U is not the root, deleting it will change the black-height along some path 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

UMBC CSMC 341 Red-Black-Trees-2 Fixing the problem Think of V as having an “extra” unit of blackness. This extra blackness must be absorbed into the tree (by a red node), or propagated up to the root and out of the tree. There are four cases – our examples and “rules” assume that V is a left child. There are symmetric cases for V as a right child. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

UMBC CSMC 341 Red-Black-Trees-2 Terminology The node just deleted was U The node that replaces it is V, which has an extra unit of blackness The parent of V is P The sibling of V is S Black Node Red or Black and don’t care Red Node 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

Bottom-Up Deletion Case 1 V’s sibling, S, is Red Rotate S around P and recolor S & P NOT a terminal case – One of the other cases will now apply All other cases apply when S is Black 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

UMBC CSMC 341 Red-Black-Trees-2 Case 1 Diagram S P Rotate S around P P S V+ V+ S P Recolor S & P V+ 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

Bottom-Up Deletion Case 2 V’s sibling, S, is Black and has two Black children. Recolor S to be Red P absorbs V’s extra blackness If P is Red, we’re done (it absorbed the blackness) If P is Black, it now has extra blackness and problem has been propagated up the tree 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

UMBC CSMC 341 Red-Black-Trees-2 Case 2 diagram Recolor S P absorbs blackness P+ P S S V V+ Either extra Black absorbed by P or P now has extra blackness 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

Bottom-Up Deletion Case 3 S is Black S’s right child is RED (Left child either color) Rotate S around P Swap colors of S and P, and color S’s right child Black This is the terminal case – we’re done 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

UMBC CSMC 341 Red-Black-Trees-2 Case 3 diagrams S P Rotate S around P P S V+ V+ S P Swap colors of S & P Color S’s right child Black V 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

Bottom-Up Deletion Case 4 S is Black, S’s right child is Black and S’s left child is Red Rotate S’s left child around S Swap color of S and S’s left child Now in case 3 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

UMBC CSMC 341 Red-Black-Trees-2 Case 4 Diagrams P P S V+ V+ P S Rotate S’s left around S V+ S Swap colors of S and S’s original left child 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

UMBC CSMC 341 Red-Black-Trees-2 Top-Down Deletion An alternative to the recursive “bottom-up” deletion is “top-down” deletion. This method is iterative. It moves down the tree only, “fixing” things as it goes. What is the goal of top-down deletion? 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

UMBC CSMC 341 Red-Black-Trees-2 65 50 80 10 70 90 60 62 Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70 8/3/2007 UMBC CSMC 341 Red-Black-Trees-2

Red Black Trees Top-Down Deletion

Recall the rules for BST deletion If vertex to be deleted is a leaf, just delete it. If vertex to be deleted has just one child, replace it with that child If vertex to be deleted has two children, replace the value of by it’s in-order predecessor’s value then delete the in- order predecessor (a recursive step)

What can go wrong? If the deleted node is red? Not a problem – no RB properties violated If the deleted node is black? If the node is not the root, deleting it will change the black-height along some path

The goal of T-D Deletion To delete a red leaf How do we ensure that’s what happens? As we traverse the tree looking for the leaf to delete, we change every node we encounter to red. If this causes a violation of the RB properties, we fix it

Bottom-Up vs. Top-Down Bottom-Up may take longer Top-Down Deletion forces rotations, recolorings that can propagate from leaf to root. Top-Down Restructures the tree on the way down so we don’t have to go back up Both methods are O(lg n), but Top-Down generally is faster (no return trip up tree)

Terminology Matching Weiss text section 12.2.3 X is the node being examined T is X’s sibling P is X’s (and T’s) parent R is T’s right child L is T’s left child This discussion assumes X is the left child of P. As usual, there are left-right symmetric cases.

Basic Strategy As we traverse the tree, we change every node we visit, X, to Red. When we change X to Red, we know P is also Red (we just came from there) T is black (since P is Red, it’s children are Black)

Step 1 – Examine the root If both of the root’s children are Black Make the root Red Move X to the appropriate child of the root Proceed to step 2 Otherwise designate the root as X and proceed to step 2B.

Step 2 – the main case As we traverse down the tree, we continually encounter this situation until we reach the node to be deleted X is Black, P is Red, T is Black We are going to color X Red, then recolor other nodes and possibly do rotation(s) based on the color of X’s and T’s children 2A. X has 2 Black children 2B. X has at least one Red child

Case 2A X has two Black Children 2A1. T has 2 Black Children 2A2. T’s left child is Red 2A3. T’s right child is Red ** if both of T’s children are Red, we can do either 2A2 or 2A3 P X T

Case 2A1 X and T have 2 Black Children P P X T X T Just recolor X, P and T and move down the tree

Case 2A2 X has 2 Black Children and T’s Left Child is Red Rotate L around T, then L around P Recolor X and P then continue down the tree L P T P X T L L1 L2 X L1 L2

Case 2A3 X has 2 Black Children and T’s Right Child is Red Rotate T around P Recolor X, P, T and R then continue down the tree T P X R T P R R1 R2 X L L R1 R2

Case 2B X has at least one Red child Continue down the tree to the next level If the new X is Red, continue down again If the new X is Black (T is Red, P is Black) Rotate T around P Recolor P and T Back to main case – step 2

Case 2B Diagram P X T Move down the tree. P P X T T X If move to Black child (2B2) Rotate T around P; Recolor P and T Back to step 2, the main case If move to the Red child (2B1) Move down again

Step 3 Eventually, find the node to be deleted – a leaf or a node with one non-null child that is a leaf. Note that if the victim has only1 child, then the victim and the child are of opposite colors. Delete the appropriate node as a Red leaf Step 4 Color the Root Black

Example 1 Delete 10 from this RB Tree 15 6 17 20 12 3 16 10 13 18 23 7 Step 1 – Root has 2 Black children. Color Root Red Descend the tree, moving X to 6

Example 1 (cont’d) 15 X 6 17 20 12 3 16 10 13 18 23 7 One of X’s children is Red (case 2B). Descend down the tree, arriving at 12. Since the new X (12) is also Red (2B1), continue down the tree, arriving at 10.

Example 1 (cont’d) 15 6 17 20 12 3 16 10 X 13 18 23 7 Step 3 -Since 10 is the node to be deleted, replace its value with the value of it’s only child (7) and delete 7’s red node

Example 1 (cont’d) 15 6 17 20 12 3 16 7 13 18 23 The final tree after 7 has replaced 10 and 7’s red node deleted and (step 4) the root has been colored Black.

Example 2 Delete 10 from this RB Tree 15 6 17 20 12 3 16 2 4 10 13 Step 1 – the root does not have 2 Black children. Set X = root and proceed to step 2

Example 2 (cont’d) X 15 6 17 20 12 3 16 2 4 10 13 X has at least one Red child (case 2B). Proceed down the tree, arriving at 6. Since 6 is also Red (case 2B1), continue down the tree, arriving at 12.

Example 2 (cont’d) 15 P 6 17 20 T X 12 3 16 2 4 10 13 X has 2 Black children. X’s sibling (3) also has 2 black children. Case 2A1– recolor X, P, and T and continue down the tree, arriving at 10.

Example 2 (cont’d) 15 6 17 20 P 12 3 16 2 4 10 13 X T X is now the leaf to be deleted, but it’s Black, so back to step 2. X has 2 Black children and T has 2 Black children – case 2A1 Recolor X, P and T. Step 3 -- Now delete 10 as a red leaf. Step 4 -- Recolor the root black

Example 2 Solution 15 6 17 20 12 3 16 2 4 13

Example 3 Delete 11 from this RB Tree 15 10 12 5 3 Valid and unaffected Right subtree 7 11 13 2 4 6 9 Step 1 – root has 2 Black children. Color Root red. Set X to appropriate child of root (10)

Example 3 (cont’d) X X has one Red child (case 2B) 15 X 10 12 5 3 7 11 13 2 4 6 9 X has one Red child (case 2B) Traverse down the tree, arriving at 12.

Example 3 (cont’d) 15 P 10 12 5 T X 3 7 11 13 2 4 6 9 Since we arrived at a black node (case 2B2) assuring T is red and P is black), rotate T around P, recolor T and P Back to step 2

Example 3 (cont’d) 15 5 P 10 X 3 7 T 12 2 4 6 9 11 13 Now X is Black with Red parent and Black sibling. X and T both have 2 Black children (case 2A1) Just recolor X, P and T and continue traversal

Example 3 (cont’d) 15 5 10 P 3 7 12 X T 2 4 6 9 11 13 Having traversed down the tree, we arrive at 11, the leaf to be deleted, but it’s Black, so back to step 2. X and T both have two Black children. Recolor X, P and T. Step 3 -- delete 11 as a red leaf. Step 4 -- Recolor the root black

Example 3 Solution 15 5 10 3 7 12 2 4 6 9 13