Red-Black Trees Comp 550.

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.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Binary Search Trees Comp 550.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
2IL50 Data Structures Spring 2015 Lecture 8: Augmenting Data Structures.
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)
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.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
13. Red-Black Tree Hsu, Lih-Hsing. Computer Theory Lab. Chapter 13P.2 One of many search-tree schemes that are “ balanced ” in order to guarantee that.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
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.
© 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1.
CS 3343: Analysis of Algorithms Lecture 16: Binary search trees & red- black trees.
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.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
Chapter 13 Red-Black Trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from the.
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: 
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION.
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.
CS 473Lecture X1 CS473-Algorithms Lecture RBT - DELETION.
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.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1 Algorithms CSCI 235, Fall 2015 Lecture 24 Red Black Trees.
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.
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
Binary Search Trees What is a binary search tree?
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 Motivations
Red-Black Trees.
CMSC 341 (Data Structures)
Red Black Trees.
Lecture 9 Algorithm Analysis
Red Black Tree Essentials
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
CS 583 Analysis of Algorithms
Red Black Tree Essentials
Red-Black Trees.
Analysis of Algorithms CS 477/677
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Red-Black Trees CS302 Data Structures
Presentation transcript:

Red-Black Trees Comp 550

Red-black Tree Binary search tree + 1 bit per node: the attribute color, which is either red or black. All other attributes of BSTs are inherited: key, left, right, and p. All empty trees (leaves) are colored black. We use a single sentinel, nil, for all the leaves of red-black tree T, with color[nil] = black. The root’s parent is also nil[T ]. Comp 550

Red-black Properties Every node is either red or black. The root is black. Every leaf (nil) is black. If a node is red, then both its children are black. For each node, all paths from the node to descendant leaves contain the same number of black nodes. Comp 550

Red-black Tree – Example 26 Remember: every internal node has two children, even though nil leaves are not usually shown. 17 41 nil[T] 30 47 38 50 Comp 550

Height of a Red-black Tree Height of a node: h(x) = number of edges in a longest path to a leaf. Black-height of a node x, bh(x): bh(x) = number of black nodes (including nil[T ]) on the path from x to leaf, not counting x. Black-height of a red-black tree is the black-height of its root. By Property 5, black height is well defined. Comp 550

Height of a Red-black Tree bh=2 Example: Height of a node: h(x) = # of edges in a longest path to a leaf. Black-height of a node bh(x) = # of black nodes on path from x to leaf, not counting x. How are they related? bh(x) ≤ h(x) ≤ 2 bh(x) 26 h=3 bh=2 h=1 bh=1 17 41 h=2 bh=1 h=2 bh=1 30 47 h=1 bh=1 38 h=1 bh=1 50 nil[T] Comp 550

Lemma “RB Height” Consider a node x in an RB tree: The longest descending path from x to a leaf has length h(x), which is at most twice the length of the shortest descending path from x to a leaf. Proof: # black nodes on any path from x = bh(x) (prop 5) # nodes on shortest path from x, s(x). (prop 1) But, there are no consecutive red (prop 4), and we end with black (prop 3), so h(x) ≤ 2 bh(x). Thus, h(x) ≤ 2 s(x). QED Comp 550

Bound on RB Tree Height Lemma: The subtree rooted at any node x has  2bh(x)–1 internal nodes. Proof: By induction on height of x. Base Case: Height h(x) = 0  x is a leaf  bh(x) = 0. Subtree has 20–1 = 0 nodes.  Induction Step: Height h(x) = h > 0 and bh(x) = b. Each child of x has height h - 1 and black-height either b (child is red) or b - 1 (child is black). By ind. hyp., each child has  2bh(x)– 1–1 internal nodes. Subtree rooted at x has  2 (2bh(x) – 1 – 1)+1 = 2bh(x) – 1 internal nodes. (The +1 is for x itself.) Comp 550

Bound on RB Tree Height Lemma: The subtree rooted at any node x has  2bh(x)–1 internal nodes. Lemma 14.1: A red-black tree with n internal nodes has height at most 2 lg(n+1). Proof: By the above lemma, n  2bh – 1, and since bh h/2, we have n  2h/2 – 1.  h  2 lg(n + 1). Comp 550

