CMPS 2200 Fall 2017 Red-black trees Carola Wenk

Similar documents
CMPS 2200 Fall 2015 Red-black trees Carola Wenk

CS 3343 Fall 2007 Red-black trees Carola Wenk

Data Structures and Algorithms CMPSC 465

Theory & Algorithms 15/01/04. Red-Black Trees. Nicolas Wack, Sylvain Le Groux

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Red-Black Trees. Every simple path from a node to one of its descendant leaf nodes contains the same number of BLACK nodes.

Sorting. CMPS 2200 Fall Carola Wenk Slides courtesy of Charles Leiserson with small changes by Carola Wenk

Lecture 6: Analysis of Algorithms (CS )

CMPS 2200 Fall 2017 B-trees Carola Wenk

CISC 235: Topic 4. Balanced Binary Search Trees

Algorithms. Deleting from Red-Black Trees B-Trees

Search Trees. COMPSCI 355 Fall 2016

CS Fall 2010 B-trees Carola Wenk

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Lecture: Analysis of Algorithms (CS )

CS350: Data Structures Red-Black Trees

Search Trees. Undirected graph Directed graph Tree Binary search tree

(2,4) Trees. 2/22/2006 (2,4) Trees 1

How fast can we sort? Sorting. Decision-tree example. Decision-tree example. CS 3343 Fall by Charles E. Leiserson; small changes by Carola

Self-Balancing Search Trees. Chapter 11

13.4 Deletion in red-black trees

Search Trees - 2. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore. M.S Ramaiah School of Advanced Studies - Bangalore

Properties of red-black trees

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Orthogonal range searching. Range Trees. Orthogonal range searching. 1D range searching. CS Spring 2009

Balanced search trees

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

Balanced Binary Search Trees

COMP Analysis of Algorithms & Data Structures

Multiway Search Trees. Multiway-Search Trees (cont d)

COMP Analysis of Algorithms & Data Structures

Search Trees - 1 Venkatanatha Sarma Y

CIS265/ Trees Red-Black Trees. Some of the following material is from:

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Augmenting Data Structures

13.4 Deletion in red-black trees

Balanced search trees. DS 2017/2018

TREES. Trees - Introduction

We assume uniform hashing (UH):

Algorithms. Red-Black Trees

Programming II (CS300)

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

Algorithms. AVL Tree

COMP Analysis of Algorithms & Data Structures

Data Structure: Search Trees 2. Instructor: Prof. Young-guk Ha Dept. of Computer Science & Engineering

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

CS102 Binary Search Trees

Binary Trees. Recursive definition. Is this a binary tree?

B Tree. Also, every non leaf node must have at least two successors and all leaf nodes must be at the same level.

Trees. (Trees) Data Structures and Programming Spring / 28

Analysis of Algorithms

CS 350 : Data Structures B-Trees

Orthogonal line segment intersection. Computational Geometry [csci 3250] Laura Toma Bowdoin College

COMP251: Red-black trees

In a non-ideal situation, we can allow the binary tree to grow to twice the height of the perfect tree (2 lg n) and periodically balance it

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

catch(...){ printf( "Assignment::SolveProblem() AAAA!"); }

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

CMPT 225. Red black trees

Advanced Set Representation Methods

CS350: Data Structures B-Trees

Computational Geometry

OPPA European Social Fund Prague & EU: We invest in your future.

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

Line segment intersection (I): Orthogonal line segment intersection. Computational Geometry [csci 3250] Laura Toma Bowdoin College

CS24 Week 8 Lecture 1

Range Searching and Windowing

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

(2,4) Trees Goodrich, Tamassia. (2,4) Trees 1

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Balanced Trees Part Two

COT 5407: Introduction to Algorithms. Giri Narasimhan. ECS 254A; Phone: x3748

CS 380 ALGORITHM DESIGN AND ANALYSIS

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

Search Trees: BSTs and B-Trees

