Algorithms. Deleting from Red-Black Trees B-Trees

Similar documents
CS350: Data Structures Red-Black Trees

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

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

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

Binary Trees, Binary Search Trees

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

Multiway searching. In the worst case of searching a complete binary search tree, we can make log(n) page faults Everyone knows what a page fault is?

Algorithms. AVL Tree

CS127: B-Trees. B-Trees

Self-Balancing Search Trees. Chapter 11

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

TREES. Trees - Introduction

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

Chapter 12 Advanced Data Structures

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

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

Binary Search Trees. Analysis of Algorithms

Lecture 6: Analysis of Algorithms (CS )

Search Trees. COMPSCI 355 Fall 2016

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

Properties of red-black trees

Binary Trees

Programming II (CS300)

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

CS 171: Introduction to Computer Science II. Binary Search Trees

Trees. Truong Tuan Anh CSE-HCMUT

CMPT 225. Red black trees

Search Trees - 1 Venkatanatha Sarma Y

Chapter 20: Binary Trees

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

Advanced Set Representation Methods

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

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

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

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

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

13.4 Deletion in red-black trees

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

13.4 Deletion in red-black trees

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

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Lecture: Analysis of Algorithms (CS )

Extra: B+ Trees. Motivations. Differences between BST and B+ 10/27/2017. CS1: Java Programming Colorado State University

BST Implementation. Data Structures. Lecture 4 Binary search trees (BST) Dr. Mahmoud Attia Sakr University of Ain Shams

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

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

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

Introduction to Indexing 2. Acknowledgements: Eamonn Keogh and Chotirat Ann Ratanamahatana

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

Red-Black trees are usually described as obeying the following rules :

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

CS350: Data Structures AVL Trees

CS24 Week 8 Lecture 1

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Trees. Eric McCreath

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

Data Structures and Algorithms for Engineers

M-ary Search Tree. B-Trees. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. Maximum branching factor of M Complete tree has height =

B-trees. It also makes sense to have data structures that use the minimum addressable unit as their base node size.

Search Trees. Undirected graph Directed graph Tree Binary search tree

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

Balanced Search Trees. CS 3110 Fall 2010

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

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

CSCI Trees. Mark Redekopp David Kempe

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

Augmenting Data Structures

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

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Data Structures and Algorithms

Balanced Binary Search Trees

CS 380 ALGORITHM DESIGN AND ANALYSIS

M-ary Search Tree. B-Trees. Solution: B-Trees. B-Tree: Example. B-Tree Properties. B-Trees (4.7 in Weiss)

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

CS102 Binary Search Trees

CMPS 2200 Fall 2017 B-trees Carola Wenk

Binary Trees. Examples:

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

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

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

CS301 - Data Structures Glossary By

Data Structures and Algorithms

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

V Advanced Data Structures

Multi-Way Search Trees

March 20/2003 Jayakanth Srinivasan,

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

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Data Structure - Advanced Topics in Tree -

PART IV. Given 2 sorted arrays, What is the time complexity of merging them together?

Multi-Way Search Trees

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

Cpt S 122 Data Structures. Data Structures Trees

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Algorithms. Red-Black Trees

Binary Search Tree (3A) Young Won Lim 6/2/18

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

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

CSCI2100B Data Structures Trees

Transcription:

Algorithms Deleting from Red-Black Trees B-Trees

Recall the rules for BST deletion 1. If vertex to be deleted is a leaf, just delete it. 2. If vertex to be deleted has just one child, replace it with that child 3. If vertex to be deleted has two children, replace the value of by it s in-order predecessor s value then delete the inorder predecessor (a recursive step)

What can go wrong? 1. If the delete node is red? Not a problem no RB properties violated 2. If the deleted node is black? If the node is not the root, deleting it will change the black-height along some path

The goal of T-D Deletion To delete a red leaf To ensure preserving RB properties: As we traverse the tree looking for the leaf to delete, we change every node we encounter to red. If this causes a violation of the RB properties, we fix it

Terminology Current node and its parent and children are: X is the node being examined T is X s sibling P is X s (and T s) parent R is T s right child L is T s left child Here we assume X is the left child of P. As usual, there are left-right symmetric cases.

Basic Strategy As we traverse the tree, we change every node we visit, X, to Red. When we change X to Red, we know P is also Red (we just came from there) T is black (since P is Red, it s children are Black)

Step 1 Examine the root 1. If both of the root s children are Black a. Make the root Red b. Move X to the appropriate child of the root c. Proceed to step 2 2. Otherwise designate the root as X and proceed to step 2B.

Step 2 the main case As we traverse down the tree, we continually encounter this situation until we reach the node to be deleted X is Black, P is Red, T is Black We are going to color X Red, then recolor other nodes and possibly do rotation(s) based on the color of X s and T s children 2A. X has 2 Black children 2B. X has at least one Red child

Case 2A X has two Black Children X P T 2A1. T has 2 Black Children 2A2. T s left child is Red 2A3. T s right child is Red ** if both of T s children are Red, we can do either 2A2 or 2A3

Case 2A1 X and T have 2 Black Children P P X T X T Just recolor X, P and T and move down the tree

Case 2A2 X has 2 Black Children and T s Left Child is Red Rotate L around T, then L around P Recolor X and P then continue down the tree X L P T X P L L1 L2 T L1 L2

Case 2A3 X has 2 Black Children and T s Right Child is Red Rotate T around P Recolor X, P, T and R then continue down the tree P T X L R1 T R2 R X P L R1 R2 R