Points to Remember Every internal node has 2 children But leaf nodes are typically not shown Leaves carry no data Red-black trees are a variation of binary search trees to ensure that the tree is balanced. Height is O(lg n), where n is the number of nodes. Operations take O(lg n) time in the worst case. Comp 550

Operations on RB Trees All operations can be performed in O(lg n) time. The query operations, which don’t modify the tree, are performed in exactly the same way as they are in BSTs. Insertion and Deletion are not straightforward. Why? Comp 550

Rotations y x    Left-Rotate(T, x) Right-Rotate(T, y) Comp 550

Rotations Rotations are the basic tree-restructuring operation for almost all balanced search trees. Rotation takes a red-black-tree and a node, Changes pointers to change the local structure, and Won’t violate the binary-search-tree property. Left rotation and right rotation are inverses. y x    Left-Rotate(T, x) Right-Rotate(T, y) Comp 550

Left Rotation – Pseudo-code Left-Rotate (T, x) y  right[x] // Set y. right[x]  left[y] //Turn y’s left subtree into x’s right subtree. if left[y]  nil[T ] then p[left[y]]  x p[y]  p[x] // Link x’s parent to y. if p[x] = nil[T ] then root[T ]  y else if x = left[p[x]] then left[p[x]]  y else right[p[x]]  y left[y]  x // Put x on y’s left. p[x]  y y x    Left-Rotate(T, x) Right-Rotate(T, y) Comp 550

Rotation The pseudo-code for Left-Rotate assumes that right[x]  nil[T ], and root’s parent is nil[T ]. Left Rotation on x, makes x the left child of y, and the left subtree of y into the right subtree of x. Pseudocode for Right-Rotate is symmetric: exchange left and right everywhere. Time: O(1) for both Left-Rotate and Right-Rotate, since a constant number of pointers are modified. Comp 550

Reminder: Red-black Properties Every node is either red or black. The root is black. Every leaf (nil) is black. If a node is red, then both its children are black. For each node, all paths from the node to descendant leaves contain the same number of black nodes. Comp 550

Insertion in RB Trees Insertion must preserve all red-black properties. Should an inserted node be colored Red? Black? Basic steps: Use Tree-Insert from BST (slightly modified) to insert a node x into T. Procedure RB-Insert(x). Color the node x red. Fix the modified tree by re-coloring nodes and performing rotation to preserve RB tree property. Procedure RB-Insert-Fixup. Comp 550

Insertion How does it differ from the Tree-Insert procedure of BSTs? RB-Insert(T, z) y  nil[T] x  root[T] while x  nil[T] do y  x if key[z] < key[x] then x  left[x] else x  right[x] p[z]  y if y = nil[T] then root[T]  z else if key[z] < key[y] then left[y]  z else right[y]  z RB-Insert(T, z) Contd. left[z]  nil[T] right[z]  nil[T] color[z]  RED RB-Insert-Fixup (T, z) How does it differ from the Tree-Insert procedure of BSTs? Which of the RB properties might be violated? Fix the violations by calling RB-Insert-Fixup. Comp 550

Insertion – Fixup Problem: we may have one pair of consecutive reds where we did the insertion. Solution: rotate it up the tree and away… Three cases have to be handled… http://gauss.ececs.uc.edu/Users/Franco/RedBlackTester/redblack.html Comp 550

Insertion – Fixup RB-Insert-Fixup (T, z) while color[p[z]] = RED do if p[z] = left[p[p[z]]] then y  right[p[p[z]]] if color[y] = RED then color[p[z]]  BLACK // Case 1 color[y]  BLACK // Case 1 color[p[p[z]]]  RED // Case 1 z  p[p[z]] // Case 1 Comp 550

Insertion – Fixup RB-Insert-Fixup(T, z) (Contd.) else if z = right[p[z]] // color[y]  RED then z  p[z] // Case 2 LEFT-ROTATE(T, z) // Case 2 color[p[z]]  BLACK // Case 3 color[p[p[z]]]  RED // Case 3 RIGHT-ROTATE(T, p[p[z]]) // Case 3 else (if p[z] = right[p[p[z]]])(same as 10-14 with “right” and “left” exchanged) color[root[T ]]  BLACK Comp 550

