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.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Chapter 12 Binary search trees Lee, Hsiu-Hui Ack: This presentation is based on the lecture slides from Hsu, Lih-Hsing, as well as various materials from.
Binary Search Trees Comp 550.
14. Augmenting Data Structures Hsu, Lih-Hsing. Computer Theory Lab. Chapter 13P Dynamic order statistics We shall also see the rank of an element―its.
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.
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,
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
Balanced Trees Balanced trees have height O(lg n).
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.
Red-Black Trees Red-black trees: –Binary search trees augmented with node color –Operations designed to guarantee that the height h = O(lg n)
Red-black tree Lai Ah Fur. Background: AVL trees may require many restructure operations (rotations) to be performed after an element removal, (2,4) trees.
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.
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.
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.
Fundamentals of Algorithms MCS - 2 Lecture # 17. Binary Search Trees.
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.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
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.
BST Trees Saurav Karmakar
Red Black Trees
Red-Black Trees.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
ساختمان داده ها و الگوريتم ها
Slide Sources: CLRS “Intro. To Algorithms” book website
Slide Sources: CLRS “Intro. To Algorithms” book website
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
Design and Analysis of Algorithms
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:

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 basic dynamic-set operations take O(lg n ) time in the worse case. Red-black trees

Computer Theory Lab. Chapter 13P Properties of red-black tree A red-black tree is a binary search tree with one extra bit of storage per node: its color, which can either RED or BLACK. By constraining the way nodes can be colored on any path from the root to a leaf, red-black trees ensure that no such path is more then twice as long as any other, so that three is approximately balanced.

Computer Theory Lab. Chapter 13P.4 Each node of the tree now contains the fields color, key, left, right, and p. If a child or the parent of a node does not exist, the corresponding pointer field of the node contains the value NIL. We shall regard these NIL ’ s as being pointers to external nodes(leaves) of the binary search tree and the normal, key-bearing nodes as being internal nodes of the tree.

Computer Theory Lab. Chapter 13P.5 A binary search tree is a red-black tree if it satisfies the following red-black properties: 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.

Computer Theory Lab. Chapter 13P.6 A red-black tree with black nodes and red nodes shaded.

Computer Theory Lab. Chapter 13P.7

Computer Theory Lab. Chapter 13P.8

Computer Theory Lab. Chapter 13P.9 Lemma 13.1 A red-black tree with n internal nodes has height at most 2lg(n+1).

Computer Theory Lab. Chapter 13P.10 Proof. We start by showing that the subtree rooted at any node x contains at least 2 bh(x) – 1 internal nodes. We prove this claim by induction on the height of x. if the eight of x is 0, then x must be a leaf( NIL [ T ]), and the sub tree rooted at x indeed contains at least 2bh(x) – 1 = 2 0 – 1 =0 internal nodes.

Computer Theory Lab. Chapter 13P.11 For the inductive step, consider a node x that has positive height and is an internal node with two children. Each child has a black- height of either bh(x) or bh(x) – 1, depending on whether its color is red or black, respectively. Since the height of a child of x is less than the height of x itself, we can apply the inductive hypothesis to conclude that each child has at least 2 bh(x)-1 -1 internal nodes. Thus, the subtree rooted at x contains at least (2 bh(x)-1 – 1)+(2 bh(x)-1 – 1) +1 = 2 bh(x) – 1 internal nodes, which proves the claim.

Computer Theory Lab. Chapter 13P.12 To complete the proof of the lemma, let h be the height of the tree. According to property 4, at least half the nodes on any simply path from the root to a leaf, not including the root, must be black. Consequently, the black- height of the root must be at least h/2 ; thus, n  2 h/2 -1. Moving the 1 to the left-hand side and taking logarithms on both sides yields lg(n + 1) ≥ h/2, or h  2lg (n+1).

Computer Theory Lab. Chapter 13P Rotations

Computer Theory Lab. Chapter 13P.14 L EFT -R OTATE (T,x) 1 y ← right[x] 2 right[x] ← left[y] 3 p[left[y]] ← x 4 p[y] ← p[x] 5 If p[x] = nil[T] 6 then root[T] ← y 7else if x = left[p[x]] 8 then left[p[x]] ← y 9 else right[p[x]] ← y 10 left[y] ← x 11 p[x] ← y The code for R IGHT -R OTATE is symmetric. Both L EFT -R OTATE and R IGHT -R OTATE run in O(1) time.

Computer Theory Lab. Chapter 13P.15 An example of L EFT -R OTATE (T,x)

