Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
AA Trees another alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A balanced.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 10.
2IL50 Data Structures Spring 2015 Lecture 8: Augmenting Data Structures.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 12.
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.
Balanced Search Trees CS Data Structures Mehmet H Gunes Modified from authors’ slides.
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 /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 /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.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
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 CS302 Data Structures Dr. George Bebis.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms 6.046J/18.401J LECTURE 10 Balanced Search.
Analysis of Red-Black Tree Because of the rules of the Red-Black tree, its height is at most 2log(N + 1). Meaning that it is a balanced tree Time Analysis:
Introduction to Algorithms Jiafen Liu Sept
Red-Black Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
October 16, Algorithms and Data Structures Lecture IX Simonas Šaltenis Aalborg University
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
Red-Black Trees Comp 550.
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 Balanced Trees There are several ways to define balance Examples: –Force the subtrees of each node to have almost equal heights –Place upper and lower.
Red-Black trees Binary search trees with additional conditions. These conditions ensure that the trees are fairly well balanced. In this way we obtain.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Red Black Trees. History The concept of a balancing tree was first invented by Adel’son-Vel’skii and Landis in They came up with the AVL tree. In.
CS 473Lecture X1 CS473-Algorithms Lecture RBT - DELETION.
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)
Sept Red-Black Trees What is a red-black tree? -node color: red or black - nil[T] and black height Subtree rotation Node insertion Node deletion.
David Luebke 1 3/20/2016 CS 332: Algorithms Skip Lists.
Red-Black Trees an alternative to AVL trees. Balanced Binary Search Trees A Binary Search Tree (BST) of N nodes is balanced if height is in O(log N) A.
CSC317 1 x y γ β α x y γ β x β What did we leave untouched? α y x β.
Balanced Search Trees Modified from authors’ slides.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Red-Black Trees.
Red-Black Trees.
Summary of General Binary search tree
Red-Black Trees Motivations
CS200: Algorithms Analysis
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
CMSC 341 (Data Structures)
Lecture 9 Algorithm Analysis
Red-Black Trees Bottom-Up Deletion.
Lecture 9 Algorithm Analysis
Lecture 9 Algorithm Analysis
Algorithms and Data Structures Lecture VIII
CS 583 Analysis of Algorithms
Red-Black Trees.
Analysis of Algorithms CS 477/677
Red-black tree properties
Announcements Midterm will be given on 10/21/99
Chapter 12&13: Binary Search Trees (BSTs)
Red-Black Trees CS302 Data Structures
Presentation transcript:

Lecture 12: Balanced Binary Search Trees Shang-Hua Teng

Insertion and Deletion on dynamic sets Insert(S,x) –A modifying operation that augments the set S with the element pointed by x Delete –Given a pointer x to an element in the set S, removes x from S –Notice that this operation uses a pointer to an element x, not a key value

Querying on dynamic sets Search(S,k) –given a set S and a key value k, returns a pointer x to an element in S such that key[x] = k, or NIL if no such element belongs S Minimum(S) –on a totally ordered set S that returns a pointer to the element S with the smallest key Maximum(S) Successor(S,x) –Given an element x whose key is from a totally ordered set S, returns a pointer to the next larger element in S, or NIL if x is the maximum element Predecessor(S,x)

Binary Search Trees All operations can be supported in – O(h) time, where h = height of tree What is the height of a binary search tree? –worst case: h = O(n) when tree is just a linear string of left or right children Today we’ll see how to maintain h = O(lg n)

Restructuring Binary Search Trees: Rotations Our basic operation for changing tree structure is called rotation: Preserves binary search tree properties O(1) time…just changes some pointers C rightRotate(y) leftRotate(x) y x AB x A y BC

Balanced Binary Search Tree Apply rotations during insertion and deletion to ensure that the height of the tree is O(lg n). Define a balanced condition for simple maintenance. Example: for each internal node, the heights of the left and right subtree differs by at most 1 –AVL trees (homework) We will discuss a scheme that uses color to balance the tree –Red-black trees

Red-Black Tree: Coloring nodes with red and black

Properties of Red-Black Trees A Red-Black tree satisfies the following properties: 1Every node is colored either red or black 2The root is black 3Every leaf (NIL, NULL) is black 4If a node is red, both of its children are black. 5Every path from a node to a leaf reference has the same number of black nodes

Red-Black Tree

Height of Red-Black trees If every path from the root to a null reference contains B black nodes, then there must be at least 2 B - 1 black nodes in the tree. Since the root is black and there cannot be two consecutive red nodes on a path, the height of a red-black tree is at most 2log(N + 1)

RB Trees: Worst-Case Time So we’ve proved that a red-black tree has O(lg n) height Corollary: These operations take O(lg n) time: –Minimum(), Maximum() –Successor(), Predecessor() –Search() Insert() and Delete(): –Will also take O(lg n) time –But will need special care since they modify tree

Red-black trees -- Insert 1.Find the location for the target node, x. 2.Insert x and color it red. Why red? Maintain the black height property. Potential problem: the parent of x is also red! Method: move this violation up the tree while always maintaining the black height property. Perform rotations and re-colorings as necessary

