CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University.

Slides:



Advertisements
Similar presentations
Chapter 13. Red-Black Trees
Advertisements

David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Definitions and Bottom-Up Insertion
AVL-Trees (Part 2: Double Rotations) Lecture 19 COMP171 Fall 2006.
Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
IKI 10100: Data Structures & Algorithms Ruli Manurung (acknowledgments to Denny & Ade Azurat) 1 Fasilkom UI Ruli Manurung (Fasilkom UI)IKI10100: Lecture.
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
CS2420: Lecture 24 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 30 Vladimir Kulyukin Computer Science Department Utah State University.
CS2420: Lecture 28 Vladimir Kulyukin Computer Science Department Utah State University.
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 Binary Search Trees Implementing Balancing Operations –AVL Trees –Red/Black Trees Reading:
CS2420: Lecture 27 Vladimir Kulyukin Computer Science Department Utah State University.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Trees and Red-Black Trees Gordon College Prof. Brinton.
Red Black Trees Colored Nodes Definition Binary search tree.
CS2420: Lecture 29 Vladimir Kulyukin Computer Science Department Utah State University.
Dynamic Set AVL, RB Trees G.Kamberova, Algorithms Dynamic Set ADT Balanced Trees Gerda Kamberova Department of Computer Science Hofstra University.
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.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
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.
2-3 Trees Extended tree.  Tree in which all empty subtrees are replaced by new nodes that are called external nodes.  Original nodes are called internal.
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 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.
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
Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.
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: 
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 RED-BLACK TREES (RBT)
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.
Red-Black Trees Definitions and Bottom-Up Insertion.
Red Black Trees Top-Down Deletion. Recall the rules for BST deletion 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has.
Bottom-Up Red-Black Trees Top-down red-black trees require O(log n) rotations per insert/delete. Color flips cheaper than rotations. Priority search trees.
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
CS 307 Fundamentals of Computer ScienceRed Black Trees 1 Topic 19 Red Black Trees "People in every direction No words exchanged No time to exchange And.
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 Bottom-Up Deletion. Recall “ordinary” BST Delete 1.If vertex to be deleted is a leaf, just delete it. 2.If vertex to be deleted has just.
David Luebke 1 3/20/2016 CS 332: Algorithms Skip Lists.
More Trees. Outline Tree B-Tree 2-3 Tree Tree Red-Black Tree.
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.
AA Trees.
Red-Black Tree Neil Tang 02/07/2008
Red-Black Tree Neil Tang 02/04/2010
G64ADS Advanced Data Structures
CS202 - Fundamental Structures of Computer Science II
AVL DEFINITION An AVL tree is a binary search tree in which the balance factor of every node, which is defined as the difference between the heights of.
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees Bottom-Up Deletion.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees Bottom-Up Deletion.
Red-Black Trees.
Red Black Trees Top-Down Deletion.
CS223 Advanced Data Structures and Algorithms
Red-Black Trees Bottom-Up Deletion.
CS202 - Fundamental Structures of Computer Science II
Red-Black Trees.
Red-Black Trees Bottom-Up Deletion.
Red-black tree properties
Red Black Trees Top-Down Deletion.
CS202 - Fundamental Structures of Computer Science II
CS202 - Fundamental Structures of Computer Science II
Presentation transcript:

CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University

Outline Red-Black Tree –Bottom-up Insertion –Top-Down Insertion Section 12.2

Red-Black Tree: Properties 1.Every node is colored black or red. 2.The root is black. 3.If a node is red, its children must be black. 4.Every path from a node to a NULL pointer must have the same number of black nodes. 5.The NULL pointers are black.

Red-Black Tree: Example

Red-Black Tree: Properties No path is more than twice as long as any other path. If every path from the root to a NULL node contains B black nodes, the tree contains at least 2 B – 1 black nodes (Proof is by induction on B). The height of a red-black tree is at most 2log(N+1).

Red-Black Tree: Bottom-Up Insertion A new node is always inserted as a leaf, because this is a BST. There are two choices for coloring the new node: black or red. If we color the node black, we violate property 4. If we color the node red, we may violate property 3. Which should we choose: black or red?

Red-Black Tree: Bottom-Up Insertion A new node is colored red. Why? Because it is easier. We may have to do no work.

