Lecture 7. Binary Search Trees and Red-Black Trees

Similar documents
Lecture 6. Binary Search Trees and Red-Black Trees

Properties of red-black trees

CS102 Binary Search Trees

Lecture 4 Median and Selection

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Balanced Search Trees Date: 9/20/18

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Trees. (Trees) Data Structures and Programming Spring / 28

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CS 380 ALGORITHM DESIGN AND ANALYSIS

Lecture 6: Analysis of Algorithms (CS )

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

Balanced search trees

CSC148 Week 7. Larry Zhang

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

Algorithms. AVL Tree

Self-Balancing Search Trees. Chapter 11

Search Trees. Undirected graph Directed graph Tree Binary search tree

CISC 235: Topic 4. Balanced Binary Search Trees

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Define the red- black tree properties Describe and implement rotations Implement red- black tree insertion

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

COMP Analysis of Algorithms & Data Structures

A set of nodes (or vertices) with a single starting point

Binary Search Trees, etc.

COMP Analysis of Algorithms & Data Structures

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Algorithms. Red-Black Trees

Binary Trees

Lecture 10. Finding strongly connected components

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

We assume uniform hashing (UH):

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Binary Trees, Binary Search Trees

Lecture: Analysis of Algorithms (CS )

TREES Lecture 12 CS2110 Spring 2019

CS 171: Introduction to Computer Science II. Binary Search Trees

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

13.4 Deletion in red-black trees

Lecture 4. The Substitution Method and Median and Selection

Binary search trees (chapters )

Algorithms. Deleting from Red-Black Trees B-Trees

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Hash Tables. CS 311 Data Structures and Algorithms Lecture Slides. Wednesday, April 22, Glenn G. Chappell

CSE373: Data Structures & Algorithms Lecture 4: Dictionaries; Binary Search Trees. Kevin Quinn Fall 2015

Binary Search Trees. Analysis of Algorithms

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Spring 2018 Mentoring 8: March 14, Binary Trees

Balanced Binary Search Trees. Victor Gao

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Recall: Properties of B-Trees

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

Binary search trees (chapters )

B-Trees. CS321 Spring 2014 Steve Cutchin

Red-Black trees are usually described as obeying the following rules :

Friday Four Square! 4:15PM, Outside Gates

B Tree. Also, every non leaf node must have at least two successors and all leaf nodes must be at the same level.

CSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)

Binary Search Trees Treesort

CSE 100 Advanced Data Structures

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

search(i): Returns an element in the data structure associated with key i

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

13.4 Deletion in red-black trees

Chapter 2: Basic Data Structures

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

12 July, Red-Black Trees. Red-Black Trees

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

CS 350 : Data Structures B-Trees

CMPS 2200 Fall 2017 Red-black trees Carola Wenk

Public-Service Announcement

CS350: Data Structures B-Trees

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

Binary Heaps in Dynamic Arrays

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Lecture 14. Greedy algorithms!

COMP Analysis of Algorithms & Data Structures

Problem Set 5 Solutions

Trees. Eric McCreath

CS350: Data Structures Red-Black Trees

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

COMP171. AVL-Trees (Part 1)

CS 3343 Fall 2007 Red-black trees Carola Wenk

Lecture 23: Binary Search Trees

CS 206 Introduction to Computer Science II

3137 Data Structures and Algorithms in C++

Sample Solutions CSC 263H. June 9, 2016

Math Dr. Miller - Constructing in Sketchpad (tm) - Due via by Friday, Mar. 18, 2016

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

CMPT 225. Red black trees

Transcription:

Lecture Binary Search Trees and Red-Black Trees

Announcements HW released! (Due Friday)

Today: binary search trees Brief foray into data structures! See CS 166 for more! What are binary search trees? You may remember these from CS 106B Why are they good? Why are they bad? this will lead us to Self-Balancing Binary Search Trees Red-Black trees.

Why are we studying self-balancing BSTs? 1. The punchline is important: A data structure with O(log(n)) INSERT/DELETE/SEARCH 2. The idea behind Red-Black Trees is clever It s good to be exposed to clever ideas. Also it s just aesthetically pleasing.

Motivation for binary search trees We ve been assuming that we have access to some basic data structures: (Sorted) linked lists HEAD 1 2 4 5 8 (Sorted) arrays 1 2 4 5 8

Sorted linked lists 1 2 4 5 8 O(1) insert/delete (assuming we have a pointer to the location of the insert/delete): HEAD 1 2 4 5 8 O(n) search/select: 6 HEAD 1 2 4 5 8

