Red-Black Trees CS302 Data Structures Dr. George Bebis.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Introduction to Algorithms Red-Black Trees
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.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
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.
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.
CS 3343: Analysis of Algorithms Lecture 16: Binary search trees & red- black trees.
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.
Red-Black Trees Red-black trees: –Binary search trees augmented with node color –Operations designed to guarantee that the height h = O(lg n)
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
Red-Black Trees Comp 550.
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: 
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
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.
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)
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.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
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.
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
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees Motivations
CS200: Algorithms Analysis
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees.
CMSC 341 (Data Structures)
Red Black Trees.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees.
CS 583 Analysis of Algorithms
Red-Black Trees.
Analysis of Algorithms CS 477/677
Chapter 12&13: Binary Search Trees (BSTs)
Red-Black Trees CS302 Data Structures
Presentation transcript:

Red-Black Trees CS302 Data Structures Dr. George Bebis

Red-Black Trees Binary search tree with an additional attribute for its nodes: color which can be red or black Constrains the way nodes can be colored on any path from the root to a leaf. Balanced tree  O(logN)

Example: RED-BLACK-TREE For convenience, we add NIL nodes and refer to them as the leaves of the tree. –Color[NIL] = BLACK NIL

Red-Black-Trees Properties (**Binary search tree property is satisfied**) 1.Every node is either red or black 2.The root is black 3.Every leaf ( NIL ) is black 4.If a node is red, then both its children are black No two consecutive red nodes on a simple path from the root to a leaf 5.For each node, all paths from that node to a leaf contain the same number of black nodes

Definitions Height of a node: the number of edges in the longest path to a leaf. Black-height bh(x) of a node x: the number of black nodes (including NIL) on the path from x to a leaf, not counting x 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

Height of Red-Black-Trees A red-black tree with n internal nodes has height at most 2log(N+1) Need to prove two claims first …

Claim 1 Any node x with height h(x) has bh(x) ≥ h(x)/2 Proof –By property 4, at most h/2 red nodes on the path from the node to a leaf –Hence at least h/2 are black 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

Claim 2 The subtree rooted at any node x contains at least 2 bh(x) - 1 internal nodes 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

Claim 2 (cont’d) Proof: By induction on h[ x] Basis: h[x] = 0  x is a leaf ( NIL[T] )  bh(x) = 0  # of internal nodes: = 0 Inductive Hypothesis: assume it is true for h[x]=h-1 NIL x

Claim 2 (cont’d) Inductive step: Prove it for h[x]=h internal nodes at x= internal nodes at l + internal nodes at r + 1 Using inductive hypothesis: internal nodes at x ≥ (2 bh(l) – 1) + (2 bh(r) – 1) + 1 x l r h h-1

Claim 2 (cont’d) Let bh(x) = b, then any child y of x has: –bh (y) = b (if the child is red), or b - 1 (if the child is black) x y1y1 y2y2 bh = 2 bh = 1 NIL bh(y)≥bh(x)-1

Claim 2 (cont’d) So, back to our proof: internal nodes at x ≥ (2 bh(l) – 1) + (2 bh(r) – 1) + 1 ≥ (2 bh(x) - 1 – 1) + (2 bh(x) - 1 – 1) + 1 = 2 · (2 bh(x) ) + 1 = 2 bh(x) - 1 internal nodes x l r h h-1

Height of Red-Black-Trees (cont’d) A red-black tree with N internal nodes has height at most 2log(N+1). Proof: N Solve for h : N + 1 ≥ 2 h/2 log(N + 1) ≥ h/2  h ≤ 2 log(N + 1) root l r height(root) = h bh(root) = b number of internal nodes ≥ 2 b - 1 ≥ 2 h/2 - 1 Claim 2 Claim 1

