Data Structures and Algorithms

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
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.
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.
CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.
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.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
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.
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:
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
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 Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees.
Data Structures and Algorithms TREE-TRAVERSAL. Searching - Re-visited Binary tree O(log n) if it stays balanced Simple binary tree good for static collections.
2-3 Tree. Slide 2 Outline  Balanced Search Trees 2-3 Trees Trees.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
CS 473Lecture X1 CS473-Algorithms Lecture RBT - INSERTION.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
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)
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Data Structures and Algorithms (AT70.02) Comp. Sc. and Inf. Mgmt. Asian Institute of Technology Instructor: Prof. Sumanta Guha Slide Sources: CLRS “Intro.
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
Data Structures Balanced Trees CSCI 2720 Spring 2007.
AA Trees.
Balanced Search Trees Modified from authors’ slides.
G64ADS Advanced Data Structures
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.
Data Structures and Algorithms
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
Binary Search Tree (BST)
CSCI Trees and Red/Black Trees
Red-Black Trees.
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Design and Analysis of Algorithms
Slide Sources: CLRS “Intro. To Algorithms” book website
Red-Black Trees Motivations
Data Structures Balanced Trees CSCI
Slide Sources: CLRS “Intro. To Algorithms” book website
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
(2,4) Trees /26/2018 3:48 PM (2,4) Trees (2,4) 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.
(2,4) Trees (2,4) Trees (2,4) Trees.
Algorithms and Data Structures Lecture VIII
2-3-4 Trees Red-Black Trees
Red-Black Trees v z /17/2019 4:20 PM Red-Black Trees
(2,4) Trees 2/15/2019 (2,4) Trees (2,4) Trees.
(2,4) Trees /24/2019 7:30 PM (2,4) Trees (2,4) Trees
(2,4) Trees (2,4) Trees (2,4) Trees.
Red-Black Trees.
CSE2331/5331 Topic 7: Balanced search trees Rotate operation
(2,4) Trees /6/ :26 AM (2,4) Trees (2,4) Trees
Red Black Trees Top-Down Deletion.
CO4301 – Advanced Games Development Week 5 Walkthrough of Red-Black Tree Insertion Gareth Bellaby.
Red-Black Trees v z /6/ :10 PM Red-Black Trees
Presentation transcript:

Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees PLSD210

Searching - Re-visited Binary tree O(log n) if it stays balanced Simple binary tree good for static collections Low (preferably zero) frequency of insertions/deletions but my collection keeps changing! It’s dynamic Need to keep the tree balanced First, examine some basic tree operations Useful in several ways!

Tree Traversal ¬ ® ­ ¯ ° Traversal = visiting every node of a tree Three basic alternatives Pre-order Root Left sub-tree Right sub-tree ¬ ® ­ ¯ ° x A + x + B C x D E F L R L L R

Tree Traversal ­ µ ¬ ± ¯ ³ ® ² ´ ° Traversal = visiting every node of a tree Three basic alternatives In-order Left sub-tree Root Right sub-tree ­ µ ¬ ± 11 ¯ ³ A x B + C x D x E + F ® ² ´ ° L L R

Tree Traversal µ ¬ ³ ´ ¯ ² ­ ° ± ® Traversal = visiting every node of a tree Three basic alternatives Post-order Left sub-tree Right sub-tree Root 11 µ ¬ ³ ´ ¯ ² A B C + D E x x F + x ­ ° ± ® L L R

Tree Traversal µ ¬ ³ ´ ¯ ² ­ ° ± ® Post-order Reverse-Polish Left sub-tree Right sub-tree Root Reverse-Polish Normal algebraic form which traversal? 11 µ ¬ ³ ´ ¯ ² (A (((BC+)(DEx) x) F +)x ) ­ ° ± ® (A x(((B+C)(DxE))+F))

Trees - Searching Binary search tree Produces a sorted list by in-order traversal In order: A D E G H K L M N O P T V

Trees - Searching Binary search tree Preserving the order Observe that this transformation preserves the search tree

Trees - Searching Binary search tree Preserving the order Observe that this transformation preserves the search tree We’ve performed a rotation of the sub-tree about the T and O nodes

Trees - Rotations Binary search tree A x B y C Rotations can be either left- or right-rotations For both trees: the inorder traversal is A x B y C

Trees - Rotations Binary search tree Rotations can be either left- or right-rotations Note that in this rotation, it was necessary to move B from the right child of x to the left child of y

Trees - Red-Black Trees A Red-Black Tree Binary search tree Each node is “coloured” red or black An ordinary binary search tree with node colourings to make a red-black tree

