Introduction to Algorithms Jiafen Liu Sept. 2013.

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.
David Luebke 1 8/25/2014 CS 332: 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.
Balanced Binary Search Trees
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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 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.
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.
© 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.
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.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
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.
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: 
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
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.
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.
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)
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.
Red-Black Trees Bottom-Up Deletion. Recall “ordinary” BST Delete 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has just.
David Luebke 1 3/20/2016 CS 332: Algorithms Skip Lists.
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.
BST Trees Saurav Karmakar
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Summary of General Binary search tree
Design and Analysis of Algorithms
Red-Black Trees Motivations
CS200: Algorithms Analysis
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
CMSC 341 (Data Structures)
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Algorithms and Data Structures Lecture VIII
CS 583 Analysis of Algorithms
Analysis of Algorithms CS 477/677
Red-Black Trees CS302 Data Structures
Presentation transcript:

Introduction to Algorithms Jiafen Liu Sept. 2013

Today’s Tasks Balanced Search Trees Red-black trees –Height of a red-black tree –Rotations –Insertion

Balanced Search Trees Balanced search tree: A search-tree data structure for which a height of O(lgn) is guaranteed when implementing a dynamic set of n items. Examples: –A–AVL Tree –2–2-3-4 Tree –B–B Tree –R–Red-black Tree

Red-black trees This data structure requires an extra 1-bit color field in each node. Red-black properties: –Every node is either red or black. –The root and leaves (NIL’s) are black. –If a node is red, then its parent is black. –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 Convention: black-height of x does not include x itself.

Example of a red-black tree It could have a bunch of blacks, but it will never repeat two reds in a row.

Goals of red-black trees there are a couple of goals that we are trying to achieve. –These properties should force the tree to have logarithmic height, O(lgn) height. –The other desire we have from these properties is that they are easy to maintain. We can create a tree in the beginning that has this property. A perfectly balanced binary tree with all nodes black will satisfy those properties. The tricky part is to maintain them when we make changes to the tree.

Height of a red black tree Let's look at the height of a red black tree. And we will start to see where these properties come from. Theorem. A red-black tree with n keys has height h ≤ 2 lg(n + 1). –Can be proved by induction, as in the book P164. –Another informal way: intuition.

Intuition Merge red nodes into their black parents. This process produces another type of balanced search tree: tree. –Any guesses why it's called a tree? –Another nice property: All of the leaves have the same depth. Why? By Property 4

height of merged tree h' Now we will prove the height of merged tree h'. The first question is how many leaves are there in a red-black tree? –#(internal nodes) +1 –It can also be proved by induction. Try it.

Height of Red-Black Tree The number of leaves in each tree is n h' ≤ n + 1 ≤ 4 h' h' ≤ lg(n + 1) How to connect h' and h? We also have h' ≥ 1/2 h h ≤ 2 lg(n + 1).

Query operations Corollary. The queries SEARCH, MIN, MAX, SUCCESSOR, and PREDECESSOR all run in O(lgn) time on a red-black tree with n nodes.

Modifying operations The operations INSERT and DELETE cause modifications to the red-black tree. How to INSERT and DELETE a node in Red Black Tree? –The first thing we do is just use the BST operation. –They will preserve the binary search tree property, but don't necessarily preserve balance.

Modifying operations The operations INSERT and DELETE cause modifications to the red-black tree. How to INSERT and DELETE a node in Red Black Tree? –The second thing is to set color of new internal node, to preserve property 1. –We can color it red, and property 3 does not hold. –The good news is that property 4 is still true.

How to fix property 3? We are going to move the violation of property 3 up the tree. IDEA: Only red-black property 3 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. we have to restructure the links of the tree via “rotation”.

Rotations Rotations maintain the inorder ordering of keys. a ∈ α, b ∈ β, c ∈ γ ⇒ a ≤ A ≤ b ≤ B ≤ c. A rotation can be performed in O(1) time. –Because we only change a constant number of pointers.

Implement of Rotation P166

Example Insert 15

Example Insert 15 Color it red

Example Insert 15 Color it red Handle Property 3 by recoloring

Example Insert 15 Color it red Handle Property 3 by recoloring

Example Insert 15 Color it red Handle Property 3 by recoloring Failed !

RIGHT-ROTATE RIGHT-ROTATE(18) It turns out to be?

RIGHT-ROTATE We are still in trouble between 10 and 18. But made this straighter. It doesn't look more balanced than before. We can not resolve by recoloring. Then?

LEFT-ROTATE LEFT-ROTATE(7) It turns out to be?

LEFT-ROTATE LEFT-ROTATE(7) and recoloring at the same time It satisfy all the 4 properties

Pseudocode of insertion RB-INSERT(T, x) TREE-INSERT(T, x) color[x] ← RED // only RB property 3 can be violated while x ≠ root[T] and color[x] = RED do if p[x] = left[ p[ p[x] ] then y ← right[ p[ p[x] ] // y = aunt or uncle of x if color[y] = RED then 〈 Case 1 〉

Case 1 Let denote a subtree with a black root. All have the same black-height. Push C’s black onto A and D, and recurse. Caution: We don't know whether B was the right child or the left child. In fact, it doesn't matter. How?

Pseudocode of insertion RB-INSERT(T, x) TREE-INSERT(T, x) color[x] ← RED // only RB property 3 can be violated while x ≠ root[T] and color[x] = RED do if p[x] = left[ p[ p[x] ] then y ← right[ p[ p[x] ] // y = aunt/uncle of x if color[y] = RED then 〈 Case 1 〉 else if x = right[p[x]] //y is red, x,y have a zigzag then 〈 Case 2 〉 // This falls into case 3

Case 2 Now we have a zigzig. we may want a straight path between x. How?

Pseudocode of insertion RB-INSERT(T, x) TREE-INSERT(T, x) color[x] ← RED // only RB property 3 can be violated while x ≠ root[T] and color[x] = RED do if p[x] = left[ p[ p[x] ] then y ← right[ p[ p[x] ] // y = aunt/uncle of x if color[y] = RED then 〈 Case 1 〉 else if x = right[p[x]] then 〈 Case 2 〉 // This falls into case 3 〈 Case 3 〉 //straight path

Case 3 Done! No more violations of RB property 3 are possible. Property 4 is also preserved. How?

Pseudocode of insertion RB-INSERT(T, x) TREE-INSERT(T, x) color[x] ← RED // only RB property 3 can be violated while x ≠ root[T] and color[x] = RED do if p[x] = left[ p[ p[x] ] then y ← right[ p[ p[x] ] // y = aunt/uncle of x if color[y] = RED then 〈 Case 1 〉 else if x = right[p[x]] then 〈 Case 2 〉 // This falls into case 3 〈 Case 3 〉 else 〈 “then” clause with “left” and “right” swapped 〉 color[root[T]] ← BLACK

Analysis Go up the tree performing Case 1, which only recolors nodes. If Case 2 or Case 3 occurs, perform 1 or 2 rotations, and terminate. Running time of insertion? – O(lgn) with O(1) rotations. RB-DELETE — same asymptotic running time and number of rotations as RB- INSERT (see textbook).