B-Trees and External Memory

CSE 530A. B+ Trees. Washington University Fall 2013

Animations. 01/29/04 Lecture

B-Trees and External Memory

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

CSC 172 Data Structures and Algorithms. Fall 2017 TuTh 3:25 pm 4:40 pm Aug 30- Dec 22 Hoyt Auditorium

Red-Black Trees. 2/24/2006 Red-Black Trees 1

Balanced Trees Part One

Trees. Eric McCreath

A set of nodes (or vertices) with a single starting point

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

8.1. Optimal Binary Search Trees:

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Data Structures in Java

Linked Structures Songs, Games, Movies Part III. Fall 2013 Carola Wenk

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

Module 4: Dictionaries and Balanced Search Trees

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Binary Trees, Binary Search Trees

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

Transcription:

CMPS 2200 Fall 2017 Red-black trees Carola Wenk Slides courtesy of Charles Leiserson with changes by Carola Wenk 9/13/17 CMPS 2200 Intro. to Algorithms 1

Dynamic Set A dynamic set, or dictionary, is a data structure which supports operations Insert Delete Find Using balanced binary search trees we can implement a dictionary data structure such that each operation takes O(log n) time. 3 7 10 8 15 18 12 17 22 9/13/17 CMPS 2200 Intro. to Algorithms 2

Search Trees A binary search tree is a binary tree. Each node stores a key. The tree fulfills the binary search tree property: For every node x holds: y x, for all y in the subtree left of x x < y, for all y in the subtree right of x x 3 7 18 x x 10 22 8 15 12 17 9/13/17 CMPS 2200 Intro. to Algorithms 3

Search Trees 7 3 18 Different variants of search trees: 1 12 10 22 Balanced search trees (guarantee height of O(log n) for n elements) 8 15 12 17 k-ary search trees (such as B-trees, 2-3-4-trees) 6 2 7 8 10 25 12 15 21 30 45 11 14 20 23 24 27 40 50 Search trees that store keys only in leaves, and store copies of 1 keys as split-values in internal nodes 6 12 17 26 41 43 59 6 8 12 14 26 35 41 42 9/13/17 CMPS 2200 Intro. to Algorithms 4 8 17 1 14 35 43 42 59 61

Balanced search trees Balanced search tree: A search-tree data structure for which a height of O(log n) is guaranteed when implementing a dynamic set of n items. Examples: AVL trees 2-3 trees 2-3-4 trees B-trees Red-black trees 9/13/17 CMPS 2200 Intro. to Algorithms 5

Red-black trees This data structure requires an extra onebit color field in each node. Red-black properties: 1. Every node is either red or black. 2. The root is black. 3. The leaves (null s) are black. 4. If a node is red, then both its children are black. 5. All simple paths from any node x, excluding x, to a descendant leaf have the same number of black nodes = black-height(x). 9/13/17 CMPS 2200 Intro. to Algorithms 6

Example of a red-black tree 7 3 18 null null 10 22 h = 4 8 11 null 26 null null null null null null 1. Every node is either red or black. 9/13/17 CMPS 2200 Intro. to Algorithms 7

Example of a red-black tree 7 3 18 null null 10 22 h = 4 8 11 null 26 null null null null null null 2., 3. The root and leaves (null s) are black. 9/13/17 CMPS 2200 Intro. to Algorithms 8

Example of a red-black tree 7 3 18 null null 10 22 h = 4 8 11 null 26 null null null null null null 4. If a node is red, then both its children are black. 9/13/17 CMPS 2200 Intro. to Algorithms 9

