Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
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.
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)
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 /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.
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.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf 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.
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 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.
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)
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 4: Data Structures.
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.
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)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
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.
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.
David Luebke 1 3/20/2016 CS 332: Algorithms Skip Lists.
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?
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Red-Black Trees.
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 Tree Essentials
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:

Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees

Mudasser Naseer 2 10/20/2015 Red-Black Trees ● Red-black trees: ■ Binary search trees augmented with node color ■ Balanced: height is O(lg n), where n is the number of nodes. ■ Operations will take O(lg n) time in the worst case. ● First: describe the properties of red-black trees ● Then: prove that these guarantee h = O(lg n) ● Finally: describe operations on red-black trees

Mudasser Naseer 3 10/20/2015 Red-Black Properties ● The red-black properties: 1. Every node is either red or black 2. The root is always black. 3. Every leaf (NULL pointer (nil[T])) is black ○ Note: this means every “real” node has 2 children 4.If a node is red, both children are black ○ Note: can’t have 2 consecutive reds on a path 5.Every path from node to descendent leaf contains the same number of black nodes

Mudasser Naseer 4 10/20/2015

Mudasser Naseer 5 10/20/2015 Red-Black Trees ● Put example on board and verify properties: 1.Every node is either red or black 2.The root is always black. 3.Every leaf (NULL pointer) is black 4. If a node is red, both children are black 5. Every path from node to descendent leaf contains the same number of black nodes ● black-height bh(x): # black nodes on path from x to leaf (including nil[T] but not counting x) ■ Label example with h and bh values

Mudasser Naseer 6 10/20/2015

Mudasser Naseer 7 10/20/2015 Height of Red-Black Trees ● What is the minimum black-height of a node with height h? ● A: a height-h node has black-height  h/2 ■ Proof: By property 4, ≤ h/2 nodes on the path from the node to a leaf are red. Hence ≥ h/2 are black. ● Theorem: A red-black tree with n internal nodes has height h  2 lg(n + 1) ● How do you suppose we’ll prove this? ■ {An internal node or inner node is any node of a tree that has child nodes and is thus not a leaf node.}

Mudasser Naseer 8 10/20/2015 RB Trees: Proving Height Bound ● Prove: n-node RB tree has height h  2 lg(n+1) ● Claim: A subtree rooted at a node x contains at least 2 bh(x) - 1 internal nodes ■ Proof by induction on height h ■ Base step: x has height 0 (i.e., NULL leaf node) ○ What is bh(x)?

Mudasser Naseer 9 10/20/2015 RB Trees: Proving Height Bound ● Prove: n-node RB tree has height h  2 lg(n+1) ● Claim: A subtree rooted at a node x contains at least 2 bh(x) - 1 internal nodes ■ Proof by induction on height h ■ Base step: x has height 0 (i.e., NULL leaf node) ○ What is bh(x)? ○ A: 0 ○ So…subtree contains 2 bh(x) - 1 = = 0 internal nodes (TRUE)

Mudasser Naseer 10 10/20/2015 RB Trees: Proving Height Bound ● Inductive proof that subtree at node x contains at least 2 bh(x) - 1 internal nodes ■ Inductive step: x has positive height and 2 children ○ Each child has black-height of bh(x) or bh(x)-1 (Why?) ○ The height of a child = (height of x) - 1 ○ So the subtrees rooted at each child contain at least 2 bh(x) internal nodes ○ Thus subtree at x contains (2 bh(x) ) + (2 bh(x) ) + 1 = 22 bh(x) = 2 bh(x) - 1 nodes

Mudasser Naseer 11 10/20/2015 RB Trees: Proving Height Bound ● Thus at the root of the red-black tree: n  2 bh(root) - 1(Why?) n  2 h/2 - 1(Why?) lg(n+1)  h/2(Why?) h  2 lg(n + 1)(Why?) Thus h = O(lg n)

