David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees.

Slides:



Advertisements
Similar presentations
David Luebke 1 6/1/2014 CS 332: Algorithms Medians and Order Statistics Structures for Dynamic Sets.
Advertisements

David Luebke 1 8/25/2014 CS 332: Algorithms Red-Black Trees.
Chapter 12 Binary Search 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.
Jan Binary Search Trees What is a search binary tree? Inorder search of a binary search tree Find Min & Max Predecessor and successor BST insertion.
Binary Search Trees Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Introduction to Algorithms Jiafen Liu Sept
CS 332: Algorithms Binary Search Trees. Review: Dynamic Sets ● Next few lectures will focus on data structures rather than straight algorithms ● In particular,
ALGORITHMS THIRD YEAR BANHA UNIVERSITY FACULTY OF COMPUTERS AND INFORMATIC Lecture six Dr. Hamdy M. Mousa.
The complexity and correctness of algorithms (with binary trees as an example)
David Luebke 1 5/4/2015 Binary Search Trees. David Luebke 2 5/4/2015 Dynamic Sets ● Want a data structure for dynamic sets ■ Elements have a key and satellite.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Binary Search Trees Comp 550.
1 Brief review of the material so far Recursive procedures, recursive data structures –Pseudocode for algorithms Example: algorithm(s) to compute a n Example:
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 11.
Lecture 12: Balanced Binary Search Trees Shang-Hua Teng.
Sorting. How fast can we sort? All the sorting algorithms we have seen so far are comparison sorts: only use comparisons to determine the relative order.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
Universal Hashing When attempting to foil an malicious adversary, randomize the algorithm Universal hashing: pick a hash function randomly when the algorithm.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Department of Computer Eng. & IT Amirkabir University of Technology (Tehran Polytechnic) Data Structures Lecturer: Abbas Sarraf Search.
1.1 Data Structure and Algorithm Lecture 12 Binary Search Trees Topics Reference: Introduction to Algorithm by Cormen Chapter 13: Binary Search Trees.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE Binary search trees Motivation Operations on binary search trees: –Search –Minimum,
David Luebke 1 7/2/2015 ITCS 6114 Binary Search Trees.
David Luebke 1 7/2/2015 Medians and Order Statistics Structures for Dynamic Sets.
David Luebke 1 7/2/2015 ITCS 6114 Red-Black Trees.
Design & Analysis of Algorithms Unit 2 ADVANCED DATA STRUCTURE.
Balanced Trees Balanced trees have height O(lg n).
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 Lecture 10 Nawazish Naveed. Red-Black Trees (Intro) BSTs perform dynamic set operations such as SEARCH, INSERT, DELETE etc in O(h) time.
CS 2133: Data Structures Binary Search Trees.
Chapter 12. Binary Search Trees. Search Trees Data structures that support many dynamic-set operations. Can be used both as a dictionary and as a priority.
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.
Tonga Institute of Higher Education Design and Analysis of Algorithms IT 254 Lecture 4: Data Structures.
2IL50 Data Structures Fall 2015 Lecture 7: Binary Search Trees.
Binary SearchTrees [CLRS] – Chap 12. What is a binary tree ? A binary tree is a linked data structure in which each node is an object that contains following.
CS 206 Introduction to Computer Science II 10 / 05 / 2009 Instructor: Michael Eckmann.
Binary Search Tree Qamar Abbas.
CS 206 Introduction to Computer Science II 02 / 13 / 2009 Instructor: Michael Eckmann.
CS 473Lecture X1 CS473-Algorithms Lecture RED-BLACK TREES (RBT)
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 9.
Lecture 9 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
1 Algorithms CSCI 235, Fall 2015 Lecture 22 Binary Search Trees.
Red-Black Trees. Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite.
October 19, 2005Copyright © by Erik D. Demaine and Charles E. LeisersonL7.1 Introduction to Algorithms LECTURE 8 Balanced Search Trees ‧ Binary.
Mudasser Naseer 1 1/25/2016 CS 332: Algorithms Lecture # 10 Medians and Order Statistics Structures for Dynamic Sets.
Analysis of Algorithms CS 477/677 Red-Black Trees Instructor: George Bebis (Chapter 14)
David Luebke 1 3/20/2016 CS 332: Algorithms Skip Lists.
CS6045: Advanced Algorithms Data Structures. Dynamic Sets Next few lectures will focus on data structures rather than straight algorithms In particular,
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Binary Search Trees What is a binary search tree?
Binary Search Trees.
CS 332: Algorithms Red-Black Trees David Luebke /20/2018.
CS200: Algorithms Analysis
CS 3013 Binary Search Trees.
Lecture 7 Algorithm Analysis
CS200: Algorithms Analysis
Red-Black Trees.
Binary Search Trees (13.1/12.1)
Lecture 7 Algorithm Analysis
CS6045: Advanced Algorithms
Lecture 7 Algorithm Analysis
Binary SearchTrees [CLRS] – Chap 12.
Analysis of Algorithms CS 477/677
Algorithms, CSCI 235, Spring 2019 Lecture 22—Red Black Trees
Binary Search Trees Comp 122, Spring 2004.
Chapter 12&13: Binary Search Trees (BSTs)
Red-Black Trees CS302 Data Structures
Presentation transcript:

David Luebke 1 9/18/2015 CS 332: Algorithms Red-Black Trees

David Luebke 2 9/18/2015 Review: Binary Search Trees ● Binary Search Trees (BSTs) are an important data structure for dynamic sets ● In addition to satellite data, eleements have: ■ key: an identifying field inducing a total ordering ■ left: pointer to a left child (may be NULL) ■ right: pointer to a right child (may be NULL) ■ p: pointer to a parent node (NULL for root)

