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

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

Data Structures and Algorithms CMPSC 465

CS 3343 Fall 2007 Red-black trees Carola Wenk

CMPS 2200 Fall 2015 Red-black trees Carola Wenk

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

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

Algorithms. AVL Tree

Binary Search Tree - Best Time. AVL Trees. Binary Search Tree - Worst Time. Balanced and unbalanced BST

Ch04 Balanced Search Trees

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

CISC 235: Topic 4. Balanced Binary Search Trees

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

Lecture 6: Analysis of Algorithms (CS )

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

Balanced Binary Search Trees

Search Trees. Undirected graph Directed graph Tree Binary search tree

Lecture: Analysis of Algorithms (CS )

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

Recall from Last Time: AVL Trees

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

Search Trees. COMPSCI 355 Fall 2016

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

Analysis of Algorithms

Algorithms. Deleting from Red-Black Trees B-Trees

COMP171. AVL-Trees (Part 1)

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

Augmenting Data Structures

Part 2: Balanced Trees

CS 380 ALGORITHM DESIGN AND ANALYSIS

Properties of red-black trees

Search Trees - 1 Venkatanatha Sarma Y

Self-Balancing Search Trees. Chapter 11

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

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

CHAPTER 10 AVL TREES. 3 8 z 4

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

COMP Analysis of Algorithms & Data Structures

CS350: Data Structures Red-Black Trees

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

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.

Balanced 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

CS102 Binary Search Trees

ECE250: Algorithms and Data Structures AVL Trees (Part A)

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

13.4 Deletion in red-black trees

13.4 Deletion in red-black trees

Algorithms. Red-Black Trees

Data Structures Week #6. Special Trees

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

Balanced search trees. DS 2017/2018

Data Structure - Advanced Topics in Tree -

Advanced Set Representation Methods

COMP Analysis of Algorithms & Data Structures

COMP251: Red-black trees

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

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

Module 4: Dictionaries and Balanced Search Trees

Chapter 2: Basic Data Structures

CSCI2100B Data Structures Trees

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

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

Data Structures Week #6. Special Trees

COMP Analysis of Algorithms & Data Structures

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

Data Structures Lesson 7

Binary Search Trees, etc.

CS350: Data Structures AVL Trees

Balanced Trees Part Two

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

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

Data Structures Week #6. Special Trees

DATA STRUCTURES AND ALGORITHMS

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

SFU CMPT Lecture: Week 9

Balanced Search Trees. CS 3110 Fall 2010

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

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

Balanced Binary Search Trees

CS 310: Tree Rotations and AVL Trees

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

Direct Addressing Hash table: Collision resolution how handle collisions Hash Functions:

Data Structures and Algorithms

Trees. Eric McCreath

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

Jana Kosecka. Red-Black Trees Graph Algorithms. Many slides here are based on E. Demaine, D. Luebke slides

CMPE 160: Introduction to Object Oriented Programming

Binary search trees (chapters )

Dynamic Access Binary Search Trees

Problem Set 5. MIT students: Each problem should be done on a separate sheet (or sheets) of three-hole punched paper.

CS Transform-and-Conquer

AVL Trees (10.2) AVL Trees

Binary Search Trees. Analysis of Algorithms

Data Structures in Java

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

Analysis of Algorithms

We assume uniform hashing (UH):

Search Structures. Kyungran Kang

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

8.1. Optimal Binary Search Trees:

Transcription:

CS62: Foundations of Algorithm Design and Machine Learning Sourangshu Bhattacharya

Binary Search Tree - Best Time All BST operations are O(d), where d is tree depth minimum d is d = ëlog for a binary tree with 2Nû N nodes What is the best case tree? What is the worst case tree? So, best case running time of BST operations is O(log N) 12/26/3 AVL Trees - Lecture 8 2

Binary Search Tree - Worst Time Worst case running time is O(N) What happens when you Insert elements in ascending order? Insert: 2, 4, 6, 8, 1, 12 into an empty BST Problem: Lack of balance : compare depths of left and right subtree Unbalanced degenerate tree 12/26/3 AVL Trees - Lecture 8 3

Balanced and unbalanced BST 1 4 4 2 3 4 5 2 5 1 3 Is this balanced? 2 6 6 1 3 5 7 7 12/26/3 AVL Trees - Lecture 8 4

Approaches to balancing trees Don't balance May end up with some nodes very deep Strict balance The tree must always be balanced perfectly Pretty good balance Only allow a little out of balance Adjust on access Self-adjusting 12/26/3 AVL Trees - Lecture 8 5

