CS350: Data Structures Red-Black Trees

Similar documents
CS350: Data Structures AA Trees

Algorithms. Deleting from Red-Black Trees B-Trees

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

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

CS350: Data Structures B-Trees

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Chapter 12 Advanced Data Structures

CS350: Data Structures AVL Trees

Red-black trees (19.5), B-trees (19.8), trees

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

CS350: Data Structures Binary Search Trees

CS 350 : Data Structures B-Trees

CS 206 Introduction to Computer Science II

Search Trees. COMPSCI 355 Fall 2016

Self-Balancing Search Trees. Chapter 11

CS 350 : Data Structures Binary Search Trees

12 July, Red-Black Trees. Red-Black Trees

A red-black tree is a balanced binary search tree with the following properties:

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

Graduate Algorithms CS F-07 Red/Black Trees

Properties of red-black trees

Binary Search Trees. Analysis of Algorithms

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

Section 4 SOLUTION: AVL Trees & B-Trees

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

Data Structures in Java

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

CS 350 : Data Structures Skip Lists

CS350: Data Structures Heaps and Priority Queues

13.4 Deletion in red-black trees

Algorithms. Red-Black Trees

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

CS 331 DATA STRUCTURES & ALGORITHMS BINARY TREES, THE SEARCH TREE ADT BINARY SEARCH TREES, RED BLACK TREES, THE TREE TRAVERSALS, B TREES WEEK - 7

Motivation for B-Trees

COMP171. AVL-Trees (Part 1)

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

TREES. Trees - Introduction

CS 758/858: Algorithms

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

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

Balanced search trees. DS 2017/2018

Search Trees. Undirected graph Directed graph Tree Binary search tree

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

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

Lecture 6: Analysis of Algorithms (CS )

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

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

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

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

Best-Case upper limit on the time for insert/delete/find of an element for a BST withnelements?

Data Structure - Advanced Topics in Tree -

Algorithms. AVL Tree

Balanced search trees

Define the red- black tree properties Describe and implement rotations Implement red- black tree insertion

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

AVL Trees (10.2) AVL Trees

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

Ch04 Balanced Search Trees

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

Transform & Conquer. Presorting

Trees. Truong Tuan Anh CSE-HCMUT

Multi-way Search Trees

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

CISC 235: Topic 4. Balanced Binary Search Trees

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

CSI33 Data Structures

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

CS24 Week 8 Lecture 1

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

Trees. Eric McCreath

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

Lecture 23: Binary Search Trees

AVL trees and rotations

Week 3 Web site:

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

CS102 Binary Search Trees

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

Lecture: Analysis of Algorithms (CS )

Binary search trees (chapters )

CMPS 2200 Fall 2015 Red-black trees Carola Wenk

B-Trees. Disk Storage. What is a multiway tree? What is a B-tree? Why B-trees? Insertion in a B-tree. Deletion in a B-tree

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

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

Augmenting Data Structures

Search Trees. The term refers to a family of implementations, that may have different properties. We will discuss:

Splay Trees. (Splay Trees) Data Structures and Programming Spring / 27

CS350: Data Structures Tree Traversal

CS127: B-Trees. B-Trees

Programming II (CS300)

CSCI Trees. Mark Redekopp David Kempe

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

CMPT 225. Red black trees

10.2 AVL Trees. Definition of an AVL Tree. 438 Chapter 10. Search Trees

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Module 4: Dictionaries and Balanced Search Trees

Analysis of Algorithms

Inf 2B: AVL Trees. Lecture 5 of ADS thread. Kyriakos Kalorkoti. School of Informatics University of Edinburgh

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

Transcription:

Red-Black Trees James Moscola Department of Engineering & Computer Science York College of Pennsylvania James Moscola

Red-Black Tree An alternative to AVL trees Insertion can be done in a bottom-up or a top-down fashion - An AVL tree uses a pass down the tree for an insertion and a second pass back up the tree to update node heights and potentially rebalance the tree - A top-down insertion into a red-black tree requires only a single pass down the tree We ll focus on bottom-up insertion 2

Red-Black Tree (Cont.) A red-black tree is a binary search tree that has the following properties: (1) Every node is colored either red or black (2) The root node is black (3) If a node is red, its children must be black (4) Every path from a node to a null node must contain the same number of black nodes These properties must be maintained after each insertion or deletion operation A null node is considered black 3

Example Red-Black Tree 1 70 8 0 6 40 (1) Every node is colored either red or black (2) The root node is black (3) If a node is red, its children must be black (4) Every path from a node to a null link must contain the same number of black nodes 4

Red-Black Tree Insertion When inserting a new leaf node, what color should it be? insert() 1 70 8 0 6 40 (1) Every node is colored either red or black (2) The root node is black (3) If a node is red, its children must be black (4) Every path from a node to a null link must contain the same number of black nodes

Red-Black Tree Insertion Inserting as a black node violates property #4 1 70 8 0 6 40 (1) Every node is colored either red or black (2) The root node is black (3) If a node is red, its children must be black (4) Every path from a node to a null link must contain the same number of black nodes 6

Red-Black Tree Insertion Inserting as a red node satisfies all four properties (in this case) 1 70 8 0 6 40 (1) Every node is colored either red or black (2) The root node is black (3) If a node is red, its children must be black (4) Every path from a node to a null link must contain the same number of black nodes 7

Red-Black Tree Insertion Inserting as a red may not always satisfy the four properties insert(99) 1 70 8 0 6 40 99 (1) Every node is colored either red or black (2) The root node is black (3) If a node is red, its children must be black (4) Every path from a node to a null link must contain the same number of black nodes 8