Correctness Loop invariant: At the start of each iteration of the while loop, z is red. If p[z] is the root, then p[z] is black. There is at most one red-black violation: Property 2: z is a red root, or Property 4: z and p[z] are both red. Comp 550

Correctness – Contd. Initialization:  Termination: The loop terminates only if p[z] is black. Hence, property 4 is OK. The last line ensures property 2 always holds. Maintenance: We drop out when z is the root (since then p[z] is sentinel nil[T ], which is black). When we start the loop body, the only violation is of property 4. There are 6 cases, 3 of which are symmetric to the other 3. We consider cases in which p[z] is a left child. Let y be z’s uncle (p[z]’s sibling). Comp 550

Case 1 – uncle y is red p[p[z]] new z C C p[z] y A D A D z       B B z is a right child here. Similar steps if z is a left child.     p[p[z]] (z’s grandparent) must be black, since z and p[z] are both red and there are no other violations of property 4. Make p[z] and y black  now z and p[z] are not both red. But property 5 might now be violated. Make p[p[z]] red  restores property 5. The next iteration has p[p[z]] as the new z (i.e., z moves up 2 levels). Comp 550

Case 2 – y is black, z is a right child p[z] p[z] A  y B  y z  z A  B     Left rotate around p[z], p[z] and z switch roles  now z is a left child, and both z and p[z] are red. Takes us immediately to case 3. Comp 550

Case 3 – y is black, z is a left child p[z] A C B  y z     A    Make p[z] black and p[p[z]] red. Then right rotate on p[p[z]]. Ensures property 4 is maintained. No longer have 2 reds in a row. p[z] is now black  no more iterations. Comp 550

RB-Insert Example Case 1 (the child x, its parent and its uncle y are red): Change parent and uncle to black Change grandparent to red (preserves black height) Make grandparent new x, grandparent’s uncle new y Comp 550

RB-Insert Example Case 2 (x and its parent are red, y is black, and x is an “inside child”): Rotate outwards on x’s parent Former parent becomes new x Old x becomes parent Comp 550

RB-Insert Example Case 3 (x and its parent are red, y is black, and x is an “outside child”): Rotate “away from x” around x’s grandparent Color x’s parent black Now x’s old grandparent is x’s sibling; color it red to maintain the right number of black nodes down each path Comp 550

Algorithm Analysis O(lg n) time to get through RB-Insert up to the call of RB-Insert-Fixup. Within RB-Insert-Fixup: Each iteration takes O(1) time. Each iteration but the last moves z up 2 levels. O(lg n) levels  O(lg n) time. Thus, insertion in a red-black tree takes O(lg n) time. Note: there are at most 2 rotations overall. Comp 550

Deletion Deletion, like insertion, should preserve all the RB properties. The properties that may be violated depends on the color of the deleted node. Red – OK. Why? Black? Steps: Do regular BST deletion. Fix any violations of RB properties that may result. Comp 550

Deletion RB-Delete(T, z) if left[z] = nil[T] or right[z] = nil[T] then y  z else y  TREE-SUCCESSOR(z) if left[y] = nil[T ] then x  left[y] else x  right[y] p[x]  p[y] // Do this, even if x is nil[T] Comp 550

Deletion RB-Delete (T, z) (Contd.) if p[y] = nil[T ] then root[T ]  x else if y = left[p[y]] then left[p[y]]  x else right[p[y]]  x if y = z then key[z]  key[y] copy y’s satellite data into z if color[y] = BLACK then RB-Delete-Fixup(T, x) return y The node passed to the fixup routine is the lone child of the spliced up node, or the sentinel. Comp 550

RB Properties Violation If y is black, we could have violations of red-black properties: Prop. 1. OK. Prop. 2. If y is the root and x is red, then the root has become red. Prop. 3. OK. Prop. 4. Violation if p[y] and x are both red. Prop. 5. Any path containing y now has 1 fewer black node. Comp 550