Balancing Binary Search Trees Many algorithms exist for keeping binary search trees balanced Adelson-Velskii and Landis (AVL) trees (heightbalanced trees) Splay trees and other self-adjusting trees B-trees and other multiway search trees 12/26/3 AVL Trees - Lecture 8 6

Perfect Balance Want a complete tree after every operation tree is full except possibly in the lower right This is expensive For example, insert 2 in the tree on the left and then rebuild as a complete tree 6 4 9 Insert 2 & complete tree 5 2 8 1 5 8 1 4 6 9 12/26/3 AVL Trees - Lecture 8 7

AVL - Good but not Perfect Balance AVL trees are height-balanced binary search trees Balance factor of a node height(left subtree) - height(right subtree) An AVL tree has balance factor calculated at every node For every node, heights of left and right subtree can differ by no more than 1 Store current heights in each node 12/26/3 AVL Trees - Lecture 8 8

Height of an AVL Tree N(h) = minimum number of nodes in an AVL tree of height h. Basis N() = 1, N(1) = 2 h Induction N(h) = N(h-1) + N(h-2) + 1 Solution (recall Fibonacci analysis) N(h) > f h (f» 1.62) h-2 h-1 12/26/3 AVL Trees - Lecture 8 9

Height of an AVL Tree N(h) > f h (f» 1.62) Suppose we have n nodes in an AVL tree of height h. n > N(h) (because N(h) was the minimum) n > f h hence log f n > h (relatively well balanced tree!!) h < 1.44 log 2 n (i.e., Find takes O(log n)) 12/26/3 AVL Trees - Lecture 8 1

Node Heights 1 Tree A (AVL) height=2 BF=1-=1 6 4 9 1 5 1 Tree B (AVL) 2 6 1 4 9 1 5 8 height of node = h balance factor = h left -h right empty height = -1 12/26/3 AVL Trees - Lecture 8 11

Node Heights after Insert 7 1 2 6 1 4 9 1 5 Tree A (AVL) 7 Tree B (not AVL) 1 3 6 1 2 4 9 1 5 height of node = h balance factor = h left -h right empty height = -1 7 8 balance factor 1-(-1) = 2-1 12/26/3 AVL Trees - Lecture 8 12

Insert and Rotation in AVL Trees Insert operation may cause balance factor to become 2 or 2 for some node only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference h left -h right ) is 2 or 2, adjust tree by rotation around the node 12/26/3 AVL Trees - Lecture 8 13

Single Rotation in an AVL Tree 1 2 6 1 2 4 9 1 5 8 1 4 1 5 2 6 7 1 8 9 7 12/26/3 AVL Trees - Lecture 8 14

Insertions in AVL Trees Let the node that needs rebalancing be a. There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of a. 2. Insertion into right subtree of right child of a. Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of a. 4. Insertion into left subtree of right child of a. The rebalancing is performed through four separate rotation algorithms. 12/26/3 AVL Trees - Lecture 8 15

AVL Insertion: Outside Case Consider a valid AVL subtree j k h h h Z X Y 12/26/3 AVL Trees - Lecture 8 16

AVL Insertion: Outside Case k j h Inserting into X destroys the AVL property at node j h+1 h Z Y X 12/26/3 AVL Trees - Lecture 8 17

AVL Insertion: Outside Case j Do a right rotation k h h+1 h Z Y X 12/26/3 AVL Trees - Lecture 8 18

Single right rotation j Do a right rotation k h h+1 h Z Y X 12/26/3 AVL Trees - Lecture 8 19

Outside Case Completed k Right rotation done! ( Left rotation is mirror symmetric) h+1 j h h X Y Z AVL property has been restored! 12/26/3 AVL Trees - Lecture 8 2

AVL Insertion: Inside Case Consider a valid AVL subtree j k h h h Z X Y 12/26/3 AVL Trees - Lecture 8 21

AVL Insertion: Inside Case Inserting into Y destroys the AVL property at node j k j Does right rotation restore balance? h h h+1 Z X Y 12/26/3 AVL Trees - Lecture 8 22

AVL Insertion: Inside Case h k j Right rotation does not restore balance now k is out of balance X h+1 h Z Y 12/26/3 AVL Trees - Lecture 8 23

AVL Insertion: Inside Case Consider the structure of subtree Y j k h X h h+1 Z Y 12/26/3 AVL Trees - Lecture 8 24

AVL Insertion: Inside Case Y = node i and subtrees V and W j k h h i h+1 Z X h or h-1 V W 12/26/3 AVL Trees - Lecture 8 25

AVL Insertion: Inside Case j We will do a left-right double rotation... k X i Z V W 12/26/3 AVL Trees - Lecture 8 26

