Red-Black Trees.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Introduction to Algorithms Red-Black Trees
CMPT 225 Red–black trees. Red-black Tree Structure A red-black tree is a BST! Each node in a red-black tree has an extra color field which is red or black.
Red-Black Trees CIS 606 Spring Red-black trees A variation of binary search trees. Balanced: height is O(lg n), where n is the number of nodes.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 /26 Red-black tree properties Every node in a red-black tree is either black or red Every null leaf is black No path from a leaf to a root can have two.
1 Red-Black Trees. 2 Definition: A red-black tree is a binary search tree where: –Every node is either red or black. –Each NULL pointer is considered.
Red-Black Trees Lecture 10 Nawazish Naveed. Red-Black Trees (Intro) BSTs perform dynamic set operations such as SEARCH, INSERT, DELETE etc in O(h) time.
Red-Black Trees CS302 Data Structures Dr. George Bebis.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
CS-2851 Dr. Mark L. Hornick 1 Okasaki’s Insertion Method for Red/Black balancing A step-by-step procedure for maintaining balance through application of.
Lecture 10 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Lecture 2 Red-Black Trees. 8/3/2007 UMBC CSMC 341 Red-Black-Trees-1 2 Red-Black Trees Definition: A red-black tree is a binary search tree in which: 
1 Algorithms CSCI 235, Fall 2015 Lecture 25 Red Black Trees II.
Data StructuresData Structures Red Black Trees. Red-black trees: Overview Red-black trees are a variation of binary search trees to ensure that the tree.
Red-Black Tree Insertion Start with binary search insertion, coloring the new node red NIL l Insert 18 NIL l NIL l 1315 NIL l
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Red-Black Trees Bottom-Up Deletion. Recall “ordinary” BST Delete 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has just.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Lecture 23 Red Black Tree Chapter 10 of textbook
Definitions and Bottom-Up Insertion
File Organization and Processing Week 3
Balanced Search Trees Modified from authors’ slides.
Red-Black Trees v z Red-Black Trees Red-Black Trees
Red-Black Trees 5/17/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
Red-Black Trees 5/22/2018 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Design and Analysis of Algorithms
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees Motivations
CS200: Algorithms Analysis
Red-Black Trees Bottom-Up Deletion.
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees v z /20/2018 7:59 AM Red-Black Trees
Red-Black Trees v z Red-Black Trees Red-Black Trees
CMSC 341 (Data Structures)
Red Black Trees.
Lecture 9 Algorithm Analysis
Red-Black Trees Bottom-Up Deletion.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees.
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Analysis of Algorithms CS 477/677
Algorithms CSCI 235, Spring 2019 Lecture 24 Red Black Trees II
Red-black tree properties
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Properties of Red-black trees
Announcements Midterm will be given on 10/21/99
Chapter 12&13: Binary Search Trees (BSTs)
Red Black Trees Top-Down Deletion.
Red-Black Trees CS302 Data Structures
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Presentation transcript:

Red-Black Trees

Source: wikipedia.org

Red-Black Tree Properties (Definition of RB Trees) A red-black tree is a BST with following properties: Every node is either red or black. The root is black. Every leaf is NIL and black. Both children of each red node are black. All root-to-leaf paths contain the same number of black nodes.

More Properties No root-to-leaf path contains two consecutive red nodes. For each node x, all paths from x to descendant leaves contain the same number of black nodes. This number, not counting x, is the black height of x, denoted bh(x). No root-to-leaf path is more than twice as long as any other. Theorem. A red-black tree with n internal nodes has height < 2 log(n + 1).

Proof of Theorem

Source: wikipedia.org

Insert a node z Insert z as in a regular BST; color it red. If any violation to RB properties, fix it. Possible violations: The root is red. (Case 0) To fix up, make it black. Both z and z’s parent are red. To fix up, consider three cases. (Actually, six cases: I, II, III, I’, II’, III’)

Source: wikipedia.org

Insert Fixup: Case I The parent and “uncle” of z are both red: Color the parent and uncle of z black; Color the grandparent of z red; Repeat on the grandparent of z. new z z

Insert Fixup: Case II Case III z new z The parent of z is red, the uncle of z is black, z’s parent is a left child , z is a right child: Left Rotate on z’s parent; Make z’s left child the new z; it becomes Case III. z new z c a Case III c b a b

Insert Fixup: Case III The parent of z is Red and the “uncle” is Black, z is a left child, and its parent is a left child: Right Rotate on the grandparent of z. Switch colors of z’s parent and z’s sibling. Done! z z z c a c a c b b a b

Case II’ Symmetric to Case II z a z a c b b c Case II Case II’

Case III’ Symmetric to Case III z c c z a b a b Case III Case III’

Demonstration http://www.ece.uc.edu/~franco/C321/html/RedBlack/redblack.html

BST Deletion Revisited

Delete z If z has no children, we just remove it. If z has only one child, we splice out z. If z has two children, we splice out its successor y, and then replace z’s key and satellite data with y’s key and satellite data. Which physical node is deleted from the tree?

BST: Delete 44 Delete(32) 17 88 Has only one child: just splice out 32. 32 65 97 28 54 82 29 76 80 78 By Jim Anderson

BST: Delete 44 Delete(32) 17 88 28 65 97 29 54 82 76 80 78 By Jim Anderson

BST: Delete 44 Delete(65) 17 88 32 65 97 Has two children: Replace 65 by successor, 76, and splice out successor. Note: Successor can have at most one child. (Why?) 28 54 82 29 76 80 78 By Jim Anderson

BST: Delete 44 Delete(65) 17 88 32 76 97 28 54 82 29 80 78 By Jim Anderson

Source: wikipedia.org

Delete z Delete z as in a regular BST. If z had two (non-nil) children, when copying y’s key and satellite data to z, do not copy the color, (i.e., keep z’s color). Let y be the node being removed or spliced out. (Note: either y = z or y = successor(z) .) If y is red, no violation to the red-black properties. If y is black, then one or more violations may arise and we need to restore the red-black properties.

Let x denote the child of y before it was spliced out. x is either nil (leaf) or was the only non-nil child of y. y y x x

Restoring RB Properties Easy: If x is red, just change it to black. More involved: If x is black. y x x

Example: x is black x y w w x new x w w

Restoring RB Properties Assume: x is black and is a left child. The case where x is black and a right child is similar (symmetric). Four cases: x’ sibling w is red. x’s sibling w is black; both children of w are black. x’s sibling w is black; left child of w is red, right child black. x’s sibling w is black; right child of w is red.

Main idea Regard the pointer x itself as black. Counting x, the tree satisfies RB properties. Transform the tree and move x up until: x points to a red node, or x is the root, or RB properties are restored. At any time, maintain RB properties, with x counted as black.

x is a black left child: Case 1 x’ sibling w is red. Left rotate on B; change colors of B and D. Transform to Case 2, 3, or 4 (where w is black). B D w x A D B E E x C A new w C

x is a black left child : Case 2 x’s sibling w is black; both children of w are black. Move x up, and change w’s color to red. If new x is red, change it to black; else, repeat. new x B B w x A D D A C E C E

x is a black left child : Case 3 x’s sibling w is black; w’s left child is red, right child black. Right rotate on w (D); switch colors of C and D. C becomes the new w. Transform to Case 4. B B w x new w x A D A C C E s t D s t E

X is a black left child : Case 4 x’s sibling w is black; w’s right child is red. Left rotate on B; switch colors of B and D; change E to black. Done! B D w x A D B E C E A C