RB Properties Violation Prop. 5. Any path containing y now has 1 fewer black node. Correct by giving x an “extra black.” Add 1 to count of black nodes on paths containing x. Now property 5 is OK, but property 1 is not. x is either doubly black (if color[x] = BLACK) or red & black (if color[x] = RED). The attribute color[x] is still either RED or BLACK. No new values for color attribute. In other words, the extra blackness on a node is by virtue of x pointing to the node. Remove the violations by calling RB-Delete-Fixup. Comp 550

Deletion – Fixup RB-Delete-Fixup(T, x) while x  root[T ] and color[x] = BLACK do if x = left[p[x]] then w  right[p[x]] if color[w] = RED then color[w]  BLACK // Case 1 color[p[x]]  RED // Case 1 LEFT-ROTATE(T, p[x]) // Case 1 w  right[p[x]] // Case 1 Comp 550

RB-Delete-Fixup(T, x) (Contd.) /* x is still left[p[x]] */ if color[left[w]] = BLACK and color[right[w]] = BLACK then color[w]  RED // Case 2 x  p[x] // Case 2 else if color[right[w]] = BLACK then color[left[w]]  BLACK // Case 3 color[w]  RED // Case 3 RIGHT-ROTATE(T,w) // Case 3 w  right[p[x]] // Case 3 color[w]  color[p[x]] // Case 4 color[p[x]]  BLACK // Case 4 color[right[w]]  BLACK // Case 4 LEFT-ROTATE(T, p[x]) // Case 4 x  root[T ] // Case 4 else (same as then clause with “right” and “left” exchanged) color[x]  BLACK Comp 550

Deletion – Fixup Idea: Move the extra black up the tree until x points to a red & black node  turn it into a black node, x points to the root  just remove the extra black, or We can do certain rotations and recolorings and finish. Within the while loop: x always points to a nonroot doubly black node. w is x’s sibling. w cannot be nil[T ], since that would violate property 5 at p[x]. 8 cases in all, 4 of which are symmetric to the other. Comp 550

Case 1 – w is red p[x] B D w x B E A D x A C C E  x  new w   A C C E         w must have black children. Make w black and p[x] red (because w is red p[x] couldn’t have been red). Then left rotate on p[x]. New sibling of x was a child of w before rotation  must be black. Go immediately to case 2, 3, or 4. Comp 550

Case 2 – w is black, both w’s children are black p[x] c new x c B B w x A D A D     C E C E         Take 1 black off x ( singly black) and off w ( red). Move that black to p[x]. Do the next iteration with p[x] as the new x. If entered this case from case 1, then p[x] was red  new x is red & black  color attribute of new x is RED  loop terminates. Then new x is made black in the last line. Comp 550

Case 3 – w is black, w’s left child is red, w’s right child is black x x new w A D A C     D C E   E       Make w red and w’s left child black. Then right rotate on w. New sibling w of x is black with a red right child  case 4. Comp 550

Case 4 – w is black, w’s right child is red x B E A D  x    c’ A C C E         Make w be p[x]’s color (c). Make p[x] black and w’s right child black. Then left rotate on p[x]. Remove the extra black on x ( x is now singly black) without violating any red-black properties. All done. Setting x to root causes the loop to terminate. Comp 550

Explanation of RB-Delete-Fixup x is the current node passed to the RB-Delete-Fixup, which is called within RB-Delete. Darkened nodes are black, heavily shaded nodes are red, lightly shaded nodes (represented as c and c’) are either red or black. w is a sibling of x. α,β, …, ζ represent arbitrary sub-trees. In each case (1, 2, 3, and 4), the before configuration is transformed to the after configuration by changing color and/or performing rotation. Case 1 is transformed into case 2, 3, or 4 by exchanging the colors of nodes B and D, performing a left rotation. In case 2, extra black represented by pointer x is moved up the tree by coloring node D red and pointing x to B Case 3 is transformed to case 4 by exchanging color of C and D, plus performing a right rotation. In case 4, extra black represented by x can be moved by exchanging colors, and performing a left rotation. Then the loop terminates. Comp 550

Analysis O(lg n) time to get through RB-Delete up to the call of RB-Delete-Fixup. Within RB-Delete-Fixup: Case 2 is the only case in which more iterations occur. x moves up 1 level. Hence, O(lg n) iterations. Each of cases 1, 3, and 4 has 1 rotation   3 rotations in all. Hence, O(lg n) time. Comp 550