RedBlackTreeInsert(z) treeInsert(z); x->color = RED; // Move red-red up tree, maintaining black height as invariant: while (z!=root and p(z)->color == RED) if (p(z) == p(p(z))->left) y = p(p(z))->right; if (y->color == RED) p(z)->color = BLACK; y->color = BLACK; p(p(z))->color = RED; x = p(p(z)); else // y->color == BLACK if (z = p(z)->right) z = p(z); leftRotate(z); p(z)->color = BLACK; p(p(z))->color = RED; rightRotate(p(p(z))); else // x->p == p(p(z))->right (same as above, but with “right” & “left” exchanged) Case 1: uncle is RED Case 2 Case 3

Insert: Case 1 if (y->color == RED) p(z)->color = BLACK; y->color = BLACK; p(p(z))->color = RED; z= p(p(z)); Case 1: “uncle” is red In figures below, all  ’s are equal-black-height subtrees C A D  B   z y  C A D  B  new z Change colors of some nodes, preserving: all downward paths have equal b.h. The while loop now continues with z’s grandparent as the new z case 1

Insert: Case 2 if (z == p(z)->right) z = p(z); leftRotate(z); // continue with case 3 code Case 2: –“Uncle” is black –Node z is a right child Transform to case 3 via a left-rotation case 2 B  z C A  y  C B A  z  y  Transform case 2 into case 3 (z is left child) with a left rotation This preserves: all downward paths contain same number of black nodes

Insert: Case 3 p(z)->color = BLACK; P(p(z))->color = RED; rightRotate(p(p(z))); Case 3: –“Uncle” is black –Node z is a left child Change colors; rotate right case 3 y C B A  z   B A z  C  Perform some color changes and do a right rotation Again, preserves: all downward paths contain same number of black nodes

Red-black trees -- Insert What we described is what happens when p[z] is a left child. The case when it is a right child is symmetrical. Note: –In case 3 we fixed the problem by a single rotation (single or double). –In case 1 we recolored the nodes and the problem may have propagated upwards. –In any case, we need at most one restructuring of the tree at the level. Total insert time : O(lgn)

Red-black trees -- Delete Let z be the node that we want to delete. Let y be the actual node that we delete. –if z has at most one non-NIL child, then y is z –if z has two children, then y is z's successor Let x be the child of y. –if y is z's successor, x is its right child (possibly a NIL child) –if y is z, x is its non-NIL child, or NIL

RB-Delete The code is similar to the delete operation for a BST References to nil are replaced with the sentinnel nil[T] This allows the assignment in line 7 to be unconditional since the nil is like other nodes If the node y that is removed is black, the fixup routine restores the black height of the tree

Red-black trees -- Delete When y is eliminated, x becomes the new child of y's parent. If y was red, no properties are violated. If y was black, we need to restructure the tree Idea! Let's transfer y's blackness to x –if x was red, it becomes black. all properties are now satisfied –if x was black, it becomes doubly black. the red-or-black property is violated!

Red-black trees -- Delete To solve the double-black problem: –propagate the extra "blackness" up the tree until we find a red node –then, just make it black -- OR -- we reach the root –then, just eliminate the extra black -- OR -- the problem gets fixed by rotating

Parameter x was the removed node’s sole child (may be nil) which has a double black count The loop moves x with its double count up the tree until: –x points to a red node which is colored black –x points to the root and the extra black count is discarded –rotations and recoloring can solve the problem

Red-black trees -- Delete There are four cases we need to consider, some of which reduce to others. In all of these cases, the next action depends on the color of x’s sibling, w, and its children.

Red-black trees -- Delete Case 1 w is red => it has black children switch colors of w, p[w] and left-rotate p[w] Result: one of the other two cases x w a b c d e x new w a b c d e

Red-black trees -- Delete Case 2 w is black with black children Take one black off x and w and add an extra to p[x]. Repeat the whole procedure for p[x] as the new x x w a b c d e for nodes that may be either red or black a b c d e new x

Red-black trees -- Delete Case 3 w is black, left[w] is red, right[w] is black Double rotate and recolor x w a b c d e a b c d e

Red-black trees -- Delete Case 4 w is black, right[w] is black (we don’t care about left[w]) color[w] = color1 color[b] = color[e] = black left-rotate at b AND WE ARE DONE! x w a b c d e color1 color2 b d color1 c e color2a

Red-black trees -- Delete x w a b c d e a b c d e new w Case 3 Notes The first half of the rotation in case 3 is a single rotation that actually results is the situation covered by case 4. Here is exactly what happens in this intermediate step: w is black, left[w] is red, right[w] is black Switch colors of w, left[w] and right rotate w

Red-black trees -- Delete Running time of "fixing" algorithm: –Cases 1, 3 terminate after some constant color changes and at most three rotations. –Case 2 may cause us to travel all the way to the root : O(lgn) –Total running time for fixing algorithm: O(lgn) Total time for Delete : O(lgn) –even if the fixing algorithm is not called, it will take O(lgn) if it needs to find the successor.