CS 240 Data Structures and Data Management. Module 4: Dictionaries

Similar documents
Data Structures and Algorithms " Search Trees!!

Ordered Dictionary & Binary Search Tree

Search Trees. Chapter 10. CSE 2011 Prof. J. Elder Last Updated: :52 AM

CS Data Structures and Algorithm Analysis

Priority queues implemented via heaps

Binary Search Trees. Motivation

Data Structures and and Algorithm Xiaoqing Zheng

Search Trees. EECS 2011 Prof. J. Elder Last Updated: 24 March 2015

Binary Search Trees. Lecture 29 Section Robb T. Koether. Hampden-Sydney College. Fri, Apr 8, 2016

Randomized Sorting Algorithms Quick sort can be converted to a randomized algorithm by picking the pivot element randomly. In this case we can show th

Amortized analysis. Amortized analysis

AVL Trees. Properties Insertion. October 17, 2017 Cinda Heeren / Geoffrey Tien 1

AVL Trees. Manolis Koubarakis. Data Structures and Programming Techniques

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

16. Binary Search Trees. [Ottman/Widmayer, Kap. 5.1, Cormen et al, Kap ]

Assignment 5: Solutions

Quiz 1 Solutions. Problem 2. Asymptotics & Recurrences [20 points] (3 parts)

Weight-balanced Binary Search Trees

Advanced Implementations of Tables: Balanced Search Trees and Hashing

Weight-balanced Binary Search Trees

Part III. Data Structures. Abstract Data Type. Dynamic Set Operations. Dynamic Set Operations

7 Dictionary. EADS c Ernst Mayr, Harald Räcke 109

CS361 Homework #3 Solutions

7 Dictionary. Ernst Mayr, Harald Räcke 126

8: Splay Trees. Move-to-Front Heuristic. Splaying. Splay(12) CSE326 Spring April 16, Search for Pebbles: Move found item to front of list

Analysis of Algorithms. Outline 1 Introduction Basic Definitions Ordered Trees. Fibonacci Heaps. Andres Mendez-Vazquez. October 29, Notes.

Splay Trees. CMSC 420: Lecture 8

7 Dictionary. Harald Räcke 125

INF2220: algorithms and data structures Series 1

Dictionary: an abstract data type

Outline. Computer Science 331. Cost of Binary Search Tree Operations. Bounds on Height: Worst- and Average-Case

Chapter 5 Data Structures Algorithm Theory WS 2017/18 Fabian Kuhn

CS 151. Red Black Trees & Structural Induction. Thursday, November 1, 12

CS60007 Algorithm Design and Analysis 2018 Assignment 1

Dictionary: an abstract data type

Fundamental Algorithms

CS 310: AVL Trees. Chris Kauffman. Week 13-1

Lecture 5: Splay Trees

past balancing schemes require maintenance of balance info at all times, are aggresive use work of searches to pay for work of rebalancing

Premaster Course Algorithms 1 Chapter 3: Elementary Data Structures

Each internal node v with d(v) children stores d 1 keys. k i 1 < key in i-th sub-tree k i, where we use k 0 = and k d =.

8 Priority Queues. 8 Priority Queues. Prim s Minimum Spanning Tree Algorithm. Dijkstra s Shortest Path Algorithm

Lecture 17: Trees and Merge Sort 10:00 AM, Oct 15, 2018

ENS Lyon Camp. Day 2. Basic group. Cartesian Tree. 26 October

Chapter 5 Arrays and Strings 5.1 Arrays as abstract data types 5.2 Contiguous representations of arrays 5.3 Sparse arrays 5.4 Representations of

1. [10 marks] Consider the following two algorithms that find the two largest elements in an array A[1..n], where n >= 2.

Problem. Maintain the above set so as to support certain

Inge Li Gørtz. Thank you to Kevin Wayne for inspiration to slides

7 Dictionary. 7.1 Binary Search Trees. Binary Search Trees: Searching. 7.1 Binary Search Trees

searching algorithms

Lecture 7: Dynamic Programming I: Optimal BSTs

In-Class Soln 1. CS 361, Lecture 4. Today s Outline. In-Class Soln 2

Quiz 1 Solutions. (a) f 1 (n) = 8 n, f 2 (n) = , f 3 (n) = ( 3) lg n. f 2 (n), f 1 (n), f 3 (n) Solution: (b)

