Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

Comp 122, Spring 2004 Binary Search Trees. btrees - 2 Comp 122, Spring 2004 Binary Trees  Recursive definition 1.An empty tree is a binary tree 2.A node.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  Implement red-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)
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
Balanced Trees Balanced trees have height O(lg n).
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.
David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.
Red-Black Trees Red-black trees: –Binary search trees augmented with node color –Operations designed to guarantee that the height h = O(lg n)
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
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: 
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
12.Binary Search Trees Hsu, Lih-Hsing. Computer Theory Lab. Chapter 12P What is a binary search tree? Binary-search property: Let x be a node in.
Binary Search Trees Lecture 6 Asst. Prof. Dr. İlker Kocabaş 1.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
1 CSC 211 Data Structures Lecture 25 Dr. Iftikhar Azim Niaz 1.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
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.
Binary Search Trees What is a binary search tree?
Balanced Search Trees Modified from authors’ slides.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Summary of General Binary search tree
Design and Analysis of Algorithms
Slide Sources: CLRS “Intro. To Algorithms” book website
Lecture 7 Algorithm Analysis
Red-Black Trees Motivations
Ch. 12: Binary Search Trees Ming-Te Chi
CS200: Algorithms Analysis
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees.
CMSC 341 (Data Structures)
Red Black Trees.
Lecture 9 Algorithm Analysis
Ch. 12: Binary Search Trees Ming-Te Chi
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Red-Black Trees.
Lecture 7 Algorithm Analysis
Algorithms and Data Structures Lecture VIII
CS6045: Advanced Algorithms
CS 583 Analysis of Algorithms
Lecture 7 Algorithm Analysis
Topic 6: Binary Search Tree Data structure Operations
Red-Black Trees.
Binary SearchTrees [CLRS] – Chap 12.
Design and Analysis of Algorithms
Analysis of Algorithms CS 477/677
Algorithms CSCI 235, Spring 2019 Lecture 24 Red Black Trees II
Properties of Red-black trees
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Algorithms CSCI 235, Spring 2019 Lecture 21 Binary Search Trees
Binhai Zhu Computer Science Department, Montana State University
Red-Black Trees CS302 Data Structures
Presentation transcript:

Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees Deleting a node from a Binary Search Tree Case 1: The node to be deleted has no children. Just delete the node. Case 2: The node to be deleted has one child. Splice out node by making link from its parent to its child. Case 3: The node to be deleted has two children. Replace node's data with data from its successor. Splice out the successor (which has no left child). 10 6 4 5 7 8 12 14 Example: Case 1: Remove 5 Case 2: Remove 8 Case 3: Remove 10 Running time?

Tree-Delete Pseudocode Tree-Delete(T, z) if z.left == NIL or z.right == NIL y = z //y is node to be deleted else y = Tree-Successor(z) if y.left != NIL x = y.left //x is node we splice to x = y.right if x != NIL x.p = y.p //bypass y if y.p == NIL //If node to be deleted (y) was root T.root = x //Set new root else if y == (y.p).left //If y was on left branch of parent (y.p).left = x //attach x to left branch of parent (y.p).right = x if y != z //We spliced out successor z.key = y.key //Copy y's data into z return y //return spliced-out node

Problem with Binary Search Trees We saw that the search, minimum, maximum, successor, predecessor, insert and delete functions all run in O(h) time for binary search trees (where h is the height of the tree). Problem: Sometimes the height of the search tree is n: 3 In this case, the running time for the tree is no better than for a linked list. 5 8 Solution: Find a way to guarantee that the binary search tree is more balanced. 10 13

Red-Black Trees Red-black trees are binary search trees with an added piece of information at each node: the color. Each node in a red-black tree has information on: color, key, left, right, and p. If a node does not have a child, the pointer will point to an external node, T.nil, which is a leaf on the tree. The root's p field also points to T.nil All normal nodes (with a key value and pointers) are internal nodes of the tree.

Properties of Red-black trees Every node is either red or black The root is black Every leaf NIL is black If a node is red, both children are black For each node, all paths from that node to the descendent leaves contain the same number of black nodes. 26 Usually omitted when drawing trees 17 41 14 21 30 NIL NIL NIL NIL NIL NIL NIL

Black Height The black height, bh(x) of a node, x, is the number of black nodes on a path from (but not including) x down to the leaf. A leaf (NIL) has bh = 0 We can show that the height of a red-black tree with n internal nodes is at most: 2lg(n+1)

Height of Red-Black tree To show that a red-black tree with n nodes has height <= 2lg(n+1), we show the following: Show, by induction, that a subtree rooted at x has at least 2bh(x)-1 internal nodes. At least half the nodes from root to leaf must be black nodes. Therefore bh >= h/2 We will work out the rest in class.

Proof by induction To prove part a) by induction: Show that if height of x, h(x), is zero, a) is true. If height of x is zero, then x is a leaf and bh(x) = 0 2bh(x)-1 = 20-1 = 0. Therefore a) is true (sub tree has at least 0 internal nodes). 2) Assume show that if a) holds for tree of height h(x) -1 then it also holds for tree of the height of h(x). Assume x is internal node with height > 0 and with 2 children. Each child has black height = bh(x) OR black height = bh(x) -1 Part a) holds for both children, so each child has at least 2bh(x)-1-1 nodes. Therefore, tree rooted at x has at least: (2bh(x)-1 - 1) + (2bh(x)-1-1) + 1 = 2(2bh(x)-1) -1 = 2bh(x)-1 Therefore part a) holds for node x.

Running time of Red-black tree functions We can use the same search, minimum, maximum, successor and predecessor functions that we used for binary search trees. These ran in O(h) time. Since h<= 2lg(n+1) for red-black trees, all of the above functions will run in O(lgn) time. We can run Tree-insert and Tree-delete on red-black trees, but they may not maintain the red-black tree properties. Next lecture we will examine new insert and delete functions for red-black trees that do maintain the red-black tree properties.

Rotations To insert and delete items in Red-Black trees while maintaining all the Red-Black Tree properties, we must be able to change node colors and sometimes we need to re-arrange the structure of the trees. We change the pointer structure through Rotations. A rotation exchanges the level of 2 adjacent nodes (parent and child) and rearranges pointers to maintain the binary search tree property.

Left Rotation y x Left Rotation a, b, g, are subtrees. a g x y Right Rotation a b g b x is a node whose right child, y, is not T.nil We exchange the levels of x and y x becomes y's left subtree (Check: is x < y?) x's left subtree remains x's left subtree y's left subtree becomes x's right subtree (Check: are all b >x and b<y?) y's right subtree remain's y's subtree. Left Rotation Right rotation is symmetric to Left Rotation.

Pseudocode for Left Rotation Left-Rotate(T, x) y = x.right //Set y to be x's right child x.right = y.left //x's right subtree assigned y's left subtree if y.left != T.nil (y.left).p = x //x becomes parent of y's left subtree y.p = x.p //y's parent is assigned x's parent if x.p == T.nil //if x was root, set y to root T.root = y else if x == (x.p).left //if x was left subchild, now y is left subchild (x.p).left = y else (y.p).right = y //Otherwise, y is now right subchild y.left = x //x is now left child of y x.p =y //y is parent of x //Right-Rotate(T, x) is similar //Running time?