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

Similar documents
Algorithms. Deleting from Red-Black Trees B-Trees

CS 380 ALGORITHM DESIGN AND ANALYSIS

Properties of red-black trees

Lecture 6: Analysis of Algorithms (CS )

Algorithms. Red-Black Trees

Graduate Algorithms CS F-07 Red/Black Trees

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

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

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

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

CS350: Data Structures Red-Black Trees

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

Search Trees. Undirected graph Directed graph Tree Binary search tree

Lecture: Analysis of Algorithms (CS )

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

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

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

Algorithms. AVL Tree

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

13.4 Deletion in red-black trees

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.

Search Trees. COMPSCI 355 Fall 2016

Data Structures and Algorithms CMPSC 465

13.4 Deletion in red-black trees

Binary Trees, Binary Search Trees

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

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

Balanced search trees

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

Balanced search trees. DS 2017/2018

CMPT 225. Red black trees

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

Trees (Part 1, Theoretical) CSE 2320 Algorithms and Data Structures University of Texas at Arlington

Ch04 Balanced Search Trees

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

Binary Search Trees. Analysis of Algorithms

COMP171. AVL-Trees (Part 1)

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

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

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

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

Trees. Eric McCreath

Chapter 22 Splay Trees

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

Dynamic Access Binary Search Trees

CMPS 2200 Fall 2015 Red-black trees Carola Wenk

CS 3343 Fall 2007 Red-black trees Carola Wenk

CS 758/858: Algorithms

Binary Trees

Self-Balancing Search Trees. Chapter 11

CISC 235: Topic 4. Balanced Binary Search Trees

CSE2331/5331. Topic 7: Balanced search trees. Rotate operation Red-black tree Augmenting data struct. CSE 2331/5331

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

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

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

Algorithms, Spring 2014, CSE, OSU Lecture 5: Red-black trees

COMP251: Red-black trees

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

ICS 691: Advanced Data Structures Spring Lecture 3

Analysis of Algorithms

We assume uniform hashing (UH):

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

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

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

Red-Black, Splay and Huffman Trees

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:

Chapter 12 Advanced Data Structures

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

Balanced Binary Search Trees

Red-Black Trees (2) Antonio Carzaniga. April 23, Faculty of Informatics Università della Svizzera italiana Antonio Carzaniga

Dynamic Access Binary Search Trees

TREES. Trees - Introduction

Red-black tree. Background and terminology. Uses and advantages

Algorithms. 演算法 Data Structures (focus on Trees)

Data Structures and Algorithms

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

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

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?

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

CS102 Binary Search Trees

Balanced Binary Search Trees. Victor Gao

Section 4 SOLUTION: AVL Trees & B-Trees

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

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

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

Data Structures Week #6. Special Trees

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

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

Cpt S 122 Data Structures. Data Structures Trees

CS S-08 Priority Queues Heaps 1

SFU CMPT Lecture: Week 9

Data Structures Week #6. Special Trees

Advanced Algorithms. Class Notes for Thursday, September 18, 2014 Bernard Moret

Data Structures and Algorithms

Analysis of Algorithms

Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

Data Structures Week #6. Special Trees

Programming II (CS300)

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

B-Trees. Based on materials by D. Frey and T. Anastasio

Transcription:

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

Quick Review of Binary Search Trees n Given a node n... q All elements of n s left subtree are less than n.data q All elements of n s right subtree are greater than n.data n We are prohibiting duplicate values n Insert/Find/Remove are O(height) (why?) n The tree s height varies between lg N and N q A balanced tree has height lg N 2

Review of Tree Rotations: Zig-Zig (Node and Parent are Same Side) Rotate P around G, then X around P 3

Review of Tree Rotations: Zig-Zag (Node and Parent are Different Sides) Rotate X around P, then X around G 4

DEFINITIONS 5

Red-Black Trees n Definition: A red-black tree is a binary search tree in which: q q q q q Every node is colored either Red or Black. Each NULL pointer is considered to be a Black node. If a node is Red, then both of its children are Black. Every path from a node to a NULL contains the same number of Black nodes. By convention, the root is Black n Definition: The black-height of a node X in a red-black tree is the number of Black nodes on any path to a NULL, not counting X. 6

X A Red-Black Tree with NULLs shown Black-Height of the tree (the root) = 3 Black-Height of node X = 2 7

A Red-Black Tree with Black-Height = 3 8

X Black Height of the tree? Black Height of X? 9

Theorem 1 Any red-black tree with root x, has n 2 bh(x) 1 nodes, where bh(x) is the black height of node x. Proof: by induction on height of x. 10

Theorem 2 In a red-black tree, at least half the nodes on any path from the root to a NULL must be Black. Proof If there is a Red node on the path, there must be a corresponding Black node. Algebraically this theorem means bh( x ) h/2 11

Theorem 3 In a red-black tree, no path from any node, X, to a NULL is more than twice as long as any other path from X to any other NULL. Proof: By definition, every path from a node to any NULL contains the same number of Black nodes. By Theorem 2, a least ½ the nodes on any such path are Black. Therefore, there can no more than twice as many nodes on any path from X to a NULL as on any other path. Therefore the length of every path is no more than twice as long as any other path. 12