Data Structure. Mohsen Arab. January 13, Yazd University. Mohsen Arab (Yazd University ) Data Structure January 13, / 86

University of New Mexico Department of Computer Science. Midterm Examination. CS 361 Data Structures and Algorithms Spring, 2003

Partitions and Covers

4.8 Huffman Codes. These lecture slides are supplied by Mathijs de Weerd

Skip Lists. What is a Skip List. Skip Lists 3/19/14

data structures and algorithms lecture 2

Lecture 4: Divide and Conquer: van Emde Boas Trees

Data Structures and Algorithms

Tree Heap Priority Queue

AVL trees. AVL trees

Divide-and-Conquer Algorithms Part Two

Heaps and Priority Queues

16. Binary Search Trees

Fundamental Algorithms

7.3 AVL-Trees. Definition 15. Lemma 16. AVL-trees are binary search trees that fulfill the following balance condition.

16. Binary Search Trees

Module 9: Tries and String Matching

25. Minimum Spanning Trees

25. Minimum Spanning Trees

Problem. Problem Given a dictionary and a word. Which page (if any) contains the given word? 3 / 26

Advanced Data Structures

CS 161 Summer 2009 Homework #2 Sample Solutions

Review Of Topics. Review: Induction

University of New Mexico Department of Computer Science. Final Examination. CS 561 Data Structures and Algorithms Fall, 2013

Divide-and-conquer: Order Statistics. Curs: Fall 2017

RANDOMIZED ALGORITHMS

COMP 250. Lecture 24. heaps 2. Nov. 3, 2017

Height, Size Performance of Complete and Nearly Complete Binary Search Trees in Dictionary Applications

15.1 Introduction to Lower Bounds Proofs

Objec&ves. Review. Data structure: Heaps Data structure: Graphs. What is a priority queue? What is a heap?

Recitation 7. Treaps and Combining BSTs. 7.1 Announcements. FingerLab is due Friday afternoon. It s worth 125 points.

Skip List. CS 561, Lecture 11. Skip List. Skip List

Advanced Data Structures

CSCI 3110 Assignment 6 Solutions

Lecture 2 September 4, 2014

Average Case Analysis of QuickSort and Insertion Tree Height using Incompressibility

Computing Static Single Assignment (SSA) Form

Graphs and Trees Binary Search Trees AVL-Trees (a,b)-trees Splay-Trees. Search Trees. Tobias Lieber. April 14, 2008

Today s Outline. CS 362, Lecture 4. Annihilator Method. Lookup Table. Annihilators for recurrences with non-homogeneous terms Transformations

Midterm 1 for CS 170

Algorithm Design CS 515 Fall 2015 Sample Final Exam Solutions

1 Probability Review. CS 124 Section #8 Hashing, Skip Lists 3/20/17. Expectation (weighted average): the expectation of a random quantity X is:

Problem: Data base too big to fit memory Disk reads are slow. Example: 1,000,000 records on disk Binary search might take 20 disk reads

1 Quick Sort LECTURE 7. OHSU/OGI (Winter 2009) ANALYSIS AND DESIGN OF ALGORITHMS

Part IA Algorithms Notes

CMSC 132, Object-Oriented Programming II Summer Lecture 6:

Searching. Sorting. Lambdas

Transcription:

CS 24 Data Structures and Data Management Module 4: Dictionaries A. Biniaz A. Jamshidpey É. Schost Based on lecture notes by many previous cs24 instructors David R. Cheriton School of Computer Science, University of Waterloo Spring 218 References: Goodrich & Tamassia 3.1, 3.2, 3.6 version 218-5-3 23:12 Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 1 / 27

Outline 1 Dictionaries and Balanced Search Trees ADT Dictionary Review: Binary Search Trees AVL Trees Insertion in AVL Trees Restoring the AVL Property: Rotations Deletion in AVL Trees Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218

Outline 1 Dictionaries and Balanced Search Trees ADT Dictionary Review: Binary Search Trees AVL Trees Insertion in AVL Trees Restoring the AVL Property: Rotations Deletion in AVL Trees Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218

Dictionary ADT A dictionary is a collection of items, each of which contains a key some data, and is called a key-value pair (KVP). Keys can be compared and are (typically) unique. Operations: search(k) (also called findelement(k)) insert(k, v) (also called insertitem(k,v)) delete(k) (also called removeelement(k))) optional: join, isempty, size, etc. Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 2 / 27

