November 5, 20011 Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University

Slides:



Advertisements
Similar presentations
David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Advertisements

Introduction to Algorithms Red-Black Trees
Topic 23 Red Black Trees "People in every direction No words exchanged No time to exchange And all the little ants are marching Red and black antennas.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture.
Balanced Binary Search Trees
A balanced life is a prefect life.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
CSC 213 Lecture 9: Red-Black Trees. Announcements Reminder: Daily Quizzes should take 15 minutes Goal is to provide chance to see if you really understand.
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.
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Fall 2007CS 2251 Self-Balancing Search Trees Chapter 9.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Multi-Way search Trees Trees: a. Nodes may contain 1 or 2 items. b. A node with k items has k + 1 children c. All leaves are on same level.
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Advanced Trees Part III Briana B. Morrison Adapted from Alan Eugenio & William J. Collins.
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 CS302 Data Structures Dr. George Bebis.
Course: Programming II - Abstract Data Types Red-Black TreesSlide Number 1 Balanced Search Trees Binary Search Tree data structures can allow insertion,
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:
10/20/2015 2:03 PMRed-Black Trees v z. 10/20/2015 2:03 PMRed-Black Trees2 Outline and Reading From (2,4) trees to red-black trees (§9.5) Red-black.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Introduction to Algorithms Jiafen Liu Sept
October 16, Algorithms and Data Structures Lecture IX Simonas Šaltenis Aalborg University
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Mudasser Naseer 1 10/20/2015 CSC 201: Design and Analysis of Algorithms Lecture # 11 Red-Black Trees.
© 2004 Goodrich, Tamassia Red-Black Trees v z.
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
1. 2 Setting Up Deletion As with binary search trees, we can always delete a node that has at least one external child If the key to be deleted is stored.
CSIT 402 Data Structures II
Beyond (2,4) Trees What do we know about (2,4)Trees? Balanced
Red-Black Trees Acknowledgment Many thanks to “erm” from Purdue University for this very interesting way of presenting this course material. 1.
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
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: 
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Lecture 11COMPSCI.220.FS.T Balancing an AVLTree Two mirror-symmetric pairs of cases to rebalance the tree if after the insertion of a new key to.
CSC 213 – Large Scale Programming Lecture 21: Red-Black Trees.
3.1. Binary Search Trees   . Ordered Dictionaries Keys are assumed to come from a total order. Old operations: insert, delete, find, …
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
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)
1 Binary Search Trees   . 2 Ordered Dictionaries Keys are assumed to come from a total order. New operations: closestKeyBefore(k) closestElemBefore(k)
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
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.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
File Organization and Processing Week 3
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.
Red Black Trees
Lecture 17 Red-Black Trees
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Summary of General Binary search tree
Design and Analysis of Algorithms
TCSS 342, Winter 2006 Lecture Notes
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
Red-Black Trees.
Algorithms and Data Structures Lecture VIII
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Binary Search Trees < > = Dictionaries
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Presentation transcript:

November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University

November 5, This Lecture Red-Black Trees Properties Rotations Insertion Deletion

November 5, Balanced Binary Search Trees Problem: worst-case execution time for dynamic set operations is  (n) Solution: balanced search trees guarantee small (logarithmic) height!

November 5, Red/Black Trees A red-black tree is a binary search tree with the following properties: edges (nodes) are colored red or black edges connecting leaves are black no two consecutive red edges on any root-leaf path same number of black edges on any root-leaf path (= black height of the tree)

November 5, More RB-Tree Poperties Some measures n – # of internal nodes l – # leaves  n + 1) h – height bh – black height Property 1:

November 5, Property 2: Property 3: Operations in the binary-search tree (search, insert, delete,...) can be accomplished in O(h) time Consequently, the RB-tree is a binary search tree, whose height is bound by 2 log(n+1), the operations run in O(log n) More RB-Tree Poperties (2)

November 5, Rotation right rotation left rotation