Trees - Red-Black Trees A Red-Black Tree Every node is RED or BLACK Every leaf is BLACK When you examine rb-tree code, you will see sentinel nodes (black) added as the leaves. They contain no data. Sentinel nodes (black)

Trees - Red-Black Trees A Red-Black Tree Every node is RED or BLACK Every leaf is BLACK If a node is RED, then both children are BLACK This implies that no path may have two adjacent RED nodes. (But any number of BLACK nodes may be adjacent.)

Trees - Red-Black Trees A Red-Black Tree Every node is RED or BLACK Every leaf is BLACK If a node is RED, then both children are BLACK Every path from a node to a leaf contains the same number of BLACK nodes From the root, there are 3 BLACK nodes on every path

Trees - Red-Black Trees A Red-Black Tree Every node is RED or BLACK Every leaf is BLACK If a node is RED, then both children are BLACK Every path from a node to a leaf contains the same number of BLACK nodes The length of this path is the black height of the tree

Trees - Red-Black Trees Lemma A RB-Tree with n nodes has height £ 2 log(n+1) Proof .. See Cormen Essentially, height £ 2 black height Search time O( log n )

Trees - Red-Black Trees Data structure As we’ll see, nodes in red-black trees need to know their parents, so we need this data structure struct t_red_black_node { enum { red, black } colour; void *item; struct t_red_black_node *left, *right, *parent; } Same as a binary tree with these two attributes added

Trees - Insertion Insertion of a new node 4 x Requires a re-balance of the tree rb_insert( Tree T, node x ) { /* Insert in the tree in the usual way */ tree_insert( T, x ); /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; Insert node 4 Mark it red Label the current node x

While we haven’t reached the root Trees - Insertion rb_insert( Tree T, node x ) { /* Insert in the tree in the usual way */ tree_insert( T, x ); /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; While we haven’t reached the root and x’s parent is red x->parent

If x is to the left of it’s granparent Trees - Insertion rb_insert( Tree T, node x ) { /* Insert in the tree in the usual way */ tree_insert( T, x ); /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; If x is to the left of it’s granparent x->parent->parent x->parent

Trees - Insertion /* Now restore the red-black property */ x->colour = red; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; y is x’s right uncle x->parent->parent x->parent right “uncle”

Trees - Insertion y = x->parent->parent->right; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; If the uncle is red, change the colours of y, the grand-parent and the parent x->parent->parent x->parent right “uncle”

Trees - Insertion y = x->parent->parent->right; while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent;

Trees - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; x’s parent is a left again, mark x’s uncle but the uncle is black this time New x

Trees - Insertion /* y is a black node */ while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); .. but the uncle is black this time and x is to the right of it’s parent

Trees - Insertion /* y is a black node */ while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); .. So move x up and rotate about x as root ...

Trees - Insertion /* y is a black node */ while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x );

.. but x’s parent is still red ... Trees - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); .. but x’s parent is still red ...

.. and x is to the left of its parent Trees - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); .. The uncle is black .. uncle .. and x is to the left of its parent

.. So we have the final case .. Trees - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); else { /* case 3 */ right_rotate( T, x->parent->parent ); } .. So we have the final case ..

Trees - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); else { /* case 3 */ right_rotate( T, x->parent->parent ); } .. Change colours and rotate ..

Trees - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); else { /* case 3 */ right_rotate( T, x->parent->parent ); }

This is now a red-black tree .. Trees - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); else { /* case 3 */ right_rotate( T, x->parent->parent ); } This is now a red-black tree .. So we’re finished!

Trees - Insertion while ( (x != T->root) && (x->parent->colour == red) ) { if ( x->parent == x->parent->parent->left ) { /* If x's parent is a left, y is x's right 'uncle' */ y = x->parent->parent->right; if ( y->colour == red ) { /* case 1 - change the colours */ x->parent->colour = black; y->colour = black; x->parent->parent->colour = red; /* Move x up the tree */ x = x->parent->parent; else { /* y is a black node */ if ( x == x->parent->right ) { /* and x is to the right */ /* case 2 - move x up and rotate */ x = x->parent; left_rotate( T, x ); else { /* case 3 */ right_rotate( T, x->parent->parent ); } else .... There’s an equivalent set of cases when the parent is to the right of the grandparent!

Red-black trees - Analysis Addition Insertion Comparisons O(log n) Fix-up At every stage, x moves up the tree at least one level O(log n) Overall O(log n) Deletion Also O(log n) More complex ... but gives O(log n) behaviour in dynamic cases

Red Black Trees - What you need to know? Code? This is not a course for masochists! You can find it in a text-book You need to know The algorithm exists What it’s called When to use it ie what problem does it solve? Its complexity Basically how it works Where to find an implementation How to transform it to your application