Theorem 4 A red-black tree with n nodes has height h 2 lg(n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 2, bh(x) h/2 From Theorem 1, n 2 bh(x) - 1 Therefore n 2 h/2 1 n + 1 2 h/2 lg(n + 1) h/2 2lg(n + 1) h 13

BOTTOM-UP INSERTION 14

Bottom Up Insertion n Insert node as usual in BST n Color the node Red n What Red-Black property may be violated? q Every node is Red or Black? q NULLs are Black? q If node is Red, both children must be Black? q Every path from node to descendant NULL must contain the same number of Blacks? 15

Bottom Up Insertion n n Insert node; Color it Red; X is pointer to it Cases 0: X is the root -- color it Black 1: Both parent and uncle are Red -- color parent and uncle Black, color grandparent Red. Point X to grandparent and check new situation. 2 (zig-zag): Parent is Red, but uncle is Black. X and its parent are opposite type children -- color grandparent Red, color X Black, rotate left(right) on parent, rotate right(left) on grandparent 3 (zig-zig): Parent is Red, but uncle is Black. X and its parent are both left (right) children -- color parent Black, color grandparent Red, rotate right(left) on grandparent 16

G X P U P G X U Case 1 U is Red Just Recolor and move up 17

G P U S X X Case 2 Zig-Zag Double Rotate X around P; X around G Recolor G and X S P G U 18

G P U X S P Case 3 Zig-Zig Single Rotate P around G Recolor P and G X S G U 19

Asymptotic Cost of Insertion n O(lg n) to descend to insertion point n O(1) to do insertion n O(lg n) to ascend and readjust == worst case only for case 1 n Total: O(lg n) 20

Insert 4 into this R-B Tree 2 1 7 11 14 15 5 8 Black node Red node 21

Insertion Practice Insert the values 2, 1, 4, 5, 9, 3, 6, 7 into an initially empty Red-Black Tree 22

Top-Down Insertion An alternative to this bottom-up insertion is top-down insertion. Top-down is iterative. It moves down the tree, fixing things as it goes. What is the objective of top-down s fixes? 23

BOTTOM-UP DELETION 24

Recall ordinary BST Delete 1. If node to be deleted is a leaf, just delete it. 2. If node to be deleted has just one child, replace it with that child (splice) 3. If node to be deleted has two children, replace the value in the node by its in-order predecessor/successor s value then delete the in-order predecessor/successor (a recursive step) 25

Bottom-Up Deletion 1. Do ordinary BST deletion. Eventually a case 1 or case 2 deletion will be done (leaf or just one child). -- If deleted node, U, is a leaf, think of deletion as replacing U with the NULL pointer, V. -- If U had one child, V, think of deletion as replacing U with V. 2. What can go wrong?? 26

Which RB Property may be violated after deletion? 1. If U is Red? Not a problem no RB properties violated 2. If U is Black? If U is not the root, deleting it will change the black-height along some path 27

Fixing the problem n Think of V as having an extra unit of blackness. This extra blackness must be absorbed into the tree (by a red node), or propagated up to the root and out of the tree. n There are four cases our examples and rules assume that V is a left child. There are symmetric cases for V as a right child. 28

Terminology n The node just deleted was U n The node that replaces it is V, which has an extra unit of blackness n The parent of V is P n The sibling of V is S Black Node Red or Black and don t care Red Node 29

Bottom-Up Deletion Case 1 n V s sibling, S, is Red q Rotate S around P and recolor S & P n NOT a terminal case One of the other cases will now apply n All other cases apply when S is Black 30

Case 1 Diagram V+ P S Rotate S around P P S V+ S P Recolor S & P V+ 31

Bottom-Up Deletion Case 2 n V s sibling, S, is Black and has two Black children. q Recolor S to be Red q P absorbs V s extra blackness n n If P is Red, we re done (it absorbed the blackness) If P is Black, it now has extra blackness and problem has been propagated up the tree 32

Case 2 diagram P Recolor S P absorbs blackness P+ V+ S V S Either extra Black absorbed by P or P now has extra blackness 33

Bottom-Up Deletion Case 3 n S is Black n S s right child is RED (Left child either color) q Rotate S around P q Swap colors of S and P, and color S s right child Black n This is the terminal case we re done 34

Case 3 diagrams V+ P S Rotate S around P P S V+ S P V Swap colors of S & P Color S s right child Black 35

Bottom-Up Deletion Case 4 n S is Black, S s right child is Black and S s left child is Red q Rotate S s left child around S q Swap color of S and S s left child q Now in case 3 36

Case 4 Diagrams P V+ S P Rotate S s left around S V+ P V+ S S Swap colors of S and S s original left child 37

Top-Down Deletion An alternative to the recursive bottom-up deletion is top-down deletion. This method is iterative. It moves down the tree only, fixing things as it goes. What is the goal of top-down deletion? 38

65 50 80 10 60 70 90 62 Perform the following deletions, in the order specified Delete 90, Delete 80, Delete 70 39