Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8.

Slides:



Advertisements
Similar presentations
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
Advertisements

Binary Search Trees Data Structures & Problem Solving Using JAVA Second Edition Mark Allen Weiss Chapter 19 (continued) © 2002 Addison Wesley.
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.
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.
November 5, Algorithms and Data Structures Lecture VIII Simonas Šaltenis Nykredit Center for Database Research Aalborg University
ITEC200 Week 11 Self-Balancing Search Trees. 2 Learning Objectives Week 11 (ch 11) To understand the impact that balance has on.
1 Balanced Search Trees  several varieties  AVL trees  trees  Red-Black trees  B-Trees (used for searching secondary memory)  nodes are added.
Chapter 6: Transform and Conquer Trees, Red-Black Trees The Design and Analysis of Algorithms.
AVL-Trees (Part 1: Single Rotations) Lecture COMP171 Fall 2006.
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:
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.
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.
CS2420: Lecture 31 Vladimir Kulyukin Computer Science Department Utah State University.
Self-Balancing Search Trees Chapter 11. Chapter Objectives  To understand the impact that balance has on the performance of binary search trees  To.
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.
Balanced Trees Abs(depth(leftChild) – depth(rightChild))
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.
AVL Trees Neil Ghani University of Strathclyde. General Trees Recall a tree is * A leaf storing an integer * A node storing a left subtree, an integer.
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.
Balancing Binary Search Trees. Balanced Binary Search Trees A BST is perfectly balanced if, for every node, the difference between the number of nodes.
Balanced Trees (AVL and RedBlack). Binary Search Trees Optimal Behavior ▫ O(log 2 N) – perfectly balanced tree (e.g. complete tree with all levels filled)
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.
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
Chapter 13 B Advanced Implementations of Tables – Balanced BSTs.
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.  Define the red-black tree properties  Describe and implement rotations  Implement red-black tree insertion  We will skip red-black.
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.
1 AVL Trees II Implementation. 2 AVL Tree ADT A binary search tree in which the balance factor of each node is 0, 1, of -1. Basic Operations Construction,
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.
Lecture 23 Red Black Tree Chapter 10 of textbook
AA Trees.
File Organization and Processing Week 3
G64ADS Advanced Data Structures
Red Black Trees
Balanced Trees (AVL and RedBlack)
Red-Black Trees v z Red-Black Trees 1 Red-Black Trees
Red-Black Trees Bottom-Up Deletion.
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 Bottom-Up Deletion.
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.
Red-Black Trees.
Red Black Trees Top-Down Deletion.
Balanced Binary Search Trees
Algorithms and Data Structures Lecture VIII
Red-Black Trees Bottom-Up Deletion.
Red-black tree properties
Red Black Trees Top-Down Deletion.
Presentation transcript:

Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 8

 Motivation of Red-Black Trees  Balanced and Unbalanced Trees  AVL Trees  Definition of Red-Black Trees  Operations for Red-Black Trees  Search  Insertion

 Balanced and Unbalanced Trees

 AVL-Tree  The balance factor of a node is the height of its left subtree minus the height of its right subtree  A node with balance factor 1, 0, or -1 is considered balanced Balance Factor: 2-0=2 Balance Factor: 2-0=2 Balance Factor: 0-1=-1 Balance Factor: 0-1= Unbalanced

 AVL-Tree: Rotation Operation

Left Right Case

 Definition: a binary tree, satisfying: 1. Every node is colored either red or black 2. The root is black 3. If a node is red, its children must be black ▪ consecutive red nodes are disallowed 4. Every path from a node to a null reference must contain the same number of black nodes

 The insertion sequence is  10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5,

Each path must contain the same number of black nodes. (Rule #4) Consecutive red nodes are not allowed. (Rule #3) The longest path is at most twice the length of the shortest path

 B = total black nodes from root to leaf  N = total all nodes  H = height All operations guaranteed logarithmic!

 A new node must be colored red  Why? ▪ A new item is always inserted as a leaf in the tree ▪ If we color a new item black, then the number of black nodes from root would be different (violate property #4)  If the parent is black, no problem.  If the parent is red, we create two consecutive red nodes (violate property #3) ▪ Thus, we have to do some rotating/recolouring…

AB G X S P C DE AB G X S P C DE X: new node P: parent S: sibling G: Grandparent  Case after insertion:  Consecutive red (P & X)  Sibling of parent (S) is black  X is “outer node” (left-left or right-right)

BC G X S P A DE AB G P S X C DE X: new node P: parent S: sibling G: Grandparent Case after insertion: Consecutive red (P & X) Sibling of parent (S) is black X is “inner node” (left-right or left)

 Case after insertion:  Consecutive red  Sibling of parent is red  Outer node (left-left or right-right) AB G X SP C DE AB G X S P C DE But what if P’s parent is red?  We have to keep going up the tree all the way to the root 

 The solution: prevent S from ever being red!  Starting from the root (searching for insertion point)  Never allow 2 red siblings  If we see a node X with 2 red children, do a colour flip. X C2C1 X C2

 Maintains property #4  Possible violation of #3: if X’s parent is red! ▪ Do single or double rotation ▪ X’s parent’s sibling can never be red!  Set the root to black (to maintain property #2) X C2C1 X C2

AB G X SP C DE AB G X SP C DE If we do the colour flipping on the way down to the insertion point, we will never reach a condition where P & S are red!

Color-flip!

Color-flip!

 The insertion sequence is 10, 85, 15, 70, 20, 60, 30, 50, 65, 80, 90, 40, 5, 55

 Deletion in BST: only leaf nodes or nodes with one child are really deleted (Why?)  If the deleted node is red: no problem (all properties maintained). Leaf nodes: Single child nodes:

 If node to be deleted is black  violate property #4  Always ensure that the node to be deleted is red.  Top-down traversal from root (looking for node to be deleted): X: visited node P: parent S: sibling B P S X A CD Idea: make sure that X is red!

 P is red (inductive invariant)  X and S are black (result of property #3)  2 cases:  1. Both X’s children (A & B) are black  2. X has at least one red child (A, B, or both) B P S X A CD

 Depends on children of S (C & D):  Both C & D are black: simply colour-flip: B P S X A C D B P S X A C D

Case 1: Both X’s children are black Outer child of S (C) is Red: do a single rotation and recolour B P S X A C D C S D P B X A

 Inner child of S (C) is Red: do a double rotation and recolour B P S X A D C C S P B X A D

 Recurse down to X’s child  If we land on a red node, fine.  If we land on a black node, rotate sibling and parent: B P S X A C C S P B X A D D

 Red-Black trees use color as balancing information instead of height in AVL trees.  An insertion may cause a local perturbation (two consecutive red nodes)  The pertubation is either  resolved locally (rotations), or  propagated to a higher level in the tree by recoloring (color flip)  O(1) for a rotation or color flip  At most one restructuring per insertion.  O(log n) color flips  Total time: O(log n)