Mudasser Naseer 12 10/20/2015 RB Trees: Worst-Case Time ● So we’ve proved that a red-black tree has O(lg n) height ● Corollary: These operations take O(lg n) time: ■ Minimum(), Maximum() ■ Successor(), Predecessor() ■ Search() ● Insert() and Delete(): ■ Will also take O(lg n) time ■ But will need special care since they modify tree

Mudasser Naseer 13 10/20/2015 Red-Black Trees: An Example ● Color this tree: Red-black properties: 1.Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Mudasser Naseer 14 10/20/2015 ● Insert 8 ■ Where does it go? Red-Black Trees: The Problem With Insertion Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Mudasser Naseer 15 10/20/2015 ● Insert 8 ■ Where does it go? ■ What color should it be? Red-Black Trees: The Problem With Insertion Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Mudasser Naseer 16 10/20/2015 ● Insert 8 ■ Where does it go? ■ What color should it be? Red-Black Trees: The Problem With Insertion Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Mudasser Naseer 17 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? 1.Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Mudasser Naseer 18 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? ■ What color? 1.Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Mudasser Naseer 19 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? ■ What color? ○ Can’t be red! (#3) 1.Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Mudasser Naseer 20 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? ■ What color? ○ Can’t be red! (#3) ○ Can’t be black! (#4) 1.Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Mudasser Naseer 21 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 11 ■ Where does it go? ■ What color? ○ Solution: recolor the tree 1.Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Mudasser Naseer 22 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 10 ■ Where does it go? 1.Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Mudasser Naseer 23 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 10 ■ Where does it go? ■ What color? 1.Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black

Mudasser Naseer 24 10/20/2015 Red-Black Trees: The Problem With Insertion ● Insert 10 ■ Where does it go? ■ What color? ○ A: no color! Tree is too imbalanced ○ Must change tree structure to allow recoloring ■ Goal: restructure tree in O(lg n) time

Mudasser Naseer 25 10/20/2015 RB Trees: Rotation ● Our basic operation for changing tree structure is called rotation: ● Does rotation preserve inorder key ordering? ● What would the code for rightRotate() actually do? y x C AB x A y BC rightRotate(y) leftRotate(x)

Mudasser Naseer 26 10/20/2015 rightRotate(y) RB Trees: Rotation ● Answer: A lot of pointer manipulation ■ x keeps its left child ■ y keeps its right child ■ x’s right child becomes y’s left child ■ x’s and y’s parents change ● What is the running time? y x C AB x A y BC

Mudasser Naseer 27 10/20/2015 Rotation Example ● Rotate left about 9:

Mudasser Naseer 28 10/20/2015 Rotation Example ● Rotate left about 9:

Mudasser Naseer 29 10/20/2015 Rotation Example ● After Colouring 9 118

Rotation LEFT-ROTATE(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 Mudasser Naseer 30 10/20/2015

Rotation - Example Mudasser Naseer 31 10/20/2015

● The longest possible path from the root to a leaf is no more than twice as long as the shortest possible path. The result is that the tree is roughly balanced. ● The shortest possible path has all black nodes, and the longest possible path alternates between red and black nodes. ● All maximal paths have the same number of black nodes, by property 5, this shows that no path is more than twice as long as any other path. Mudasser Naseer 32 10/20/2015

Time Taken ● Read-only operations: binary search trees. ● Insertion or removal may violate the properties of a red-black tree. ● Restoring the red-black properties requires a small number (O(lg n)) of color changes and no more than three tree rotations (two for insertion). Although insert and delete operations are complicated, their times remain O(lg n). Mudasser Naseer 33 10/20/2015

Advantages ● Red-black trees offer the best possible worst- case insertion time, deletion time, and search time. Mudasser Naseer 34 10/20/2015

Applications ● Time-sensitive applications such as real-time applications. ● Data structures used in computational geometry. ● Valuable in functional programming where they are one of the most common persistent data structures, used to construct associative arrays. Mudasser Naseer 35 10/20/2015