November 5, Rotation (2) The basic operation for maintaining balanced trees Maintains inorder key ordering After left rotation (right rotation has the opposite effect) Depth(  ) decreases by 1 Depth(  ) stays the same Depth(  ) increases by 1 Rotation takes O(1) time

November 5, Insertion in the RB-Trees Perform a standard search to find the leaf Replace the leaf with an internal node with the new key. The new node has two children (leaves) with black incoming edges Color the incoming edge of the new node red If the parent has an incoming red edge, we have two consecutive red edges! We must re- organize a tree to remove that violation.

November 5, Insertion, Plain and Simple Let n … the new node p … its parent g … its grandparent Case 0: Incoming edge of p is black Insertion algorithm stops here. No further manipulation of the tree necessary!

November 5, Insertion: Case 1 Case 1: Incoming edge of p is red and its sibling (uncle of n) has a red incoming edge a tree rooted at g is balanced enough

November 5, Insertion: Case 1 (2) We call this a promotion Note how the black depth remains unchanged for all of the descendants of g This process will continue upward beyond g if necessary; rename g as n and repeat

November 5, Insertion: Case 3 Case 3: Incoming edge of p is red, its sibling is black and n is left child of p We do a “right rotation” No further work on tree is necessary Inorder remains unchanged Tree becomes more balanced No two consecutive red edges!

November 5, Insertion: Cases 2 Case 2: Incoming edge of p is red, its sibling is black and n is right child of p We must perform a “double rotation” – left rotation around p (we end in case 3) followed by right rotation around n

November 5, Insertion: Mirror cases All three cases are handled analogously if p is a right child For example, case 2 and 3 – right-left double rotation

November 5, Insertion: Pseudo Code Case 1 Case 2 Case 3

November 5, Insertion Summary If two red edges are present, we do either a restructuring (with a simple or double rotation) and stop (cases 2 and 3), or a promotion and continue (case 1) A restructuring takes constant time and is performed at most once. It reorganizes an off- balanced section of the tree Promotions may continue up the tree and are executed O(log n) times (height of the tree) The running time of an insertion is O(log n)

November 5, Inserting "REDSOX" into an empty tree Now, let us insert "CUBS" An Insertion Example

November 5, Example C

November 5, Example U

November 5, Example U (2) Double Rotation

November 5, Example B What now?

November 5, Example B (2) Right Rotation on D

November 5, Example S two red edges and red uncle X- promotion could have placed it on either side

November 5, Example S (2) again two red edges - rotation

November 5, Deletion As with binary search trees, we can always delete a node that has at least one external child If the key to be deleted is stored at a node that has no external children, we move there the key of its inorder predecessor (or successor), and delete that node instead Example: to delete key 7, we move key 5 to node u, and delete node v

November 5, Deletion Algorithm 1. Remove v 2. If v was red, we are done. Else, color u double black.

November 5, Deletion Algorithm (2) How to eliminate double black edges? The intuitive idea is to perform a "color compensation" Find a red edge nearby, and change the pair (red, double black) into (black, black) As for insertion, we have two cases: restructuring, and recoloring Restructuring resolves the problem locally, while recoloring may propagate it two levels up Slightly more complicated than insertion

November 5, Deletion Case 2 If sibling and its children are black, perform a recoloring If parent becomes double black, continue upward

November 5, Deletion: Cases 3 and 4 If sibling is black and one of its children is red, perform a restructuring

November 5, Deletion Case 1 If sibling is red, perform a rotation to get into one of cases 2, 3, and 4 If the next case is 2 (recoloring), there is no propagation upward (parent is now red)

November 5, A Deletion Example Delete 9

November 5, A Deletion Example (2) Case 2 (sibling is black with black children) – recoloring

November 5, A Deletion Example (3) Delete 8: no double black

November 5, A Deletion Example (4) Delete 7: restructuring

November 5, How long does it take? Deletion in a RB-tree takes O(log n) Maximum three rotations and O(log n) recolorings

November 5, Red-Black trees are related to trees (non-binary trees) Other balanced trees AVL-trees have simpler algorithms, but may perform a lot of rotations

November 5, Next Week External storage search trees: B-Trees