Sorted Arrays 1 2 4 5 8 O(n) insert/delete: 1 2 4 54.5 8 O(log(n)) search, O(1) select: 1 2 4 5 8 Search: Binary search to see if is in A. Select: Third smallest is A[].

The best of both worlds Sorted Arrays Linked Lists Binary Search Trees* Search O(log(n)) O(n) O(log(n)) Insert/Delete O(n) O(1) O(log(n))

Tree terminology This node is the root 5 This is a node. It has a key (). Today all keys are distinct. Both children of are NIL 1 The left child of is 2 The right child of is 4 1 2 4 8 These nodes are leaves.

Binary Search Trees It s a binary tree so that: Every LEFT descendent of a node has key less than that node. Every RIGHT descendent of a node has key larger than that node. Example of building a binary search tree: 4 5 8 1 2

Binary Search Trees It s a binary tree so that: Every LEFT descendent of a node has key less than that node. Every RIGHT descendent of a node has key larger than that node. Example of building a binary search tree: 4 5 8 1 2

Binary Search Trees It s a binary tree so that: Every LEFT descendent of a node has key less than that node. Every RIGHT descendent of a node has key larger than that node. Example of building a binary search tree: 5 1 2 4 8

Binary Search Trees It s a binary tree so that: Every LEFT descendent of a node has key less than that node. Every RIGHT descendent of a node has key larger than that node. Example of building a binary search tree: 5 2 1 4 8

Binary Search Trees It s a binary tree so that: Every LEFT descendent of a node has key less than that node. Every RIGHT descendent of a node has key larger than that node. Example of building a binary search tree: 5 2 4 8 1

Aside: this should look familiar kinda like QuickSort 4 5 8 1 2

Binary Search Trees It s a binary tree so that: Every LEFT descendent of a node has key less than that node. Every RIGHT descendent of a node has key larger than that node. 5 5 2 4 8 2 4 8 1 Binary Search Tree 1 NOT a Binary Search Tree

SEARCH in a Binary Search Tree definition by example 5 2 4 8 EXAMPLE: Search for 4. EXAMPLE: Search for 4.5 It turns out it will be convenient to return 4 in this case (that is, return the last node before we went off the tree) 1 Write pseudocode (or actual code) to implement this! Ollie the over-achieving ostrich

INSERT in a Binary Search Tree 1 5 2 4 8 x = 4 4.5 EXAMPLE: Insert 4.5 INSERT(key): x = SEARCH(key) if key > x.key: Make a new node with the correct key, and put it as the right child of x. if key < x.key: Make a new node with the correct key, and put it as the left child of x. if x.key == key: return

DELETE in a Binary Search Tree EXAMPLE: Delete 2 5 DELETE(key): x = SEARCH(key) if x.key == key:.delete x. 1 2 4 8 x = 2 This is a bit more complicated

DELETE in a Binary Search Tree several cases (by example) say we want to delete 5 5 5 5 Case 1: if is a leaf, just delete it. 2 This triangle is a cartoon for a subtree 2 We won t write down pseudocode for this try to do it yourself! Case 2: if has just one child, move that up. Ollie the over-achieving ostrich

DELETE in a Binary Search Tree ctd. Case : if has two children, replace with it s immediate successor. (aka, next biggest thing after ) How do we find the immediate successor? SEARCH(.right, ) 5 5 2 4 2.1 4 How do we remove it when we find it? Run DELETE for one of the previous two cases..1 Wait, what if it s THIS case? (Case ). It s not.

How long do these operations take? SEARCH is the big one. Everything else just calls SEARCH and then does some small O(1)-time operation. 5 Time = O(depth of tree) Trees have depth O(log(n)). Done! 2 4 6 8 Lucky the lackadaisical lemur.

Wait... 2 4 This is a valid binary search tree. The version with n nodes has depth n, noto(log(n)). 5 6 8 Could such a tree show up? In what order would I have to insert the nodes? Inserting in the order 2,,4,5,6,,8 would do it. So this could happen.

What to do? How often is every so often in the worst case? It s actually pretty often! Keep track of how deep the tree is getting. Ollie the over-achieving ostrich If it gets too tall, re-do everything from scratch. At least Ω(n) every so often. Other ideas?

Idea 1: Rotations No matter what lives underneath A,B,C, this takes time O(1). (Why?) Maintain Binary Search Tree (BST) property, while moving stuff around. YOINK! X Y Y Y C A B X A X A B C B C CLAIM: this still has BST property.

This seems helpful YOINK! 5 2 5 4 2 4 6 8 6 8

