RBT Operations. The basic algorithm for inserting a node into an RBT is:

Similar documents
CSS 343 Data Structures, Algorithms, and Discrete Math II. Balanced Search Trees. Yusuf Pisan

CSE 100: RED-BLACK TREES

Topic 23 Red Black Trees

Balanced Trees. Balanced Trees Tree. 2-3 Tree. 2 Node. Binary search trees are not guaranteed to be balanced given random inserts and deletes

CS/ENGRD 2110 Object-Oriented Programming and Data Structures Spring 2012 Thorsten Joachims. Lecture 17: Heaps and Priority Queues

5 AVL trees: deletion

PRIORITY QUEUES AND HEAPS. Lecture 19 CS2110 Spring 2014

PRIORITY QUEUES AND HEAPS

CSI33 Data Structures

MITOCW 6. AVL Trees, AVL Sort

Binary Search Tree (Part 2 The AVL-tree)

MITOCW ocw lec11

and 6.855J. Network Simplex Animations

Self-Adjusting Binary Search Trees. Andrei Pârvu

CS : Data Structures

PRIORITY QUEUES AND HEAPS. Slides of Ken Birman, Cornell University

Past questions from the last 6 years of exams for programming 101 with answers.

Random Binary Search Trees. EECS 214, Fall 2017

Splay tree concept Splaying operation: double-rotations Splay insertion Splay search Splay deletion Running time of splay tree operations

have to get on the phone or family members for the names of more distant relatives.

mywbut.com Two agent games : alpha beta pruning

CPS331 Lecture: Search in Games last revised 2/16/10

Conversion Masters in IT (MIT) AI as Representation and Search. (Representation and Search Strategies) Lecture 002. Sandro Spina

DATA STRUCTURE TREES UNIT II

CS188 Spring 2010 Section 3: Game Trees

Simple Search Algorithms

Heuristics, and what to do if you don t know what to do. Carl Hultquist

CS188 Spring 2010 Section 3: Game Trees

Twin Binary Sequences: A Nonredundant Representation for General Nonslicing Floorplan

Computer Graphics (CS/ECE 545) Lecture 7: Morphology (Part 2) & Regions in Binary Images (Part 1)

Universal Cycles for Permutations Theory and Applications

Lecture 14. Questions? Friday, February 10 CS 430 Artificial Intelligence - Lecture 14 1

Problem A. Worst Locations

UMBC 671 Midterm Exam 19 October 2009

Algorithms for Data Structures: Search for Games. Phillip Smith 27/11/13

Solving the 4 x 4 Cube

AN ALTERNATIVE METHOD FOR ASSOCIATION RULES

A New Method for the Visualization Binary Trees using L-Systems

ARTIFICIAL INTELLIGENCE (CS 370D)

Minimax Trees: Utility Evaluation, Tree Evaluation, Pruning

1 Permutations. Example 1. Lecture #2 Sept 26, Chris Piech CS 109 Combinatorics

Games (adversarial search problems)

1 Permutations. 1.1 Example 1. Lisa Yan CS 109 Combinatorics. Lecture Notes #2 June 27, 2018

CMPUT 396 Tic-Tac-Toe Game

Part I: The Swap Puzzle

CSE 332: Data Structures and Parallelism Games, Minimax, and Alpha-Beta Pruning. Playing Games. X s Turn. O s Turn. X s Turn.

Adversary Search. Ref: Chapter 5

Chapter 7: Sorting 7.1. Original

Game Theory and Randomized Algorithms

16.410/413 Principles of Autonomy and Decision Making

COMP 2804 solutions Assignment 4

Games and Adversarial Search II

Artificial Intelligence Lecture 3

INF September 25, The deadline is postponed to Tuesday, October 3

Introduction to. Algorithms. Lecture 10. Prof. Constantinos Daskalakis CLRS

Homework Assignment #1

Checkpoint Questions Due Monday, October 7 at 2:15 PM Remaining Questions Due Friday, October 11 at 2:15 PM

Foundations of Artificial Intelligence

SEARCHING is both a method of solving problems and

Binary trees. Application: AVL trees and the golden ratio. The golden ratio. φ 1=1/φ 1. φ =1+

: Principles of Automated Reasoning and Decision Making Midterm

43.1 Introduction. Foundations of Artificial Intelligence Introduction Monte-Carlo Methods Monte-Carlo Tree Search. 43.