Red-Black Tree Insertion Easier to fix a violation of property #3 than it is to fix a violation of property #4 - So, insert all nodes as red nodes Must repair violations of property #3 and any new violations that occur as a result of the tree modification - Operations for repairing the tree include: Single and Double Rotations (similar to AVL trees) Color changes 9

Red-Black Tree Insertion (Bottom-Up) Insert nodes into a red-black tree using the standard binary search tree insertion - Make the newly inserted node red - If the parent of the newly inserted node is black, then no violations have occurred and the insertion is complete - If the parent of the newly inserted node is red, then property #3 has been violated and must be fixed through rotations and recoloring Four different cases must be considered

Red-Black Tree Insertion -- Violations The four cases to consider when property #3 is violated (i.e. when a red node is inserted as the child of another red node) (1) Parent s sibling is red and new node is inserted as an outside grandchild (2) Parent s sibling is red and new node is inserted as an inside grandchild (3) Parent s sibling is black and new node is inserted as an outside grandchild (4) Parent s sibling is black and new node is inserted as an inside grandchild There is a different approach to fix each of these cases 11

Insert Violation -- Case #1 (1) Parent s sibling is red and new node is inserted as an outside grandchild G P S X C D E A B 12

Fixing the Insertion Violation -- Case #1 No rotation necessary Make Parent and Sibling black, Grandparent becomes red G G P S P S X C D E X C D E A B A B Since G becomes red, more work might be needed further up the tree 13

Insert Violation -- Case #2 (2) Parent s sibling is red and new node is inserted as an inside grandchild G P S A X D E B C 14

Fixing the Insertion Violation -- Case #2 No rotation necessary Make Parent and Sibling black, Grandparent becomes red G G P S P S A X D E A X D E B C Since G becomes red, more work might be needed further up the tree B C 1

Insert Violation -- Case #3 (3) Parent s sibling is black and new node is inserted as an outside grandchild G P S X C D E A B 16

Fixing the Insertion Violation -- Case #3 Perform a single rotation P becomes new root of subtree Nodes P and G get a color change G P P S X G X C D E A B C S A B D E 17

Insert Violation -- Case #4 (4) Parent s sibling is black and new node is inserted as an inside grandchild G P S A X D E B C 18

Fixing the Insertion Violation -- Case #4 Perform a double rotation X becomes new root of subtree Nodes X and G get a color change G X P S P G A X D E A B C S B C D E 19

Red-Black Tree Insertion Example insert(99) 1 70 8 0 6 40 99 (1) Every node is colored either red or black (2) The root node is black (3) If a node is red, its children must be black (4) Every path from a node to a null link must contain the same number of black nodes

Red-Black Tree Insertion Example insert(99) 1 70 8 0 6 40 99 Case #1: Parent s sibling is red and new node is inserted as an outside grandchild 21

Red-Black Tree Insertion Example insert(99) 1 70 Push black down from the grandparent 8 0 6 40 99 Case #1: Parent s sibling is red and new node is inserted as an outside grandchild 22

Red-Black Tree Insertion Example insert(99) 1 70 Push black down from the grandparent 8 0 6 40 99 23

Red-Black Tree Insertion Example insert(99) 1 70 8 0 6 40 99 Balanced

Red-Black Tree Insertion Example insert(23) 1 70 8 0 6 23 40 99 (1) Every node is colored either red or black (2) The root node is black (3) If a node is red, its children must be black (4) Every path from a node to a null link must contain the same number of black nodes 2

Red-Black Tree Insertion Example insert(23) 1 70 8 Null 0 6 23 40 99 Case #4: Parent s sibling is black and new node is inserted as an inside grandchild 26

Red-Black Tree Insertion Example insert(23) 1 70 8 Null 0 6 23 Perform a double rotation: First, rotate between nodes 23 &, then, rotate between nodes 23 & and change their colors 40 99 27

Red-Black Tree Insertion Example insert(23) 1 70 8 Null 23 0 6 40 99 Perform a double rotation: First, rotate between nodes 23 &, then, rotate between nodes 23 & and change their colors HEY, LOOK who showed up after the first rotation!! It s case #3: Parent s sibling is black and new node is inserted as an outside grandchild 28

Red-Black Tree Insertion Example insert(23) 1 70 23 8 0 6 40 99 Perform a double rotation: First, rotate between nodes 23 &, then, rotate between nodes 23 & and change their colors 29

Red-Black Tree Insertion Example insert(23) 1 70 23 8 0 6 40 99 Finally, recolor nodes 23 and

Red-Black Tree Insertion Example insert(23) 1 70 23 8 0 6 40 99 Balanced 31

Red-Black Tree Deletion Start with standard BST deletion - In BST deletion, when deleting a node with two children, the node was not actually deleted -- its contents were replaced (1) Contents replaced with contents from successor or predecessor (2) Then the successor/predecessor node was deleted - Successor/predecessor can have 0 or 1 child (if the node had two children, then one of its children would be the successor/ predecessor) - Replacing the contents of a node do not affect the coloring of the node, therefore the properties of the red-black tree are not altered - Removal of the node with 0 or 1 child needs consideration as it can potentially cause violations in the red-black tree 32

Red-Black Tree Deletion If the node removed is red, then there are no problems and the 4 properties of the red-black tree will still be true -- there is nothing to do in this case If the node removed is black, then there is a new violation in the red-black tree -- violation of property #4 (4) Every path from a node to a null node must contain the same number of black nodes - It is necessary to work back up the tree and account for the missing black node 33