Does this work? Whenever something seems unbalanced, do rotations until it s okay again. Even for me this is pretty vague. What do we mean by seems unbalanced? What s okay? Lucky the Lackadaisical Lemur

Idea 2: have some proxy for balance Maintaining perfect balance is too hard. Instead, come up with some proxy for balance: If the tree satisfies [SOME PROPERTY], then it s pretty balanced. We can maintain [SOME PROPERTY] using rotations. There are actually several ways to do this, but today we ll see

Red-Black Trees A Binary Search Tree that balances itself! No more time-consuming by-hand balancing! Be the envy of your friends and neighbors with the time-saving 5 Maintain balance by stipulating that black nodes are balanced, and that there aren t too many red nodes. 2 4 6 8 It s just good sense!

Red-Black Trees these rules are the proxy for balance Every node is colored red or black. The root node is a black node. NIL children count as black nodes. Children of a red node are black nodes. For all nodes x: all paths from x to NONE s have the same number of black nodes on them. 5 2 4 6 8

Examples(?) Yes! Every node is colored red or black. The root node is a black node. NONE children count as black nodes. Children of a red node are black nodes. For all nodes x: all paths from x to NONE s have the same number of black nodes on them. No! No! No!

Why??????? This is pretty balanced. The black nodes are balanced The red nodes are spread out so they don t mess things up too much. We can maintain this property as we insert/delete nodes, by using rotations. 5 2 4 6 8 9

Let s build some intuition! This is pretty balanced Lucky the lackadaisical lemur To see why, intuitively, let s try to build a Red-Black Tree that s unbalanced. One path could be twice as long as all the others if we pad it with red nodes. Conjecture: the height of a red-black tree is at most 2 log(n)

That turns out the be basically right. [proof sketch] Say there are b(x) black nodes in any path from x to NONE. (including x). Claim: Then there are at least 2 b(x) 1 nodes in the subtree underneath x. [Proof by induction on board if time] Then: n 2 $ %&&' 1 2 +,-.+/ 1 0 1 Rearranging: using the Claim b(root) >= height/2 because of RBTree rules. n + 1 2 456' 1 height 2log (n + 1) y x

Okay, so it s balanced but can we maintain it? Yes! For the rest of lecture: sketch of how we d do this. See CLRS for more details.

Inserting into a Red-Black Tree 6 Make a new red node. Insert it as you would normally. What if it looks like this? 6 Example: insert 0 0

Inserting into a Red-Black Tree 6 Make a new red node. Insert it as you would normally. Fix things up if needed. 6 Example: insert 0 What if it looks like this? No! 0

Inserting into a Red-Black Tree 6 Make a new red node. Insert it as you would normally. Fix things up if needed. 6 Example: insert 0 What if it looks like this? Can t we just insert 0 as a black node? 0 No!

Inserting into a Red-Black Tree -1 Make a new red node. Insert it as you would normally. Fix things up if needed. 6 What if it looks like this? Example: insert 0 0 6 Instead recolor like this. Need to argue: RB-Tree properties still hold. What about the red root? if 6 is actually the root, color it black. Else, recursively re-color up the tree. Now the problem looks like this, where I m inserting 6-1 6 ish

Inserting into a Red-Black Tree 6 Make a new red node. Insert it as you would normally. Fix things up if needed. 0 6 Example: Insert 0. What if it looks like this? Actually, this can t happen? It might happen that we just turned 0 red from the previous step. Or it could happen if is actually NIL.

Recall Rotations Maintain Binary Search Tree (BST) property, while moving stuff around. YOINK! X Y Y Y C A B X A X A B C B C CLAIM: this still has BST property.

Inserting into a Red-Black Tree Make a new red node. Insert it as you would normally. Fix things up if needed. 6 What if it looks like this? YOINK! 6 Need to argue that if RB-Tree property held before, it still does. 0 6 0

That s basically it A few things still left to check for INSERT! Anything else that might happen looks basically like what we just did. Formally dealing with the recursion. You check these! (or see CLRS) DELETEis similar. The punchline: Plucky the pedantic penguin Red-Black Trees always have height at most 2log(n+1). As with general Binary Search Trees, all operations are O(height) So all operations are O(log(n)).

Conclusion: The best of both worlds Sorted Arrays Linked Lists Balanced Binary Search Trees Search O(log(n)) O(n) O(log(n)) Insert/Delete O(n) O(1) O(log(n))

Recap Balanced binary trees are the best of both worlds! But we need to keep them balanced. Red-Black Trees do that for us. We get O(log(n))-time INSERT/DELETE/SEARCH Clever idea: have a proxy for balance Next time Hashing!