Adversarial Search 1

Greedy Algorithms. Kleinberg and Tardos, Chapter 4

Search then involves moving from state-to-state in the problem space to find a goal (or to terminate without finding a goal).

COS 226 Algorithms and Data Structures Fall Midterm Exam

CS 188: Artificial Intelligence Spring Announcements

Announcements. CS 188: Artificial Intelligence Spring Game Playing State-of-the-Art. Overview. Game Playing. GamesCrafters

Permutations. Example 1. Lecture Notes #2 June 28, Will Monroe CS 109 Combinatorics

DeltaCad and Your Cylinder (Shepherd s) Sundial Carl Sabanski

Introduction to. Algorithms. Lecture 10. Prof. Piotr Indyk

Graph Application in The Strategy of Solving 2048 Tile Game

An O(1) Time Algorithm for Generating Multiset Permutations

PhyzLab: Fork it Over

The Theory Behind the z/architecture Sort Assist Instructions

RAM Analytical Skills Introductory Theory Primer Part 1: Intervals Part 2: Scales and Keys Part 3: Forming Chords Within Keys Part 4: Voice-leading

MITOCW R11. Principles of Algorithm Design

CSE 573 Problem Set 1. Answers on 10/17/08

The tenure game. The tenure game. Winning strategies for the tenure game. Winning condition for the tenure game

1 V NAME. Clock Pulse. Unipolar NRZ NRZ AMI NRZ HDB3

Create styles that control the display of Civil 3D objects. Copy styles from one drawing to another drawing.

Game-Playing & Adversarial Search

1 Introduction. 1.1 Game play. CSC 261 Lab 4: Adversarial Search Fall Assigned: Tuesday 24 September 2013

In this lecture, we will learn about some more basic laws governing the behaviour of electronic circuits beyond that of Ohm s law.

Unit-III Chap-II Adversarial Search. Created by: Ashish Shah 1

CSE : Python Programming

6.034 Quiz 2 20 October 2010

G51PGP: Software Paradigms. Object Oriented Coursework 4

Backward Induction. ISCI 330 Lecture 14. March 1, Backward Induction ISCI 330 Lecture 14, Slide 1

Will Monroe June 28, with materials by Mehran Sahami and Chris Piech. Combinatorics

MAS336 Computational Problem Solving. Problem 3: Eight Queens

Parallel Randomized Best-First Search

Announcements. Homework 1 solutions posted. Test in 2 weeks (27 th ) -Covers up to and including HW2 (informed search)

CS188: Section Handout 1, Uninformed Search SOLUTIONS

Diane Burton, STEM Outreach.

EECS 583 Class 7 Classic Code Optimization cont d

Adversarial Search. Human-aware Robotics. 2018/01/25 Chapter 5 in R&N 3rd Ø Announcement: Slides for this lecture are here:

Launchpad Maths. Arithmetic II

UNIVERSITY of PENNSYLVANIA CIS 391/521: Fundamentals of AI Midterm 1, Spring 2010

Previous Lecture. How can computation sort data faster for you? Sorting Algorithms: Speed Comparison. Recursive Algorithms 10/31/11

Transcription:

RBT Operations The basic algorithm for inserting a node into an RBT is: 1: procedure RBT INSERT(T, x) 2: BST insert(t, x) : colour[x] red 4: if parent[x] = red then 5: RBT insert fixup(t, x) 6: end if 7: end procedure By colouring x red, we may violate the property that says red nodes have black children. Think of x as the problem node. To fix up the violation we distinguish three cases: 1. x s uncle is red 2. x s uncle is black and x is an inner child.. x s uncle is black and x is an outer child. All fixups push the problem back up the tree, so RBT insert fixup needs to traverse the tree upwards until either there is no problem anymore, or we reach the root of the tree. This can be done recursively or iteratively. COSC242 Lecture 16 / Slide 1

Case 1: x s uncle is red 1 4 uncle 2 x Solution: push grandparent s black down one level, and make the grandparent red. new x 1 4 2 COSC242 Lecture 16 / Slide 2

Case 2: x s uncle is black and x is an inner child. 1 uncle 2 x Solution: rotate left around x s parent, turning the problem into a case problem. 2 new x 1 COSC242 Lecture 16 / Slide