Double rotation : first rotation j left rotation complete i k Z W X V 12/26/3 AVL Trees - Lecture 8 27

Double rotation : second rotation j Now do a right rotation i k Z W X V 12/26/3 AVL Trees - Lecture 8 28

Double rotation : second rotation right rotation complete i Balance has been restored k j h h or h-1 h X V W Z 12/26/3 AVL Trees - Lecture 8 29

Implementation balance (1,,-1) key left right No need to keep the height; just the difference in height, i.e. the balance factor; this has to be modified on the path of insertion even if you don t perform rotations Once you have performed a rotation (single or double) you won t need to go back up the tree 12/26/3 AVL Trees - Lecture 8 3

Single Rotation RotateFromRight(n : reference node pointer) { p : node pointer; p := n.right; n n.right := p.left; p.left := n; n := p } You also need to modify the heights or balance factors of n and p X Y Z Insert 12/26/3 AVL Trees - Lecture 8 31

Double Rotation Implement Double Rotation in two lines. DoubleRotateFromRight(n : reference node pointer) {???? n } X Z 12/26/3 AVL Trees - Lecture 8 32 V W

Insertion in AVL Trees Insert at the leaf (as for all BST) only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference h left -h right ) is 2 or 2, adjust tree by rotation around the node 12/26/3 AVL Trees - Lecture 8 33