Elementary Implementations Common assumptions: Dictionary has n KVPs Each KVP uses constant space Keys can be compared in constant time Unordered array or linked list search Θ(n) insert Θ(1) delete Θ(n) (need to search) Ordered array search Θ(log n) (via binary search) insert Θ(n) delete Θ(n) Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 3 / 27

Outline 1 Dictionaries and Balanced Search Trees ADT Dictionary Review: Binary Search Trees AVL Trees Insertion in AVL Trees Restoring the AVL Property: Rotations Deletion in AVL Trees Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218

Binary Search Trees (review) Structure Binary tree (all internal nodes have two children) Every internal node stores a KVP Every external node stores empty tree (usually not shown) Ordering Every key k in T.left is less than the root key. Every key k in T.right is greater than the root key. 15 6 25 1 8 14 23 29 27 5 ( In our examples we only show the keys, and we show them directly in the ) node. A more accurate picture would be key = 15, <other info> Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 4 / 27

BST Search and Insert BST-search(k) Start at root, compare k to current node. Stop if found or node is external, else recurse at child. BST-insert(k, v) Search for k, then insert (k, v) as new node Example: Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 5 / 27

BST Delete First search for the node x that contains the key. If x is a leaf, just delete it. If x has one child, move child up Else, swap key at x with key at successor or predecessor node and then delete that node Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 6 / 27

Height of a BST BST-search, BST-insert, BST-delete all have cost Θ(h), where h = height of the tree = max. path length from root to leaf If n items are BST-inserted one-at-a-time, how big is h? Worst-case: n 1 = Θ(n) Best-case: Θ(log n). Any binary tree with n nodes has height log(n + 1) 1 Average-case: Can show Θ(log n) Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 7 / 27

Outline 1 Dictionaries and Balanced Search Trees ADT Dictionary Review: Binary Search Trees AVL Trees Insertion in AVL Trees Restoring the AVL Property: Rotations Deletion in AVL Trees Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218

AVL Trees Introduced by Adel son-vel skĭı and Landis in 1962, an AVL Tree is a BST with an additional height-balance property: The heights of the left subtree L and right subtree R differ by at most 1. (The height of an empty tree is defined to be 1.) At each non-empty node, we require height(r) height(l) { 1,, 1}: 1 means the tree is left-heavy means the tree is balanced +1 means the tree is right-heavy Need to store at each node the height of the subtree rooted at it Can show: It suffices to store height(r) height(l) at each node. uses fewer bits code gets more complicated, especially for deleting Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 8 / 27

AVL tree example (The lower numbers indicate the height of the subtree.) 22 4 1 3 31 2 4 1 14 2 28 37 1 6 13 18 1 46 16 Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 9 / 27

Height of an AVL tree Theorem: An AVL tree on n nodes has Θ(log n) height. AVL-search, AVL-insert, AVL-delete all cost Θ(log n) in the worst case! Proof: Define N(h) to be the least number of nodes in a height-h AVL tree. What is a recurrence relation for N(h)? What does this recurrence relation resolve to? Caution, Goodrich & Tamassia uses a different height-definition, therefore their base cases are different from ours Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 1 / 27

Outline 1 Dictionaries and Balanced Search Trees ADT Dictionary Review: Binary Search Trees AVL Trees Insertion in AVL Trees Restoring the AVL Property: Rotations Deletion in AVL Trees Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218

AVL insertion To perform AVL-insert(T, k, v): First, insert (k, v) into T with the usual BST insertion. We assume that this returns the new leaf z where the key was stored. Then, move up the tree from z, updating heights. We assume for this that we have parent-links. This can be avoided if BST-Insert returns the full path to z. If the height difference becomes ±2 at node z, then z is unbalanced. Must re-structure the tree to rebalance. Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 11 / 27

AVL insertion AVL-insert(r, k, v) 1. z BST-insert(r, k, v) 2. z.height 3. while (z is not null) 4. setheightfromchildren(z) 5. if ( z.left.height z.right.height = 2) then 6. AVL-fix(z) // see later 7. break // can argue that we are done 8. else 9. z parent of z setheightfromchildren(u) 1. u.height 1 + max{u.left.height, u.right.height} Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 12 / 27

AVL Insertion Example Example: 22 4 1 3 31 2 4 1 14 2 28 37 1 6 13 18 1 46 16 Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 13 / 27

