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.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

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.
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.
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.
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.
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 tree Recall binary search tree –Key values in the left subtree = the node value Operations:
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.
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.
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.
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.
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
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:

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 web.

chap13Hsiu-Hui Lee2 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. e.g. AVL trees Red-black trees Balanced search Trees

chap13Hsiu-Hui Lee3 Red-Black Tree a variation of binary search trees with one extra bit of storage per node: its color, which can either RED or BLACK. 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.

chap13Hsiu-Hui Lee4 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 x to descendant leaves contain the same number of black nodes (i.e. black-height(x)).

chap13Hsiu-Hui Lee5 Example of a red-black tree

chap13Hsiu-Hui Lee6 Example of a red-black tree 1. Every node is either red or black.

chap13Hsiu-Hui Lee7 Example of a red-black tree The root and leaves (NIL’s) are black.

chap13Hsiu-Hui Lee8 4. If a node is red, then its children are black. ?? Means ( If a node is red, then its parent is black. ) Example of a red-black tree

chap13Hsiu-Hui Lee9 5. All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x). Example of a red-black tree

chap13Hsiu-Hui Lee10 black-height of the node: bh(x) # of black nodes on any path from, but not including, a node x down to a leaf black-height of a RB tree = black-height of its root

chap13Hsiu-Hui Lee11

chap13Hsiu-Hui Lee12 Leaves and the root’s parent omitted entirely

chap13Hsiu-Hui Lee13 Height and black-height Height of a node x: h(x) is the number of edges in a longest path to a leaf. Black-height of a node x: bh(x) is the number of black nodes (including nil[T ]) on the path from x to leaf, not counting x.

chap13Hsiu-Hui Lee14 Lemma 13.0a The subtree rooted at any node x contains ≥ 2 bh(x) − 1 internal nodes. Proof By induction on height of x. Basis: Height of x = 0 ⇒ x is a leaf ⇒ bh(x) = 0. The subtree rooted at x has 0 internal nodes. 2 0 − 1 = 0. Inductive step: Let the height of x be h and bh(x) = b. Any child of x has height h − 1 and black-height either b (if the child is red) or b − 1 (if the child is black). By the inductive hypothesis, each child has ≥ 2 bh(x)-1 − 1 internal nodes. Thus, the subtree rooted at x contains ≥ 2 · (2 bh(x)-1 − 1 )+1 = 2 bh(x) −1 internal nodes. (The +1 is for x itself.)

chap13Hsiu-Hui Lee15 Lemma 13.0b Any node with height h has black – height at least h/2. Proof By property 4 (If a node is red, then both its children are black), ≤ h/2 nodes on the path from the node to a leaf are red. Hence ≥ h/2 are black.

chap13Hsiu-Hui Lee16 Lemma 13.1 A red-black tree with n internal nodes has height at most 2lg(n+1). Proof Let h and b be the height and black-height of the root, respectively. By the above two claims, n ≥ 2 b − 1 ≥ 2 h/2 − 1. Adding 1 to both sides and then taking logs gives lg(n + 1) ≥ h/2, which implies that h ≤ 2 lg(n + 1).

chap13Hsiu-Hui Lee Rotations Left-Rotation makes y ’ s left subtree into x ’ s right subtree. Right-Rotation makes x ’ s right subtree into y ’ s left subtree.

chap13Hsiu-Hui Lee18 L EFT -R OTATE (T, x) 1.y ← right[x] ✄ Set y. 2.right[x] ← left[y] ✄ Turn y ’ s left subtree into x ’ s right subtree. 3.if left[y] ≠ nil[T ] 4. then p[left[y]] ← x 5.p[y] ← p[x] ✄ Link x ’ s parent to y. 6.if p[x] = nil[T ] 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 The code for R IGHT -R OTATE is symmetric. Both L EFT -R OTATE and R IGHT -R OTATE run in O(1) time.

chap13Hsiu-Hui Lee19 Example of L EFT -R OTATE (T,x) Rotation makes y ’ s left subtree into x ’ s right subtree.

chap13Hsiu-Hui Lee20 Some Good Java Applets to simulate Red-Black Tree /AVL tree applet.htmhttp://webpages.ull.es/users/jriera/Docencia/AVL /AVL tree applet.htm

Insertion and Deletion

chap13Hsiu-Hui Lee22 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 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)

chap13Hsiu-Hui Lee23 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

chap13Hsiu-Hui Lee24 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

chap13Hsiu-Hui Lee25 The operation of RB-I NSERT -F IXUP 紅叔 黑叔右子

chap13Hsiu-Hui Lee26 黑叔左子

chap13Hsiu-Hui Lee27 case 1 of the RB-I NSERT (z’s uncle y is red) 紅叔右子 紅叔左子

chap13Hsiu-Hui Lee28 case 2/3 of the RB-INSERT (z’s uncle y is black and z is a right/left child) 黑叔右子黑叔左子

chap13Hsiu-Hui Lee29 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.

chap13Hsiu-Hui Lee 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]

chap13Hsiu-Hui Lee31 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

chap13Hsiu-Hui Lee32 RB-D ELETE -F IXUP (T,x) 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

chap13Hsiu-Hui Lee33 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 RIGHT-ROTATE(T,w) Case3 16w ← right[p[x]] Case3 17 color[w] ← color[p[x]] Case4 18 color[p[x]] ← BLACK Case4 19 color[right[w]] ← BLACK Case4 20 LEFT-ROTATE(T,p[x]) Case4 21 x ← root[T] Case4 22 else (same as then clause with “ right ” and “ left ” exchanged) 23 color[x] ← BLACK

chap13Hsiu-Hui Lee34 the case in the while loop of RB-D ELETE -F IXUP

chap13Hsiu-Hui Lee35

chap13Hsiu-Hui Lee36 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)