Example of a red-black tree 7 bh = 2 3 18 bh = 2 null null bh = 1 10 22 h = 4 bh = 1 8 11 null 26 bh = 0 null null null null null null 5. All simple paths from any node x, excluding x, to a descendant leaf have the same number of black nodes = black-height(x). 9/13/17 CMPS 2200 Intro. to Algorithms 10

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes into their black parents. 9/13/17 CMPS 2200 Intro. to Algorithms 11

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes into their black parents. 9/13/17 CMPS 2200 Intro. to Algorithms 12

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes into their black parents. 9/13/17 CMPS 2200 Intro. to Algorithms 13

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes into their black parents. 9/13/17 CMPS 2200 Intro. to Algorithms 14

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes into their black parents. 9/13/17 CMPS 2200 Intro. to Algorithms 15

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 log(n + 1). Proof. INTUITION: T Merge red nodes h into their black parents. This process produces a tree in which each node has 2, 3, or 4 children. The 2-3-4 tree has uniform depth h of leaves. 9/13/17 CMPS 2200 Intro. to Algorithms 16

Proof (continued) We have h h/2, since at most half the vertices on any path are red. # leaves in T = # leaves in T # leaves in T = n+1 (fact about binary trees with exactly 2 children per internal node) # leaves in T 2 h (fact about binary trees; T can only have more) n + 1 2 h' log(n + 1) h' h/2 h 2 log(n + 1). 9/13/17 CMPS 2200 Intro. to Algorithms 17 T T h h

Query operations Corollary. The queries SEARCH, MIN, MAX, SUCCESSOR, and PREDECESSOR all run in O(log n) time on a red-black tree with n nodes. 3 7 10 18 22 8 11 26 9/13/17 CMPS 2200 Intro. to Algorithms 18

Modifying operations The operations INSERT and DELETE cause modifications to the red-black tree: 1. the operation itself, 2. color changes, 3. restructuring the links of the tree via rotations. 9/13/17 CMPS 2200 Intro. to Algorithms 19

Rotations B RIGHT-ROTATE(B) A A LEFT-ROTATE(A) B Rotations maintain the inorder ordering of keys: a, b, c a A b B c. Rotations maintain the binary search tree property Can be applied to any BST, not just red-black trees A rotation can be performed in O(1) time. 9/13/17 CMPS 2200 Intro. to Algorithms 20

B RIGHT-ROTATE(B) A Rotation Example A LEFT-ROTATE(A) B 31 RIGHT-ROTATE(20) 31 9 41 9 41 6 20 39 42 6 16 39 42 2 8 16 22 2 8 15 20 11 15 17 In-order traversal: 2 6 8 9 11 15 16 17 20 22 31 39 41 42 In-order(v){ if v==null return; In-order(v.left); print(v.key+ ); In-order(v.right); } 11 17 22 In-order traversal: 2 6 8 9 11 15 16 17 20 22 31 39 41 42 Maintains sorted order of keys, and can reduce height 9/13/17 CMPS 2200 Intro. to Algorithms 21

Red-black trees This data structure requires an extra onebit color field in each node. Red-black properties: 1. Every node is either red or black. 2. The root is black. 3. The leaves (null s) are black. 4. If a node is red, then both its children are black. 5. All simple paths from any node x, excluding x, to a descendant leaf have the same number of black nodes = black-height(x). 9/13/17 CMPS 2200 Intro. to Algorithms 22

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: Insert x =15. 3 7 10 18 22 8 11 26 15 9/13/17 CMPS 2200 Intro. to Algorithms 23

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: 3 Insert x =15. Recolor, moving the 10 violation up the tree. 8 11 7 18 15 22 26 9/13/17 CMPS 2200 Intro. to Algorithms 24

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: 3 Insert x =15. Recolor, moving the 10 violation up the tree. 8 11 7 18 15 22 26 9/13/17 CMPS 2200 Intro. to Algorithms 25

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: 3 Insert x =15. Recolor, moving the violation up the tree. RIGHT-ROTATE(18). 7 10 8 11 18 15 22 26 9/13/17 CMPS 2200 Intro. to Algorithms 26

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: 3 Insert x =15. Recolor, moving the violation up the tree. RIGHT-ROTATE(18). 7 8 10 11 15 18 22 26 9/13/17 CMPS 2200 Intro. to Algorithms 27

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: 3 Insert x =15. Recolor, moving the violation up the tree. RIGHT-ROTATE(18). LEFT-ROTATE(7) 9/13/17 CMPS 2200 Intro. to Algorithms 28 7 8 10 11 15 18 22 26

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: Insert x =15. Recolor, moving the violation up the tree. RIGHT-ROTATE(18). LEFT-ROTATE(7) 8 11 9/13/17 CMPS 2200 Intro. to Algorithms 29 3 7 10 15 18 22 26

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: Insert x =15. 7 Recolor, moving the 3 violation up the tree. RIGHT-ROTATE(18). LEFT-ROTATE(7) and recolor. 8 11 9/13/17 CMPS 2200 Intro. to Algorithms 30 10 15 18 22 26

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 4 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: Insert x =15. 7 Recolor, moving the 3 violation up the tree. RIGHT-ROTATE(18). LEFT-ROTATE(7) and recolor. 8 11 9/13/17 CMPS 2200 Intro. to Algorithms 31 10 15 18 22 26