Operations on Red-Black-Trees Non-modifying binary-search-tree operations (e.g., RetrieveItem, GetNextItem, ResetTree), run in O(h)=O(logN) time What about InsertItem and DeleteItem? –They will still run on O(logN) –Need to guarantee that the modified tree will still be a valid red-black tree

15 InsertItem What color to make the new node? Red? Let’s insert 35! –Property 4 is violated: if a node is red, then both its children are black Black? Let’s insert 14! –Property 5 is violated: all paths from a node to its leaves contain the same number of black nodes

DeleteItem What color was the node that was removed? Black? 1.Every node is either red or black 2.The root is black 3.Every leaf ( NIL ) is black 4.If a node is red, then both its children are black 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes OK! Not OK! Could create two red nodes in a row Not OK! Could change the black heights of some nodes Not OK! If removing the root and the child that replaces it is red

Rotations Operations for re-structuring the tree after insert and delete operations –Together with some node re-coloring, they help restore the red-black-tree property –Change some of the pointer structure –Preserve the binary-search tree property Two types of rotations: –Left & right rotations

Left Rotations Assumptions for a left rotation on a node x : –The right child y of x 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

19 Example: LEFT-ROTATE

Right Rotations Assumptions for a right rotation on a node x : –The left child x of y 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

InsertItem 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 i.e., use RB-INSERT-FIXUP

RB Properties Affected by Insert 1.Every node is either red or black 2.The root is black 3.Every leaf ( NIL ) is black 4.If a node is red, then both its children are black 5.For each node, all paths from the node to descendant leaves contain the same number of black nodes OK! If z is the root  not OK OK! If p(z) is red  not OK z and p(z) are both red OK!

23 RB-INSERT-FIXUP Case 1: z ’s “uncle” (y) is red (z could be either left or right child) Idea: p[p[z]] ( z ’s grandparent) must be black. color p[z]  black color y  black color p[p[z]]  red z = p[p[z]] –Push the “red” violation up the tree

RB-INSERT-FIXUP Case 2: z ’s “uncle” (y) is black z is a left child Case 2 Idea: color p[z]  black color p[p[z]]  red RIGHT-ROTATE(T, p[p[z]]) No longer have 2 reds in a row p[z] is now black

25 RB-INSERT-FIXUP Case 3: z ’s “uncle” ( y ) is black z is a right child Idea: z  p[z] LEFT-ROTATE( T, z)  now z is a left child, and both z and p[z] are red  case 2 Case 3Case 2

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 2 z and p[z] are red z’s uncle y is black z is a left child z

RB-INSERT-FIXUP (T, z) 1.while color[p[z]] = RED 2. do if p[z] = left[p[p[z]]] 3. then y ← right[p[p[z]]] 4. if color[y] = RED 5. then Case1 6. else if z = right[p[z]] 7. then Case2 8. Case3 9. else (same as then clause with “right” and “left” exchanged) 10.color[root[T]] ← BLACK The while loop repeats only when case1 is executed: O(logN) times Set the value of x’s “uncle” We just inserted the root, or The red violation reached the root

Analysis of InsertItem Inserting the new element into the tree O(logN) RB-INSERT-FIXUP –The while loop repeats only if CASE 1 is executed –The number of times the while loop can be executed is O(logN) Total running time of InsertItem: O(logN)

DeleteItem Delete as usually, then re-color/rotate A bit more complicated though … 29

Problems

Problems What red-black tree property is violated in the tree below? How would you restore the red-black tree property in this case? –Property violated: if a node is red, both its children are black –Fixup: color 7 black, 11 red, then right-rotate around z

Problems Let a, b, c be arbitrary nodes in subtrees , ,  in the tree below. How do the depths of a, b, c change when a left rotation is performed on node x? –a: increases by 1 –b: stays the same –c: decreases by 1

Problems When we insert a node into a red-black tree, we initially set the color of the new node to red. Why didn’t we choose to set the color to black? Would inserting a new node to a red-black tree and then immediately deleting it, change the tree?