Case 2B X has at least one Red child Continue down the tree to the next level If the new X is Red, continue down again If the new X is Black (T is Red, P is Black) Rotate T around P Recolor P and T Back to main case step 2

Case 2B Diagram X P T Move down the tree. P P X T T X If move to Black child (2B2) Rotate T around P; Recolor P and T Back to step 2, the main case If move to the Red child (2B1) Move down again

Step 3 Eventually, find the node to be deleted a leaf or a node with one non-null child that is a leaf. Delete the appropriate node as a Red leaf Color the Root Black Step 4

Example 1 Delete 10 from this RB Tree 15 6 17 3 16 20 10 13 18 23 7 Step 1 Root has 2 Black children. Color Root Red Descend the tree, moving X to 6

Example 1 (cont d) 15 X 6 17 3 16 20 10 13 18 23 7 One of X s children is Red (case 2B). Descend down the tree, arriving at. Since the new X () is also Red (2B1), continue down the tree, arriving at 10.

Example 1 (cont d) 15 6 17 3 16 20 10 X 13 18 23 7 Step 3 -Since 10 is the node to be deleted, replace it s value with the value of it s only child (7) and delete 7 s red node

Example 1 (cont d) 15 6 17 3 16 20 7 13 18 23 The final tree after 7 has replaced 10 and 7 s red node deleted and (step 4) the root has been colored Black.

Example 2 Delete 10 from this RB Tree 15 6 17 3 16 20 2 4 10 13 Step 1 the root does not have 2 Black children. Color the root red, Set X = root and proceed to step 2

Example 2 (cont d) X 15 6 17 3 16 20 2 4 10 13 X has at least one Red child (case 2B). Proceed down the tree, arriving at 6. Since 6 is also Red (case 2B1), continue down the tree, arriving at.

Example 2 (cont d) 15 6 P 17 3 T X 16 20 2 4 10 13 X has 2 Black children. X s sibling (3) also has 2 black children. Case 2A1 recolor X, P, and T and continue down the tree, arriving at 10.

Example 2 (cont d) 15 6 17 3 P 16 20 2 4 X 10 T 13 X is now the leaf to be deleted, but it s Black, so back to step 2. X has 2 Black children and T has 2 Black children case 2A1 Recolor X, P and T. Step 3 -- Now delete 10 as a red leaf. Step 4 -- Recolor the root black

Example 2 Solution 15 6 17 3 16 20 2 4 13

Example 3 Delete 11 from this RB Tree 10 15 5 3 2 4 7 6 9 11 13 Valid and unaffected Right subtree Step 1 root has 2 Black children. Color Root red. Set X to appropriate child of root (10)

Example 3 (cont d) 10 X 15 5 3 7 11 13 2 4 6 9 X has one Red child (case 2B) Traverse down the tree, arriving at.

Example 3 (cont d) 10 P 15 5 T X 3 7 11 13 2 4 6 9 Since we arrived at a black node (case 2B2) assuring T is red and P is black), rotate T around P, recolor T and P Back to step 2

Example 3 (cont d) 15 3 5 7 10 T P X 2 4 6 9 11 13 Now X is Black with Red parent and Black sibling. X and T both have 2 Black children (case 2A1) Just recolor X, P and T and continue traversal

Example 3 (cont d) 15 5 3 7 10 P 2 4 6 9 11 Having traversed down the tree, we arrive at 11, the leaf to be deleted, but it s Black, so back to step 2. X and T both have two Black children. Recolor X, P and T. Step 3 -- delete 11 as a red leaf. Step 4 -- Recolor the root black X 13 T

Example 3 Solution 15 5 3 7 10 2 4 6 9 13

B-Trees

B-Trees One way for having a balanced tree is using m-way trees Balanced trees do not require that each node always be full. Empty space will permit insertion without rebalancing. Allowing empty space after a deletion can also avoid rebalancing. Rebalancing will sometimes be necessary

B-Tree Example with N = 2 2 3 8 13 27 The root has between 2 and 2N+1 children. Each non-root internal node has between N+1 and 2N+1 children. All leaf nodes are at the same level. (Leaf nodes are shown here by null pointers.)

Insert 10 2 3 8 10 13 27 We find the location for 10 by following a path from the root using the stored key values to guide the search. The 1st child of the root has room for the new element, so we store it there.

Insert 11 2 3 8 10 11 13 27 11 should be inserted to the left child of root. But there is no more room in the left child of the root to hold 11. Therefore, we must split this node...

Insert 11 (Continued) 8 2 3 10 11 13 27 The 2N + 1 children are divided evenly between the old and new nodes. The parent gets one new child. (If the parent become overfull, then it, too, will have to be split).

Remove 8 8 2 3 10 11 13 27 Removing 8 might force us to move another key up from one of the children. It could either be the 3 from the 1st child or the 10 from the second child. However, neither child has more than the minimum number of children (3), so the two nodes will have to be merged. Nothing moves up.

Remove 8 (Continued) 2 3 10 11 13 27 The root contains one fewer key, and has one fewer child.

Remove 13 2 3 10 11 13 27 Removing 13 would cause the node containing it have less than minimum values. To fix this, we try to reassign one key from a sibling that has spares (re-distribute).

Remove 13 (Cont) 11 2 3 10 27 The 13 is replaced by the parent s key. The parent s key is replaced by the spare key 11 from the left sibling. The sibling has one fewer element.