Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Red-Black tree Recall binary search tree –Key values in the left subtree = the node value Operations:
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 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
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.
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 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.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
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: 
Data Structures and Algorithms TREE-TRAVERSAL. Searching - Re-visited Binary tree O(log n) if it stays balanced Simple binary tree good for static collections.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
Red Black Tree Essentials Notes from “Introduction to Algorithms”, Cormen et al.
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.
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.
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)
CSE Advanced Algorithms Instructor : Gautam Das Submitted by Raja Rajeshwari Anugula & Srujana Tiruveedhi.
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.
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)
Lecture 9 Algorithm Analysis
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Red Black Tree Essentials
Lecture 9 Algorithm Analysis
Ch. 12: Binary Search Trees
Lecture 9 Algorithm Analysis
Red-Black Trees.
CS 583 Analysis of Algorithms
Red Black Tree Essentials
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
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:

Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro. To Algorithms” book website (copyright McGraw Hill) adapted and supplemented

CLRS “Intro. To Algorithms” Ch. 13: Red-Black Trees

Quick review of binary search trees (Ch. 12). A red-black tree is a binary search tree with one extra bit of storage per node, the color, which can be RED or BLACK. An RB tree, therefore, contains 5 fields per node: color, key and the left, right and parent pointers. NIL’s represent missing child or parent nodes. NIL’s are considered leaves, other key-containing nodes are considered internal. Therefore, every internal node has 2 children. A red-black tree must satisfy the following conditions: 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 its children are black. 5. For each node, all paths from the node to descendant leaves contain the same number of black nodes.

The black-height of a node x, denoted bh(x), is the number of black nodes, not counting x itself, on a path from x down to a leaf. This is a valid defn. because of Property 5. The black-height of an RB tree is the black-height of its root. Draw some binary search trees and then try to color the nodes to make the tree RB. Observe that the RB properties force the tree to be nearly balanced. Sentinel NIL node

Lemma 13.1: An RB tree with n nodes has height at most 2log(n+1), in particular, its height is O(log n). Note: By an RB tree we mean the original tree minus NIL leaves. To calculate the black height of a node, however, we must count the contribution of black NIL ptrs. Proof: We first prove by induction on height the following claim: the subtree rooted at any node x contains at least 2 bh(x) – 1 nodes. To start induction, observe that if ht(x) = 0 then x is a leaf, so bh(x) = 1. And, indeed, the subtree rooted at x contains 2 bh(x) – 1 = 2 1 – 1 = 1 node. For the inductive step, consider a node x with ht(x) > 0. Each child of x has black-height either bh(x) or bh(x) – 1 (why?). Applying the inductive hypothesis, the subtree rooted at each child of x has at least 2 bh(x) – 1 – 1 nodes. Therefore, the subtree rooted at x contains at least (2 bh(x) – 1 – 1) + (2 bh(x) – 1 – 1) + 1 = 2 bh(x) – 1 nodes, proving the claim. Next, let h be the height of the tree. According to Property 4 at least half the nodes on a path from the root to the leaf, not including the root, must be black. Therefore, bh(root)  h/2. Using the claim proved above, the number of nodes of the tree itself is n  2 bh(root) – 1  2 h/2 – 1  h ≤ 2log(n+1)  h = O(log n) Therefore, TREE-SEARCH, TREE-MINIMUM, etc., operations that don’t modify the structure of a binary search tree, take O(log n) time on an RB tree. Exs ,

if left(y)  nil(T) then

Extra lines of code + NIL has been replaced by nil(T) Ordinary binary search tree insert

Case 1: z has red uncle y.

Case 2: z has black uncle y, z is inside child. Case 3: z has black uncle y, z is outside child. Ques: What is the running time of RB-INSERT ? Ques: What is the maximum number of rotations that RB-INSERT can perform? y δ Right rotation at CLeft rotation at A

Ex Try inserting other sequences of keys!

Ordinary binary search tree delete

Ordinary binary search tree delete code

// change: no test if x  nil[T ]! // change!

Problems Ex Ex Ex Ex Ex Ex Read about deletion in RB-trees in Sec Prob Prob Prob. 13-3