Fixing a slightly-unbalanced AVL tree This is applied at a node z that has balance ±2, but the subtrees at z are AVL-trees. It makes the subtree rooted at z balanced. AVL-fix(z) // Find child and grand-child that go deepest. 1. if (z.right.height > z.left.height) then 2. y z.right 3. if (y.left.height > y.right.height) then 4. x y.left 5. else x y.right 6. else 7. y z.left 8. if (y.right.height > y.left.height) then 9. x y.right 1. else x y.left 11. Apply appropriate rotation to restructure at x, y, z Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 14 / 27

Outline 1 Dictionaries and Balanced Search Trees ADT Dictionary Review: Binary Search Trees AVL Trees Insertion in AVL Trees Restoring the AVL Property: Rotations Deletion in AVL Trees Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218

How to fix an unbalanced AVL tree Note: there are many different BSTs with the same keys. 4 4 3 2 2 2 3 2 4 3 4 3 D 2 D A 4 A 3 A C A B C D B D B C A B C D B C Goal: change the structure among three nodes without changing the order and such that the subtree becomes balanced. Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 15 / 27

Right Rotation This is a right rotation on node z: z y y x z x D C A B C D A B Note: Only two edges need to be moved, and two subtree heights updated. Useful to fix left-left imbalance. Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 16 / 27

Right Rotation in detail z y x D C A B Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 17 / 27

Pseudocode for right rotation rotate-right(z) z: node of BST tree 1. y z.left 2. make y.right the new left child of z 3. make y the new root of the subtree 4. make z the new right child of y 5. setheightfromchildren(z) 6. setheightfromchildren(y) Recall: update to links also need to update the parent! For example, to make y the new root of the subtree: 1. p parent of z 2. if p is not null 3. if z = p.left 4. p.left y 5. else p.right y 6. else make y the overall root of the tree Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 18 / 27

Left Rotation Symmetrically, this is a left rotation on node z: z y y z x A x B A B C D C D Again, only two edges need to be moved and two heights updated. Useful to fix right-right imbalance. Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 19 / 27

Double Right Rotation This is a double right rotation on node z: z x y y z x D A A B C D B C First, a left rotation at y. Second, a right rotation at z. Useful for left-right imbalance. Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 2 / 27

Double Left Rotation Symmetrically, there is a double left rotation on node z: z x y z y A x D A B C D B C First, a right rotation at y. Second, a left rotation at z. Useful for right-left imbalance. Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 21 / 27

Fixing a slightly-unbalanced AVL tree revisited AVL-fix(z) 1.... // identify y and x as before 2. case 3. z 4. z 5. z 6. z x y x y x y y x : : // Right rotation rotate-right(z) : : // Double-right rotation rotate-left(y) rotate-right(z) : : // Double-left rotation rotate-right(y) rotate-left(z) : : // Left rotation rotate-left(z) Rule: The middle key of x, y, z becomes the new root. Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 22 / 27

AVL Insertion Example revisited Example: 22 4? 1 3? 31 2 4 2 14 2 28 37 1 6 1 13 18 1 46 8 16 Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 23 / 27

Outline 1 Dictionaries and Balanced Search Trees ADT Dictionary Review: Binary Search Trees AVL Trees Insertion in AVL Trees Restoring the AVL Property: Rotations Deletion in AVL Trees Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218

AVL Deletion Remove the key k with BST-delete. We assume that BST-delete returns the place where structural change happened, i.e., the parent z of the node that got deleted. (This is not necessarily near the one that had k.) Now go back up to root, update heights, and rotate if needed. AVL-delete(r, k) 1. z BST-delete(r, k) 2. while (z is not null) 3. setheightfromchildren(z) 4. if ( z.left.height z.right.height = 2) then 5. AVL-fix(z) 6. // Always continue up the path and fix if needed. 7. z parent of z Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 24 / 27

AVL Deletion Example Example: 22 4 1 3 31 2 6 1 14 2 28 37 1 4 8 13 18 1 46 16 Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 25 / 27

AVL Tree Operations Runtime AVL-search: Just like in BSTs, costs Θ(height) AVL-insert: BST-insert, then check & update along path to new leaf total cost Θ(height) AVL-fix restores the height of the tree it fixes to what it was, so AVL-fix will be called at most once. AVL-delete: BST-delete, then check & update along path to deleted node total cost Θ(height) AVL-fix may be called Θ(height) times. Total cost for all operations is Θ(height) = Θ(log n). Biniaz, Jamshidpey, Schost (SCS, UW) CS24 Module 4 Spring 218 26 / 27