Red-Black Tree: Three Basic Insertion Operations Color Flip (Color Change) Two Rotations –Single rotation –Double rotation

Red-Black Tree: Rotations In terms of pointer manipulation, Red-Black tree rotations are the same as the AVL search tree rotations. Thus, there are two single rotations (right and left) and two double rotations (left right and right left). For implementation purposes, we need only the two single rotations. The rotations are symmetric. Unlike the AVL search tree rotations, the Red- Black tree rotations change node colors.

Single Right at G: S is Black G P X S X is inserted and colored red X’s parent’s sibling, S, is black AB CDE

Single Right at G: S is Black G P X S AB CDE P XG S DE CAB Before RotationAfter Rotation

Single Left at G: S is Black G P X S X is inserted and colored red X’s parent’s sibling, S, is black AB C D E

Single Left at G: S is Black G P X S AB C DE P XG S DE C AB Before RotationAfter Rotation

Double Rotation (LR) at G: S is Black G P X S A BC DE X is inserted and colored red X’s parent’s sibling, S, is black

Double Rotation (LR) at G: S is Black G P X S A BC DE P AB G X C S DE Before RotationAfter Rotation

Double Rotation (RL) at G: S is Black RL at G is symmetric to LR at G. We first rotate P right and then rotate G left. The color flips are the same as in LR.

Question When S is black, will the single and double rotations re-balance the tree?

Answer Yes, when S is black, both the single and double rotations re-balance the tree. Why? Because the number of black nodes on all paths remains the same.

Single Right at G: S is Red G P X S X is inserted and colored red X’s parent’s sibling, S, is red AB CDE

Single Right at G: S is Red G P X S AB CDE P X AB G C S DE Before RotationAfter Rotation

Single Left at G : S is Red Single left at G when S is Red is symmetric to single right at G when S is Red. G goes down, P goes up. The color flips are the same.

Double Rotation (LR) at G: S is Red G P X S A BC DE

G P X S A BC DE X PG S DE AB C Before RotationAfter Rotation

Question When S is red, will the single or double rotations re-balance the tree?

Answer When S is red, neither the single nor the double rotations re-balance the tree, because the root of the sub-tree is now red. Property 3 may be violated higher up.

Bottom-Up Insertion We insert a leaf, color it red, and then climb up the insertion path until we reach the root or perform a rotation that rebalances the tree. We may have to make two passes: one down and one up the tree.

Red-Black Tree: Top-Down Insertion Can we insert, change colors, and rotate in one pass down the tree? Yes, we can. How? We need to guarantee that when we insert a node (leaf), S is never red. Basically, we have to flip colors as we drill down the tree.

Top-Down Color Flip When a node X has two red children, X is colored red and its two children are colored black.

Top-Down Color Flip X XLXR X XLXR Before Color FlipAfter Color Flip

Question Is there anything that can go wrong during the top-down color flip?

Answer Yes, the color flip may introduce two consecutive red nodes. Can it introduce more than two consecutive red nodes? No! Because that would mean that property 3 was already violated before the insertion.

Question Can we fix property 3 violations?

Answer Yes! If a color flip results in two consecutive red nodes, we can apply either single or double rotation, whichever is appropriate. But, since we have ensured that S is black, the rotation will rebalance the tree.

Top-Down Insertion: Example

Top-Down Insertion: Example Flip?

Top-Down Insertion: Example Flip? No!

Top-Down Insertion: Example Flip? No! Flip? 45

Top-Down Insertion: Example Flip? No! 45

Top-Down Insertion: Example Flip? No! Flip? 45

Top-Down Insertion: Example Flip? No! 45

Top-Down Insertion: Example Flip? No! Flip? 45

Top-Down Insertion: Example Flip? No! Flip? Yes! 45

Top-Down Insertion: Example Reds!!! 45

Question Which Rotation and Where?

Single Right at 70: 85 is Black AB CDE DE CAB Before RotationAfter Rotation

Top-Down Insertion: After the Rotation

Top-Down Insertion: Example Flip? 45

Top-Down Insertion: Example Flip? No! 45

Top-Down Insertion: Example

Red-Black Tree: Implementation nullNode is a node used in place of a NULL pointer. nullNode is always colored black. header is a node that is used as a dummy root. The value of the header is The real root is the right child of the header.