Computer Theory Lab. Chapter 13P Insertion RB-I NSERT ( T, x ) 1 y ← nil[T] 2 x ← root[T] 3 while x ≠ nil[T] 4 do y ← x 5 if key[z] < key[x] 6 then x ← left[x] 7 else x ← right[x] 8 p[z] ← y

Computer Theory Lab. Chapter 13P.17 9 if y = nil[T] 10 then root[T] ← z 11 else if key[z] < key[y] 12 then left[y] ← z 13 else right[y] ← z 14 left[z] ← nil[T] 15 right[z] ← nil[T] 16 color[z] ← RED 17 RB-I NSERT -F IXUP (T, z)

Computer Theory Lab. Chapter 13P.18 RB-I NSERT- F IXUP ( T, z ) 1 while color[p[z]] = RED 2 do if p[z] = left[p[p[z]]] 3 then y ← right[p[p[z]]] 4if color[y] = RED 5 then color[p[z]] ← BLACK Case 1 6 color[y] ← BLACK Case 1 7 color[p[p[z]]] ← RED Case 1 8 z ← p[p[z]] Case 1

Computer Theory Lab. Chapter 13P.19 9 else if z = right[p[z]] 10then z ← p[p[z]] Case 2 11 L EFT -R OTATE (T,z) Case 2 12 color[p[z]] ← BLACK Case 3 13color[p[p[z]]] ← RED Case 3 14R IGHT -R OTATE (T,p[p[z]]) Case 3 15else (same as then clause with “right” and “left” exchanged) 16 color[root[T]] ← BLACK

Computer Theory Lab. Chapter 13P.20 The operation of RB-I NSERT - F IXUP

Computer Theory Lab. Chapter 13P.21

Computer Theory Lab. Chapter 13P.22 case 1 of the RB-I NSERT

Computer Theory Lab. Chapter 13P.23 case 2 and 3 of RB-I NSERT

Computer Theory Lab. Chapter 13P.24 Analysis RB-INSERT take a total of O(lg n) time. It never performs more than two rotations, since the while loop terminates if case 2 or case 3 executed.

Computer Theory Lab. Chapter 13P Deletion RB-D ELETE (T,z) 1 if left[z] = nil[T] or right[z] = nil[T] 2 then y ← z 3 else y ← T REE -S UCCESSOR (z) 4 if left[y] ≠ nil[T] 5 then x ← left[y] 6 else x ← right[y] 7 p[x] ← p[y] 8 if p[y] = nil[T]

Computer Theory Lab. Chapter 13P.26 9 then root[T] ← x 10 else if y = left[p[y]] 11then left[p[y]] ← x 12else right[p[y]] ← x 13 if y ≠ z 14 then key[z] ← key[y] 15 copy y’s satellite data into z 16 if color[y] = BLACK 17then RB-D ELETE -F IXUP (T, x) 18 return y

Computer Theory Lab. Chapter 13P.27 RB-D ELETE -F IXUP (T, x) RB-D ELETE -F IXUP 1while x ≠ root[T] and color[x] = BLACK 2 do if x = left[p[x]] 3then w ← right[p[x]] 4 if color[w] = RED 5then color[w] ← BLACK Case1 6 color[p[x]] = RED Case1 7 LEFT-ROTATE(T,p[x]) Case1 8 w ← right[p[x]] Case1

Computer Theory Lab. Chapter 13P.28 9if color[right[w]] = BLACK and color[right[w]= BLACK 10 then color[w] ← RED Case2 11 x ← p[x] Case2 12 else if color[right[w]] = BLACK 13 then color[left[w]] ← BLACK Case3 14 color[w] ← RED Case3 15 R IGHT -R OTATE (T,w) Case3 16w ← right[p[x]] Case3 17 color[w] ← color[p[x]] Case4

Computer Theory Lab. Chapter 13P color[p[x]] ← BLACK Case4 19 color[right[w]] ← BLACK Case4 20 L EFT -R OTATE (T,p[x]) Case4 21 x ← root[T] Case4 22 else (same as then clause with “right” and “left” exchanged) 23 color[x] ← BLACK

Computer Theory Lab. Chapter 13P.30 the case in the while loop of RB-D ELETE -F IXUP

Computer Theory Lab. Chapter 13P.31

Computer Theory Lab. Chapter 13P.32 Analysis The RB-D ELETE -F IXUP takes O(lg n) time and performs at most three rotations. The overall time for RB-DELETE is therefore also O(lg n)