Case : x s uncle is black and x is an outer child 2 uncle x 1 Solution: make x s parent black and x s grandparent red. Then rotate right around x s grandparent. 2 1 In this case the new subtree has black at its root and therefore we don t need to propagate the fixup any further up the tree. Note that in these three cases we ve used examples where x is on the left of the root of the subtree. There are equivalent cases which are mirror images of the ones discussed here. COSC242 Lecture 16 / Slide 4

Example Insertion Here are the RBTs that successively result when inserting 9,, 6,, 5: 9 9 9 case 6 9 6 case 1 6 9 6 9 6 9 5 case 2 case 6 9 5 5 9 6 COSC242 Lecture 16 / Slide 5

BST Deletion To delete a node z, BST-deletion recursively searches for z, and then uses replace-the-root. Replace-the-root falls into two cases: 1. if the root has < 2 (nonempty) children, replace it by a child (empty if thats all that is available); 2. if the root has two children, replace the root by its successor (minimum of right subtree). Some node, call it y, eventually gets spliced out. It may be that y = z, or y may be z s successor. delete 0 0 5 5 COSC242 Lecture 16 / Slide 6

RBT deletion To delete a node, z, in an RBT: 1. delete z as for a BST 2. fix any RBT violations (how?) Call the spliced-out node y (remember that the spliced out node is the node that is removed from the tree - which is not necessarily z). Here are the RBT properties again: 1. Every node is either red or black 2. The root is black. All dummy leaves are black (think of dummy leaves as NULL subtrees) 4. If a node is red, then the roots of both its subtrees are black 5. For each node, all paths from the node to the leaves contain the same number of black nodes (including the dummy leaves). If y is red, can any of those properties be violated? If y is black, can any of those properties be violated? COSC242 Lecture 16 / Slide 7

If y is black If y is black, what can go wrong: If y was the root, the new root might be red (property 2). This is easy to fix - just make the root black. Splicing out y might leave two red nodes in a parent-child relationship. The black-height of at least one path will be reduced by one (an imbalance). This is always the case when splicing out a black node. Let s start by defining some labels: z is the node to be deleted y is the node that gets spliced out (sometimes y = z and sometimes y is z s successor) x is the child that replaced y w is the new sibling of x COSC242 Lecture 16 / Slide

Four cases There are 4 cases to handle: 1. x s sibling, w, is red, fix then fall to case 2, or 4 2. w is black and has two black children, fix then traverse up the tree. w is black and w s inner child is red and outer child is black, fix then fall to case 4 4. w is black and w s outer child is red, fix and terminate The trick to fixing all of these is to give x an extra black value. Since we ve removed a black node, giving x the value of 2 black restores the black-height property of the tree. We move this extra black value up the tree until we can either give it to a red node, or until we reach the root. COSC242 Lecture 16 / Slide 9

Case 1 x s sibling w is red. We can convert this case to another case by rotating and swapping colours: x(bb) w 0 x(bb) new w 0 Now we have case 2, or 4. COSC242 Lecture 16 / Slide

Case 2 x s sibling is not red and has two black children: x(bb) w 0 new x (bb or rb) 0 Either we are done (rb) or we head up the tree with a new x. COSC242 Lecture 16 / Slide 11

Case w s inner child is red and outer child is black: x (bb) w 0 x (bb) new w 0 Now we have Case 4. COSC242 Lecture 16 / Slide 12

Case 4 w s outer child is red: x (bb) w 0 x 0 COSC242 Lecture 16 / Slide 1

Exercises 1. Draw all the RBTs that result from successive insertions of 5, 4,, 2, 1 into an initially empty RBT. State which cases apply. 2. Draw all the RBTs that result from successive insertion of 11,, 9,, 2, 6,, 5, 4, 1 into an initially empty RBT. State which cases apply.. Draw all the RBTs that result from successive insertion of 14, 12, 65,, 92 into an initially empty RBT. Now draw the RBTs that result from deletion of 65. State which cases apply. 4. Draw the RBTs that result from successive insertion of, 92, 12, 2, 17 into an initially empty RBT. Now draw the RBTs that result from deletion of 92. State which cases apply. 5. Draw all the RBTs that result from successive insertion of 6, 96, 9, 4, 11, 26, 7 into an initially empty RBT. Now draw the RBTs that result from deletion of 96. State which cases apply. COSC242 Lecture 16 / Slide 14