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: 

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Definitions and Bottom-Up Insertion
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
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.
Balanced Search Trees CS Data Structures Mehmet H Gunes Modified from authors’ slides.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
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.
Comp 122, Spring 2004 Red-Black Trees. redblack - 2 Comp 122, Spring 2004 Red-black trees: Overview  Red-black trees are a variation of binary search.
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,
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
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.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
Introduction to Algorithms Jiafen Liu Sept
Red-Black Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
Red-Black Trees Comp 550.
Balanced Search Trees 3.4 – 3.7 Team names Majed Suhaim Ahmed Sulaiman M Alharbi.
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.
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in They came up with the AVL tree. In.
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.
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.
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.
Definitions and Bottom-Up Insertion
Balanced Search Trees Modified from authors’ slides.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red-Black Trees.
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Motivations
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
CMSC 341 (Data Structures)
Red Black Trees.
Lecture 9 Algorithm Analysis
Red-Black Trees Bottom-Up Deletion.
Red Black Tree Essentials
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees.
Red Black Trees Top-Down Deletion.
Red Black Tree Essentials
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
Red-Black Trees Bottom-Up Deletion.
Analysis of Algorithms CS 477/677
Red-black tree properties
Red Black Trees Top-Down Deletion.
Red-Black Trees CS302 Data Structures
Presentation transcript:

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:  Every node is colored either Red or Black.  Each NULL pointer is considered to be a Black “node”.  If a node is Red, then both of its children are Black.  Every path from a node to a NULL contains the same number of Black nodes.  By convention, the root is Black Definition: The black-height of a node, X, in a red-black tree is the number of Black nodes on any path to a NULL, not counting X.

8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 3 A Red-Black Tree with NULLs shown Black-Height of the tree (the root) = 3 Black-Height of node “X” = 2 X

8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 4 A Red-Black Tree with Black-Height = 3

8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 5 Black Height of the tree? Black Height of X? X

8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 6 Theorem :– In a red-black tree, at least half the nodes on any path from the root to a NULL must be Black. Proof – If there is a Red node on the path, there must be a corresponding Black node. Algebraically this theorem means bh( x ) ≥ h/2 Claim

7 Claim (cont’d) example NIL h = 4 bh = 2 h = 3 bh = 2 h = 2 bh = 1 h = 1 bh = 1 h = 1 bh = 1 h = 2 bh = 1 h = 1 bh = 1

8 Rotations Operations for re-structuring the tree after insert and delete operations on red-black trees Rotations take a red-black-tree and a node within the tree and:  Together with some node re-coloring they help restore the red-black-tree property  Change some of the pointer structure  Do not change the binary-search tree property Two types of rotations:  Left & right rotations

9 Left Rotations Assumptions for a left rotation on a node x :  The right child of x (y) is not NIL Idea:  Pivots around the link from x to y  Makes y the new root of the subtree  x becomes y ’s left child  y ’s left child becomes x ’s right child

10 Example: LEFT-ROTATE

11 LEFT-ROTATE(T, x) 1. y ← right[x] ►Set y 2. right[x] ← left[y] ► y’s left subtree becomes x’s right subtree 3. if left[y]  NIL 4. then p[left[y]] ← x ► Set the parent relation from left[y] to x 5. p[y] ← p[x] ► The parent of x becomes the parent of y 6. if p[x] = NIL 7. then root[T] ← y 8. else if x = left[p[x]] 9. then left[p[x]] ← y 10. else right[p[x]] ← y 11. left[y] ← x ► Put x on y’s left 12. p[x] ← y ► y becomes x’s parent

12 Right Rotations Assumptions for a right rotation on a node x :  The left child of y (x) is not NIL Idea:  Pivots around the link from y to x  Makes x the new root of the subtree  y becomes x ’s right child  x ’s right child becomes y ’s left child

13 Example: RIGHT-ROTATE

14 Insertion Goal:  Insert a new node z into a red-black-tree Idea:  Insert node z into the tree as for an ordinary binary search tree  Color the node red  Restore the red-black-tree properties

8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 15 Insertion What Red-Black property may be violated?  Every node is Red or Black?  NULLs are Black?  If node is Red, both children must be Black?  Every path from node to descendant NULL must contain the same number of Blacks?

8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 16 Insertion Insert node; Color it Red; X is pointer to it Cases 0: X is the root -- color it Black 1: Both parent and uncle are Red -- color parent and uncle Black, color grandparent Red. Point X to grandparent and check new situation. 2 (zig-zag): Parent is Red, but uncle is Black. X and its parent are opposite type children -- color grandparent Red, color X Black, rotate left(right) on parent, rotate right(left) on grandparent 3 (zig-zig): Parent is Red, but uncle is Black. X and its parent are both left (right) children -- color parent Black, color grandparent Red, rotate right(left) on grandparent

8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 17 X P G U P G U Case 1 – U is Red Just Recolor and move up X Both parent and uncle are Red  color parent and uncle Black, color grandparent Red. Point X to grandparent and check new situation.

8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 18 X P G U S X P G S U Case 2 – Zig-Zag Double Rotate X around P; X around G Recolor G and X (zig-zag): Parent is Red, but uncle is Black. X and its parent are opposite type children  color grandparent Red, color X Black, rotate left(right) on parent, rotate right(left) on grandparent

8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 19 X P G U S P X G S U Case 3 – Zig-Zig Single Rotate P around G Recolor P and G Parent is Red, but uncle is Black. X and its parent are both left (right) children  color parent Black, color grandparent Red, rotate right(left) on grandparent

Insertion into a redblack tree 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 20 Example: Insert x in tree. Color x red. Only red-black property 3 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Insert x =15.

Insertion into a redblack tree 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 21

Insertion into a redblack tree 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 22

Insertion into a redblack tree 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 23

Insertion into a redblack tree 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 24

8/3/2007 UMBC CSMC 341 Red-Black-Trees Insert 4 into this R-B Tree

26 Example 11 Insert y z Case 1 y z and p[z] are both red z’s uncle y is red z z and p[z] are both red z’s uncle y is black z is a right child Case z y Case 3 z and p[z] are red z’s uncle y is black z is a left child z

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 just one child, replace it with that child 3. 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? 1. If the delete node is red? Not a problem – no RB properties violated 2. If the deleted node is black? If the node is not the root, deleting it will change the black-height along some path

Terminology  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.

Step 1 – Examine the root 1. If both of the root’s children are Black a. Make the root Red b. Move X to the appropriate child of the root c. Proceed to step 2 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

P T X 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

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

Case 2A2 P T X L 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 L1L2 P T X L L1L2

Case 2A3 P T X 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 R1R2 P R X T R1 R LL

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 XT Move down the tree. P XT P TX 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. Delete the appropriate node as a Red leaf Step 4 Color the Root Black

Example 1 Delete 10 from this RB Tree Step 1 – Root has 2 Black children. Color Root Red Descend the tree, moving X to 6

Example 1 (cont’d) 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. X

Example 1 (cont’d) Step 3 -Since 10 is the node to be deleted, replace it’s value with the value of it’s only child (7) and delete 7’s red node X

Example 1 (cont’d) 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 Step 1 – the root does not have 2 Black children. Color the root red, Set X = root and proceed to step 2

Example 2 (cont’d) X 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) X 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. P T

Example 2 (cont’d) P XT 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

Example 3 Delete 11 from this RB Tree Valid and unaffected Right subtree 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) Traverse down the tree, arriving at 12.

Example 3 (cont’d) X 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 P T 2

Example 3 (cont’d) X P T 2 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) X P T 2 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