Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

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.
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 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
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.
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.
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.
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:
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.
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.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
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.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Binary Search Trees What is a binary search tree?
Lecture 23 Red Black Tree Chapter 10 of textbook
Definitions and Bottom-Up Insertion
Lecture 15 Nov 3, 2013 Height-balanced BST Recall:
AA Trees.
Balanced Search Trees Modified from authors’ slides.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Summary of General Binary search tree
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
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.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
CMSC 341 (Data Structures)
Lecture 9 Algorithm Analysis
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Red Black Tree Essentials
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Lecture 9 Algorithm Analysis
Ch. 12: Binary Search Trees
Lecture 9 Algorithm Analysis
Red-Black Trees.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
CS 583 Analysis of Algorithms
Red Black Tree Essentials
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Red-Black Trees.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Analysis of Algorithms CS 477/677
Red-black tree properties
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Binary Search Trees Comp 122, Spring 2004.
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 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

A red-black tree must satisfy the following conditions: Quick review of binary search trees (Ch. 12). Weakness: they can grow unbalanced under insertions leading to worst-case O(n) time for search/insertion/deletion. 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 (e.g., 0 = red, 1 = 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 internal. Therefore, every internal node has 2 children. A red-black tree must satisfy the following conditions: Every node is either red or black. The root is black. Every leaf (NIL) is black. If a node is red, then its children are black. For each node, all paths from the node to descendant leaves contain the same number of black nodes.

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

Lemma 13.1: An RB tree with n nodes has height at most 2log(n+1), in particular, its height is O(log n) (i.e., it is always balanced!). 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 the height at a node the following claim: the subtree rooted at any node x contains at least 2bh(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 2bh(x) – 1 = 21 – 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 2bh(x) – 1 – 1 nodes. Therefore, the subtree rooted at x contains at least (2bh(x) – 1 – 1) + (2bh(x) – 1 – 1) + 1 = 2bh(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  2bh(root) – 1  2h/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. 13.1-1, 13.1-2

if left(y)  nil(T) then

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

Case 1: z has red uncle y.

Case 2: z has black uncle y, z is inside child. Left rotation at A Right rotation at C y δ y δ 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?

Ex. 13.3-2 Try inserting other sequences of keys!

Ordinary binary search tree delete

Ordinary binary search tree delete code

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

Problems Ex. 13.1-5 Ex. 13.1-6 Ex. 13.2-1 Ex. 13.2-4 Ex. 13.3-1 Read about deletion in RB-trees in Sec. 13.4 13.4-3 Prob. 13-1 Prob. 13-2 Prob. 13-3