CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

© 2001 by Charles E. Leiserson Introduction to AlgorithmsDay 18 L10.1 Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 10 Prof. Erik Demaine.
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.
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:
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.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 COSC 2P03 Lecture #5 – Trees Part III, Heaps. 2 Today Take up the quiz Assignment Questions Red-Black Trees Binary Heaps Heap sort D-Heaps, Leftist.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Search.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
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.
Balanced Binary Search Trees
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
© 2014 by Ali Al Najjar Introduction to Algorithms Introduction to Algorithms Red Black Tree Dr. Ali Al Najjar Day 18 L10.1.
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
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.
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: 
1 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
AVL trees1 AVL Trees Height of a node : The height of a leaf is 1. The height of a null pointer is zero. The height of an internal node is the maximum.
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.
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.
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.
Binary Search Trees What is a binary search tree?
Lecture 23 Red Black Tree Chapter 10 of textbook
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Summary of General Binary search tree
Design and Analysis of Algorithms
Slide Sources: CLRS “Intro. To Algorithms” book website
CS200: Algorithms Analysis
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees.
CMSC 341 (Data Structures)
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
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Chapter 12&13: Binary Search Trees (BSTs)
Red-Black Trees CS302 Data Structures
Presentation transcript:

CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)

CS 473Lecture X2 Overview Previous lecture showed that a BST of height h can implement any of the dynamic set operations in O(h) time. Thus, set operations are fast if the height of BST is small. But, if h is large, its performance is no better than linked list.

CS 473Lecture X3 Overview Red-Black trees are one of many search tree schemes that are balanced to guarantee h= O(lgn) First, we will show that h= O(lgn) in RBTs Then, show how to manipulate & maintain RBTs during dynamic set operations.

CS 473Lecture X4 Properties of Red-Black Trees Add color field (1 bit) to each node of a BST. Consider “NIL” child fields as pointer to external nodes (leafs) i.e. Leaf nodes do not have keys Consider normal key-bearing nodes as internal nodes. Hence, each internal node has exactly 2 children. Thus, RBTs are full binart trees (FBTs) FBT: Each node is either a leaf or has degree exactly 2. i.e. There are no degree-1 nodes.

CS 473Lecture X5 Red-Black Properties 1.Every node is either red or black 2.Every leaf (NIL) is black 3.If a node is red, then both of its children are black. i.e. There can’t be 2 consecutive reds on a simple path. 4.Every simpe path from a node to a descendant leaf contains the same number of black nodes. 5.( Additional propery) The root is black. 6.Simplifies explanation of insertion which assumes and guarantees that the root is black.

CS 473Lecture X6 A Sample Red Black Tree bh = 2 h = 4 bh = 2 h = 3 bh = 1 h = 2 bh = 1 h = 1 bh = 0 h = 0 bh = 1 h = 1 bh = 2 h = 4

CS 473Lecture X7 Properties of Red-Black Trees Black-Height : bh (x) # of blacks on the simple paths to all descendant leaves not counting x. By the property 4, the black-height is well defined. Red-Black properties ensure that No simpe path from a node to a descendant leaf is more that twice as long as any other. So, red-black trees are apptoximately balanced.

CS 473Lecture X8 Properties of Red-Black Trees Lemma 1 : A height h node has black height ≥ h/2 –Height is length of the longest path to a leaf. –By property 3, # of reds on this path ≤ h/2 –Hence, # of blacks ≥ h/2 Lemma 2 : The subtree T x rooted at any node x contains at least |T x | ≥ 2 bh(x) – 1 internal nodes.

CS 473Lecture X9 Properties of Red-Black Trees Basis: h(x)=0 x must be a leaf (NIL) bh(x)=0 T x contains 0 internal nodes = 2 bh(x) -1=2-1= 0 Inductive Step: h(x) > 0 x is internal node with 2 children l[x] : left[x] r[x] : right[x] h(x) bh(x) r[x] l [x] T r[x] T l[x] x

CS 473Lecture X10 Depending on l[x] and r[x] being red or black –bh(l[x]) = bh(x) or bh(x) -1, respectively, –bh(r[x]) = bh(x) or bh(x) -1, respectively. Since h(l[x]), h(r[x]) < h(x) we can apply inductive hypothesis. |T x | = |T l | + |T r | +1 ≥ (2 bh(x)-1 – 1) + (2 bh(x)-1 – 1) + 1 = 2 bh(x)-1 – 1 Q.E.D. Properties of Red-Black Trees

CS 473Lecture X11 Theorem : A red-black tree with n internal nodes has height at most 2lg(n+1) Due to lemma 2 |T root | = n ≥ 2 bh(root) – 1 Due to lemma 1 bh(root) ≥ h(root) / 2 = H / 2 n ≥ 2 H/2 -1 (n+1) ≥ 2 H/2 H ≤ 2 lg(n+1) Properties of Red-Black Trees

CS 473Lecture X12 Corollary: Dynamic set operations MIN, MAX, SEARCH, SUCCESSOR, PREDECESSOR take O(lgn) time. Fact: Let l min (x) & l max (x) denote the lengths of the shortest & longest paths from a node x to its leafs, respectively. Then, l max (x) ≤ l min (x) for any node x. Proof: l max (x) = h(x) ≤ 2bh(x) ≤ l min (x) Properties of Red-Black Trees Lemma 1

CS 473Lecture X13 The Problem of Insertion into Red-Black Trees Draw a R-B tree Color choices starting at 12 Forced choices : 12 → R ; 5 and 9 → B

CS 473Lecture X14 The Problem of Insertion into Red-Black Trees Insert 8 (Left child of 9) No problem, Just color it red ? → R

CS 473Lecture X15 The Problem of Insertion into Red-Black Trees Insert 11 (Left child of 12) 11 can’t be red (Violate property 3) 11 can’t be black (Violate property 4) ?

CS 473Lecture X16 The Problem of Insertion into Red-Black Trees Can fix up by recoloring the tree New 11 to Red Change 9 to Red Change 8 and 12 to Black R → B B → R R → B 11 ?→R

CS 473Lecture X17 The Problem of Insertion into Red-Black Trees Insert 10 (Left child of 11) Recoloring is not enough, why? -Because of tree imbalance -Can’t satisfy property 4 (Equal # of blacks on paths) without violating property 3 (No consecutive reds on paths) Must change tree structure -Goal : Restructure in O(lgn) time ?