Insert in BST Insert(T : reference tree pointer, x : element) : integer { if T = null then T := new tree; T.data := x; return 1;//the links to //children are null case T.data = x : return ; //Duplicate do nothing T.data > x : return Insert(T.left, x); T.data < x : return Insert(T.right, x); endcase } 12/26/3 AVL Trees - Lecture 8 34

Insert in AVL trees Insert(T : reference tree pointer, x : element) : { if T = null then {T := new tree; T.data := x; height := ; return;} case T.data = x : return ; //Duplicate do nothing T.data > x : Insert(T.left, x); if ((height(t.left)- height(t.right)) = 2){ if (T.left.data > x ) then //outside case T = RotatefromLeft (T); else //inside case T = DoubleRotatefromLeft (T);} T.data < x : Insert(T.right, x); code similar to the left case Endcase T.height := max(height(t.left),height(t.right)) +1; return; } 12/26/3 AVL Trees - Lecture 8 35

Example of Insertions in an AVL Tree 2 2 1 1 3 25 35 Insert 5, 4 12/26/3 AVL Trees - Lecture 8 36

Example of Insertions in an AVL Tree 5 1 2 2 1 1 3 25 35 5 1 2 1 3 3 25 2 35 1 Now Insert 45 4 12/26/3 AVL Trees - Lecture 8 37

Single rotation (outside case) 5 1 3 2 2 1 3 25 Imbalance 2 35 1 4 5 1 2 1 3 3 25 2 4 1 35 45 45 Now Insert 34 12/26/3 AVL Trees - Lecture 8 38

Double rotation (inside case) 5 1 3 2 3 1 3 Imbalance 25 2 4 5 1 2 1 35 1 3 1 35 45 25 34 3 2 4 1 45 Insertion of 34 34 12/26/3 AVL Trees - Lecture 8 39

AVL Tree Deletion Similar but more complex than insertion Rotations and double rotations needed to rebalance Imbalance may propagate upward so that many rotations may be needed. 12/26/3 AVL Trees - Lecture 8 4

Pros and Cons of AVL Trees Arguments for AVL trees: 1. Search is O(log N) since AVL trees are always balanced. 2. Insertion and deletions are also O(logn) 3. The height balancing adds no more than a constant factor to the speed of insertion. Arguments against using AVL trees: 1. Difficult to program & debug; more space for balance factor. 2. Asymptotically faster but rebalancing costs time. 3. Most large searches are done in database systems on disk and use other structures (e.g. B-trees). 4. May be OK to have O(N) for a single operation if total run time for many consecutive operations is fast (e.g. Splay trees). 12/26/3 AVL Trees - Lecture 8 41

Double Rotation Solution DoubleRotateFromRight(n : reference node pointer) { RotateFromLeft(n.right); n RotateFromRight(n); } X Z V W 12/26/3 AVL Trees - Lecture 8 42

Balanced search trees Balanced search tree: A search-tree data structure for which a height of O(lg 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 October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.2

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 and leaves (NIL s) are black. 3. If a node is red, then its parent is black. 4. All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x). October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.3

Example of a red-black 77 tree 33 1188 NIL NIL 11 2222 h = 4 88 NIL 1111 2266 NIL NIL NIL NIL NIL NIL October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.4

Example of a red-black 77 tree 33 1188 NIL NIL 11 2222 88 1111 NIL 2266 NIL NIL NIL NIL NIL NIL 1. Every node is either red or black. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.5

Example of a red-black 77 tree 33 1188 NIL NIL 11 2222 88 1111 NIL 2266 NIL NIL NIL NIL NIL NIL 2. The root and leaves (NIL s) are black. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.6

Example of a red-black 77 tree 33 1188 NIL NIL 11 2222 NIL 88 1111 2266 NIL NIL NIL NIL NIL NIL 3. If a node is red, then its parent is black. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.7

Example of a red-black 77 tree bh = 2 33 1188 bh = 2 NIL NIL bh = 1 11 2222 bh = 1 NIL 88 1111 2266 bh = NIL NIL NIL NIL NIL NIL 4. All simple paths from any node x to a descendant leaf have the same number of black nodes = black-height(x). October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.8

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 lg(n + 1). Proof. (The book uses induction. Read carefully.) INTUITION: Merge red nodes into their black parents. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.9

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 lg(n + 1). Proof. (The book uses induction. Read carefully.) INTUITION: Merge red nodes into their black parents. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.1

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 lg(n + 1). Proof. (The book uses induction. Read carefully.) INTUITION: Merge red nodes into their black parents. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.11

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 lg(n + 1). Proof. (The book uses induction. Read carefully.) INTUITION: Merge red nodes into their black parents. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.12

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 lg(n + 1). Proof. (The book uses induction. Read carefully.) INTUITION: Merge red nodes into their black parents. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.13

Height of a red-black tree Theorem. A red-black tree with n keys has height h 2 lg(n + 1). Proof. (The book uses induction. Read carefully.) INTUITION: 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. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.14

Proof (continued) We have h ³ h/2, since at most half the leaves on any path are red. The number of leaves in each tree is n + 1 Þ n + 1 ³ 2 h' Þ lg(n + 1) ³ h' ³ h/2 Þ h 2 lg(n + 1). h h October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.15

Query operations Corollary. The queries SEARCH, MIN, MAX, SUCCESSOR, and PREDECESSOR all run in O(lg n) time on a red-black tree with n nodes. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.16

Modifying operations The operations INSERT and DELETE cause modifications to the red-black tree: the operation itself, color changes, restructuring the links of the tree via rotations. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.17

Rotation s B RIGHT-ROTATE(B) A a A b g LEFT-ROTATE(A) a b B g Rotations maintain the inorder ordering of keys: a Î a, b Î b, c Î g Þ a A b B c. A rotation can be performed in O(1) time. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.18

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. 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. 77 Example: 33 1188 11 2222 88 1111 2266 October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.19

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. 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. Example: 33 Insert x =15. Recolor, moving the violation up the tree. 77 88 11 1111 1188 1155 2222 2266 October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.2

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. 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. Example: 33 Insert x =15. Recolor, moving the violation up the tree. RIGHT-ROTATE(18). 77 88 11 1111 1188 1155 2222 2266 October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.21

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. 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. 77 Example: 33 Insert x =15. Recolor, moving the violation up the tree. RIGHT-ROTATE(18). LEFT-ROTATE(7) and recolor. 88 11 1188 1111 2222 1155 2266 October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.22

Insertion into a red-black tree IDEA: Insert x in tree. Color x red. Only redblack property 3 might be violated. Move the violation up the tree by recoloring until it can be fixed with rotations and recoloring. Example: Insert x =15. 77 Recolor, moving the 33 violation up the tree. RIGHT-ROTATE(18). LEFT-ROTATE(7) and recolor. 88 11 1188 1111 2222 1155 2266 October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.23

Pseudocode RB-INSERT(T, x) TREE-INSERT(T, x) color[x] RED only RB property 3 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 October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.24

Graphical notation Let All denote a subtree with a black root. s have the same black-height. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.25

Case 1 AA x BB CC DD y Recolor AA BB CC new x DD (Or, children of A are swapped.) Push C s black onto A and D, and recurse, since C s parent may be red. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.26

Case 2 AA x BB CC y LEFT-ROTATE(A) x A A BB CC y Transform to Case 3. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.27

Case 3 x AA BB CC RIGHT-ROTATE(C) y AA BB CC Done! No more violations of RB property 3 are possible. October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.28

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(lg n) with O(1) rotations. RB-DELETE same asymptotic running time and number of rotations as RB-INSERT (see textbook). October 19, 25 Copyright 21-5 by Erik D. Demaine and Charles E. Leiserson L7.29