1 Data Structures and Algorithms Searching Red-Black and Other Dynamically BalancedTrees.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
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.
1 AVL Trees. 2 AVL Tree AVL trees are balanced. An AVL Tree is a binary search tree such that for every internal node v of T, the heights of the children.
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.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  Implement red-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.
Balanced Binary Search Trees
CSE 2331/5331 Topic 10: Balanced search trees Rotate operation Red-black tree Augmenting data struct.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
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.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
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 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
Self-Balancing Search Trees Chapter 11. Chapter 11: Self-Balancing Search Trees2 Chapter Objectives To understand the impact that balance has on the performance.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
Chapter 13 Binary Search Trees. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Define a binary search tree abstract.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 9 Balanced trees Motivation Red-black trees –Definition, Height –Rotations, Insert,
CSC 212 Lecture 19: Splay Trees, (2,4) Trees, and Red-Black Trees.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Balanced Search Trees CS 3110 Fall Some Search Structures Sorted Arrays –Advantages Search in O(log n) time (binary search) –Disadvantages Need.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 10: Binary Search Trees Java Software Structures: Designing.
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:
Red Black Tree Smt Genap Outline Red-Black Trees ◦ Motivation ◦ Definition ◦ Operation Smt Genap
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.
CSIT 402 Data Structures II
Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
CSCE 3110 Data Structures & Algorithm Analysis AVL Trees Reading: Chap. 4, Weiss.
1 Red-Black Trees By Mary Hudachek-Buswell Red Black Tree Properties Rotate Red Black Trees Insertion Red Black Trees.
Data Structures Balanced Trees 1CSCI Outline  Balanced Search Trees 2-3 Trees Trees Red-Black Trees 2CSCI 3110.
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: 
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.
Red-Black trees Binary search trees with additional conditions. These conditions ensure that the trees are fairly well balanced. In this way we obtain.
Red–black trees.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
Fall 2006 CSC311: Data Structures 1 Chapter 10: Search Trees Objectives: Binary Search Trees: Search, update, and implementation AVL Trees: Properties.
Red-Black Trees Definitions and Bottom-Up Insertion.
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
1 Binary Search Trees  Average case and worst case Big O for –insertion –deletion –access  Balance is important. Unbalanced trees give worse than log.
Keeping Binary Trees Sorted. Search trees Searching a binary tree is easy; it’s just a preorder traversal public BinaryTree findNode(BinaryTree node,
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 β.
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
AA Trees.
BCA-II Data Structure Using C
G64ADS Advanced Data Structures
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
Red Black Trees
Binary Search Trees (Continued)
Red-Black Trees.
AVL Tree.
Red-Black Trees Motivations
TCSS 342, Winter 2006 Lecture Notes
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Data Structures and Algorithms
Algorithms and Data Structures Lecture VIII
Binary Search Trees.
2-3-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.
Presentation transcript:

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

2 Red/Black Trees Red/Black trees provide another alternative implementation of balanced binary search trees A red/black tree is a balanced binary search tree where each node stores a color (usually implemented as a boolean) The following rules govern the color of a node: The root is black All children of a red node are black Every path from the root to a leaf contains the same number of black nodes

3 FIGURE Valid red/black trees

4 Insertion into Red/Black Trees The color of a new element is set to red Once the new element has been inserted, the tree is rebalanced/recolored as needed to to maintain the properties of a red/black tree This process is iterative beginning at the point of insertion and working up the tree toward the root The process terminates when we reach the root or when the parent of the current element is black

5 Insertion into Red/Black Trees In each iteration of the rebalancing process, we will focus on the color of the sibling of the parent of the current node There are two possibilities for the parent of the current node: The parent could be a left child The parent could be a right child The color of a null node is considered to be black

6 Insertion into Red/Black Trees In the case where the parent of the current node is a right child, there are two cases Leftaunt.color == red Leftaunt.color == black If leftaunt.color is red then the processing steps are:

7 FIGURE Red/black tree after insertion

8 Insertion into Red/Black Trees If leftaunt.color is black, we first must check to see if current is a left child or a right child If current is is a left child, then we must set current equal to current.parent and then rotate current.left to the right The we continue as if current were a right child to begin with:

9 Insertion into Red/Black Trees In the case where the parent of current is a left child, there are two cases: either rightuncle.color == red or rightuncle.color == black If rightuncle.color == red then the processing steps are:

10 FIGURE Red/black tree after insertion

11 Insertion into Red/Black Trees If rightuncle.color == black then we first need to check to see if current is a left or right child If current is a right child then we set current equal to current.parent then rotate current.right or the left around current We then continue as if current was left child to begin with:

12 Element Removal from Red/Black Trees As with insertion, the tree will need to be rebalanced/recolored after the removal of an element Again, the process is an iterative one beginning at the point of removal and continuing up the tree toward the root This process terminates when we reach the root or when current.color == red

13 Element Removal from Red/Black Trees Like insertion, the cases for removal are symmetrical depending upon whether current is a left or right child In insertion, we focused on the color of the sibling of the parent In removal, we focus on the color of the sibling of current keeping in mind that a null node is considered to be black

14 Element Removal from Red/Black Trees We will only examine the cases where current is a right child, the other cases are easily derived If the sibling’s color is red then we begin with the following steps:

15 FIGURE Red/black tree after removal

16 Element Removal from Red/Black Trees Next our processing continues regardless of whether the original sibling was red or black Now our processing is divided into two cases based upon the color of the children of the sibling If both of the children of the sibling are black then:

17 Element Removal from Red/Black Trees If the children of the sibling are not both black, then we check to see if the left child of the sibling is black If it is, then we must complete the following steps before continuing:

18 Element Removal from Red/Black Trees Then to complete the process in the case when both of the children of the sibling are not black:

19 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!

20 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 ¬ ­ ® LR L LR ¯ °

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

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

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

24 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 1)public void inorderPrint(BinaryNode t ) 2) if (left != null) 2) if (left != null) 3) inorderPrint(t.left ); 3) inorderPrint(t.left ); 4) System.out.println(data); 4) System.out.println(data); 5) if (right != null) 5) if (right != null)  inorderPrint(t.right );  }

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