David Luebke 3 9/18/2015 Review: Binary Search Trees ● BST property: key[left(x)]  key[x]  key[right(x)] ● Example: F BH KDA

David Luebke 4 9/18/2015 Review: Inorder Tree Walk ● An inorder walk prints the set in sorted order: TreeWalk(x) TreeWalk(left[x]); print(x); TreeWalk(right[x]); ■ Easy to show by induction on the BST property ■ Preorder tree walk: print root, then left, then right ■ Postorder tree walk: print left, then right, then root

David Luebke 5 9/18/2015 Review: BST Search TreeSearch(x, k) if (x = NULL or k = key[x]) return x; if (k < key[x]) return TreeSearch(left[x], k); else return TreeSearch(right[x], k);

David Luebke 6 9/18/2015 Review: BST Search (Iterative) IterativeTreeSearch(x, k) while (x != NULL and k != key[x]) if (k < key[x]) x = left[x]; else x = right[x]; return x;

David Luebke 7 9/18/2015 Review: BST Insert ● Adds an element x to the tree so that the binary search tree property continues to hold ● The basic algorithm ■ Like the search procedure above ■ Insert x in place of NULL ■ Use a “trailing pointer” to keep track of where you came from (like inserting into singly linked list) ● Like search, takes time O(h), h = tree height

David Luebke 8 9/18/2015 Review: Sorting With BSTs ● Basic algorithm: ■ Insert elements of unsorted array from 1..n ■ Do an inorder tree walk to print in sorted order ● Running time: ■ Best case:  (n lg n) (it’s a comparison sort) ■ Worst case: O(n 2 ) ■ Average case: O(n lg n) (it’s a quicksort!)

David Luebke 9 9/18/2015 Review: Sorting With BSTs ● Average case analysis ■ It’s a form of quicksort! for i=1 to n TreeInsert(A[i]); InorderTreeWalk(root);

David Luebke 10 9/18/2015 Review: More BST Operations ● Minimum: ■ Find leftmost node in tree ● Successor: ■ x has a right subtree: successor is minimum node in right subtree ■ x has no right subtree: successor is first ancestor of x whose left child is also ancestor of x ○ Intuition: As long as you move to the left up the tree, you’re visiting smaller nodes. ● Predecessor: similar to successor

David Luebke 11 9/18/2015 Review: More BST Operations ● Delete: ■ x has no children: ○ Remove x ■ x has one child: ○ Splice out x ■ x has two children: ○ Swap x with successor ○ Perform case 1 or 2 to delete it F BH KDA C Example: delete K or H or B

David Luebke 12 9/18/2015 Red-Black Trees ● Red-black trees: ■ Binary search trees augmented with node color ■ Operations designed to guarantee that the height h = O(lg n) ● First: describe the properties of red-black trees ● Then: prove that these guarantee h = O(lg n) ● Finally: describe operations on red-black trees

David Luebke 13 9/18/2015 Red-Black Properties ● The red-black properties: 1. Every node is either red or black 2.Every leaf (NULL pointer) is black ○ Note: this means every “real” node has 2 children 3.If a node is red, both children are black ○ Note: can’t have 2 consecutive reds on a path 4.Every path from node to descendent leaf contains the same number of black nodes 5.The root is always black

David Luebke 14 9/18/2015 Red-Black Trees ● Put example on board and verify properties: 1.Every node is either red or black 2.Every leaf (NULL pointer) is black 3.If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black ● black-height: # black nodes on path to leaf ■ Label example with h and bh values

David Luebke 15 9/18/2015 Height of Red-Black Trees ● What is the minimum black-height of a node with height h? ● A: a height-h node has black-height  h/2 ● Theorem: A red-black tree with n internal nodes has height h  2 lg(n + 1) ● How do you suppose we’ll prove this?

David Luebke 16 9/18/2015 RB Trees: Proving Height Bound ● Prove: n-node RB tree has height h  2 lg(n+1) ● Claim: A subtree rooted at a node x contains at least 2 bh(x) - 1 internal nodes ■ Proof by induction on height h ■ Base step: x has height 0 (i.e., NULL leaf node) ○ What is bh(x)?

David Luebke 17 9/18/2015 RB Trees: Proving Height Bound ● Prove: n-node RB tree has height h  2 lg(n+1) ● Claim: A subtree rooted at a node x contains at least 2 bh(x) - 1 internal nodes ■ Proof by induction on height h ■ Base step: x has height 0 (i.e., NULL leaf node) ○ What is bh(x)? ○ A: 0 ○ So…subtree contains 2 bh(x) - 1 = = 0 internal nodes (TRUE)

David Luebke 18 9/18/2015 RB Trees: Proving Height Bound ● Inductive proof that subtree at node x contains at least 2 bh(x) - 1 internal nodes ■ Inductive step: x has positive height and 2 children ○ Each child has black-height of bh(x) or bh(x)-1 (Why?) ○ The height of a child = (height of x) - 1 ○ So the subtrees rooted at each child contain at least 2 bh(x) internal nodes ○ Thus subtree at x contains (2 bh(x) ) + (2 bh(x) ) + 1 = 22 bh(x) = 2 bh(x) - 1 nodes

David Luebke 19 9/18/2015 RB Trees: Proving Height Bound ● Thus at the root of the red-black tree: n  2 bh(root) - 1(Why?) n  2 h/2 - 1(Why?) lg(n+1)  h/2(Why?) h  2 lg(n + 1)(Why?) Thus h = O(lg n)

David Luebke 20 9/18/2015 RB Trees: Worst-Case Time ● So we’ve proved that a red-black tree has O(lg n) height ● Corollary: These operations take O(lg n) time: ■ Minimum(), Maximum() ■ Successor(), Predecessor() ■ Search() ● Insert() and Delete(): ■ Will also take O(lg n) time ■ But will need special care since they modify tree