Pseudocode RB-INSERT(T, x) TREE-INSERT(T, x) color[x] RED only RB property 4 can be violated while x root[t] and color[p[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 Case 2 falls into Case 3 Case 3 else then clause with left and right swapped color[root[t]] BLACK 9/13/17 CMPS 2200 Intro. to Algorithms 32

Graphical notation Let All denote a subtree with a black root. s have the same black-height. 9/13/17 CMPS 2200 Intro. to Algorithms 33

Case 1 A x B C D y Recolor A B C Continue new x D (Or, A s children are swapped.) p[x] = left[p[p[x]] y right[p[p[x]] color[y] = RED Push C s black onto A and D, and recurse, since C s parent may be red. 9/13/17 CMPS 2200 Intro. to Algorithms 34

Case 2 A x B C y LEFT-ROTATE(A) x A B C y p[x] = left[p[p[x]] y right[p[p[x]] color[y] = BLACK x = right[p[x]] Transform to Case 3. 9/13/17 CMPS 2200 Intro. to Algorithms 35

Case 3 x A B C RIGHT-ROTATE(C) y (and recolor) A B C p[x] = left[p[p[x]] y right[p[p[x]] color[y] = BLACK x = left[p[x]] Done! No more violations of RB property 4 are possible. 9/13/17 CMPS 2200 Intro. to Algorithms 36

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: O(log n) with O(1) rotations. RB-DELETE same asymptotic running time and number of rotations as RB-INSERT. 9/13/17 CMPS 2200 Intro. to Algorithms 37

Pseudocode (part II) else then clause with left and right swapped p[x] = right[p[p[x]] then y left[p[p[x]] y = aunt/uncle of x if color[y] = RED then Case 1 else if x = left[p[x]] then Case 2 Case 2 falls into Case 3 Case 3 color[root[t]] BLACK 9/13/17 CMPS 2200 Intro. to Algorithms 38

Case 1 D y C A x B Recolor D y C Continue new x A x B (Or, A s children are swapped.) p[x] = right[p[p[x]] y left[p[p[x]] color[y] = RED Push C s black onto A and D, and recurse, since C s parent may be red. 9/13/17 CMPS 2200 Intro. to Algorithms 39

Case 2 y C x B A RIGHT-ROTATE(A) y C B x A p[x] = right[p[p[x]] y left[p[p[x]] color[y] = BLACK x = left[p[x]] Transform to Case 3. 9/13/17 CMPS 2200 Intro. to Algorithms 40

Case 3 C y B x LEFT-ROTATE(C) (and recolor) C B A A p[x] = right[p[p[x]] y left[p[p[x]] color[y] = BLACK x = right[p[x]] Done! No more violations of RB property 4 are possible. 9/13/17 CMPS 2200 Intro. to Algorithms 41