26 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

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

28 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 Right_rotate Left_rotate

29 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 colorings to make a red-black tree

30 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)

31 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.)

32 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

33 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

34 Trees - Red-Black Trees Lemma A RB-Tree with n nodes has height  2 log(n+1) Essentially, height  2 black height Search time O( log n )

35 Same as a binary tree with these two attributes added 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 class RedBlackNode { Colour red, black ; Object data; RedBlackNode left, right, parent; }

36 Trees - Insertion Insertion of a new node Requires a re-balance of the tree Label the current node x Insert node 4 Mark it red

37 Trees - Insertion While we haven’t reached the root and x’s parent is red x.parent

38 Trees - Insertion If x is to the left of it’s grandparent x.parent x.parent.parent

39 Trees - Insertion y is x’s right uncle x.parent x.parent.parent right “uncle”

40 Trees - Insertion x.parent x.parent.parent right “uncle” If the uncle is red, change the colours of y, the grand-parent and the parent

41 Trees - Insertion

42 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

43 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 the uncle is black this time and x is to the right of it’s parent

44 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 );.. So move x up and rotate about x as root...

45 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 );

46 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...

47 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.... and x is to the left of its parent uncle

48 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 */ x.parent.colour = black; x.parent.parent.colour = red; right_rotate( T, x.parent.parent ); }.. So we have the final case..

49 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 */ x.parent.colour = black; x.parent.parent.colour = red; right_rotate( T, x.parent.parent ); }.. Change colours and rotate..

50 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 */ x.parent.colour = black; x.parent.parent.colour = red; right_rotate( T, x.parent.parent ); }

51 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 */ x.parent.colour = black; x.parent.parent.colour = red; right_rotate( T, x.parent.parent ); } This is now a red-black tree.. So we’re finished!

52 Trees - Insertion while ( (x != T.root) && (x.parent.colour == red) ) { if ( x.parent == x.parent.parent.left ) { else.... There’s an equivalent set of cases when the parent is to the right of the grandparent!

53 Red-black trees - Analysis Addition InsertionComparisons 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

54 Red Black Trees - What you need to know? 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