Fall 2017 Mentoring 9: October 23, Min-Heapify This. Level order, bubbling up. Level order, bubbling down. Reverse level order, bubbling up

Similar documents
(f) Given what we know about linked lists and arrays, when would we choose to use one data structure over the other?

EXAMINATIONS 2016 TRIMESTER 2

Fall 2017 Mentoring 7: October 9, Abstract Data Types

Domain-Driven Design Activity

Collections, Maps and Generics

CIT-590 Final Exam. Name: Penn Key (Not ID number): If you write a number above, you will lose 1 point

CSE373 Fall 2013, Second Midterm Examination November 15, 2013

CS 61B Midterm 2 Guerrilla Section Spring 2018 March 17, 2018

Programming Languages and Techniques (CIS120)

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

CS61B Spring 2016 Guerrilla Section 6 Worksheet

Hash tables. hashing -- idea collision resolution. hash function Java hashcode() for HashMap and HashSet big-o time bounds applications

CS61B Fall 2014 Guerrilla Section 2 This worksheet is quite long. Longer than we ll cover in guerrilla section.

CS61B Lecture #24: Hashing. Last modified: Wed Oct 19 14:35: CS61B: Lecture #24 1

Introducing Hashing. Chapter 21. Copyright 2012 by Pearson Education, Inc. All rights reserved

CS61B, Fall 2015 Final Examination (with corrections) P. N. Hilfinger

CSC 1351: Final. The code compiles, but when it runs it throws a ArrayIndexOutOfBoundsException

USAL1J: Java Collections. S. Rosmorduc

Menu. In general, what are possible advantages of one design over another? Class 27: Exam 2. Exam 2 Parenthesizing the Expression. Design A.

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. Fall 2016.

Dynamic Dictionaries. Operations: create insert find remove max/ min write out in sorted order. Only defined for object classes that are Comparable

Section 05: Midterm Review

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Announcements. Submit Prelim 2 conflicts by Thursday night A6 is due Nov 7 (tomorrow!)

Data Structures Brett Bernstein

EXAMINATIONS 2017 TRIMESTER 2

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger.

Section 05: Solutions

Practice Midterm Exam Solutions

1.00 Lecture 32. Hashing. Reading for next time: Big Java Motivation

Standard ADTs. Lecture 19 CS2110 Summer 2009

Equality for Abstract Data Types

CMSC 433 Section 0101 Fall 2012 Midterm Exam #1

CS 520 Theory and Practice of Software Engineering Fall 2018

Tutorial #11 SFWR ENG / COMP SCI 2S03. Interfaces and Java Collections. Week of November 17, 2014

COMP 103 Introduction to Data Structures and Algorithms

Selection, Bubble, Insertion, Merge, Heap, Quick Bucket, Radix

CS/ENGRD 2110 FALL Lecture 4: The class hierarchy; static components

CS 520 Theory and Practice of Software Engineering Fall 2017

Hashing. Reading: L&C 17.1, 17.3 Eck Programming Course CL I

DO NOT. UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N.

CS 520 Theory and Practice of Software Engineering Fall 2017

CS61BL: Data Structures & Programming Methodology Summer 2014

EXAMINATIONS 2005 END-YEAR. COMP 103 Introduction to Data Structures and Algorithms

The dictionary problem

Questions. 6. Suppose we were to define a hash code on strings s by:

Solutions to Quiz 1 (March 14, 2016)

The Java Type System (continued)

CS61BL Summer 2013 Midterm 2

Hash Table. Ric Glassey

Chapter 11: Collections and Maps

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

a) For the binary tree shown below, what are the preorder, inorder and post order traversals.

CS 61BL Data Structures & Programming Methodology

DO NOT. In the following, long chains of states with a single child are condensed into an edge showing all the letters along the way.

(b) Draw a hash table having 10 buckets. Label the buckets 0 through 9.

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

Introduction hashing: a technique used for storing and retrieving information as quickly as possible.

Practice Problems for the Final

Draw the resulting binary search tree. Be sure to show intermediate steps for partial credit (in case your final tree is incorrect).

CSE331 Winter 2014, Midterm Examination February 12, 2014

Announcements. Hash Functions. Hash Functions 4/17/18 HASHING

Programming Languages and Techniques (CIS120)

Prelim 2. CS 2110, November 20, 2014, 7:30 PM Extra Total Question True/False Short Answer

Section 05: Solutions

Chair of Software Engineering. Languages in Depth Series: Java Programming. Prof. Dr. Bertrand Meyer. Exercise Session 10

CS 310: Hash Table Collision Resolution

Hash Table and Hashing

Module 2: Classical Algorithm Design Techniques

Compsci 201 Hashing. Jeff Forbes February 7, /7/18 CompSci 201, Spring 2018, Hashiing

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

You must include this cover sheet. Either type up the assignment using theory5.tex, or print out this PDF.

Section 1: True / False (1 point each, 15 pts total)

Quiz 1 Solutions. Asymptotic growth [10 points] For each pair of functions f(n) and g(n) given below:

I have neither given nor received any assistance in the taking of this exam.

Announcements. Container structures so far. IntSet ADT interface. Sets. Today s topic: Hashing (Ch. 10) Next topic: Graphs. Break around 11:45am

CS61BL: Data Structures & Programming Methodology Summer 2014

Lecture 13: AVL Trees and Binary Heaps

Lecture 16: HashTables 10:00 AM, Mar 2, 2018

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

Mapping Structures. Chapter An Example: Language Dictionaries

Hash Tables. Gunnar Gotshalks. Maps 1

Midterm Exam 2 CS 455, Spring 2015

CS/COE 1501

Lecture 7: Efficient Collections via Hashing

Priority Queues and Heaps (continues) Chapter 13: Heaps, Balances Trees and Hash Tables Hash Tables In-class Work / Suggested homework.

1. AVL Trees (10 Points)

CSC 207 (16fa) Practice Exam 2 Page 1 of 13. Practice Exam 2 (Exam 2 date: Friday 11/18) Logistics:

Prelim 2, CS2110. SOLUTION

CS/ENGRD 2110 SPRING Lecture 4: The class hierarchy; static components

INSTRUCTIONS TO CANDIDATES

Programming Languages and Techniques (CIS120e)

Java Collections Framework reloaded

Second Examination Solution

Computer Science 62. Bruce/Mawhorter Fall 16. Midterm Examination. October 5, Question Points Score TOTAL 52 SOLUTIONS. Your name (Please print)

CS 3343 (Spring 2013) Exam 2

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

APCS Semester #1 Final Exam Practice Problems

Today. Reading. Homework. Lecture Notes CPSC 224 (Spring 2012) hashcode() method. Collections class. Ch 9: hw 7 out (due in a week)

CSC 207 (17fa) Practice Final Page 1 of 19. Practice Final

Transcription:

CSM B Heaps & Hashing Fall 0 Mentoring : October 3, 0 Min-Heapify This. In general, there are 4 ways to heapify. Which ways actually work? Level order, bubbling up Level order, bubbling down Reverse level order, bubbling up Reverse level order, bubbling down Only level order, bubbling up and reverse level order, bubbling down work as they maintain heap invariant. Namely, that every node is either larger (in a max heap) or smaller (in a min heap) than all of its children. 8. (a) Show the heapification of the tree. 8 8 8 8 8 (b) Now, insert the value. 8 8

Heaps & Hashing 8 (c) Finally, remove the value. 8 8 8

Heaps & Hashing 3 Motivation. (a) In the worst case, how long does it take to index into a linked list? Θ(N) (b) In the worst case, how long does it take to index into an array? Θ() (c) In the worst case, how long does it take to insert into a linked list? Θ(N), where N is the length of the linked list. (d) Assuming there s space, how long does it take to put a element in an array? Θ() (e) What if we assume there is no more space in the array? Θ(N) to copy over N elements into the new array. (f) Given what we know about linked lists and arrays, how could we build a data structure with efficient access and efficient insertion? If you know in advance how large your data structure is, arrays are faster than linked lists in insertion, mutation, etc. However, if the array needs to expand frequently then things get expensive. But there are ways to amortize the cost of resizing with ArrayLists, for example. An array of linked lists will offer constant look up to a certain linked list, and adding to the front of that linked list will also be constant. This is a HashMap. Objects with rows and columns (like a chessboard) where we wish to randomly index into exact position and where the board is of a fixed size Arguments to a java program: String[] args. Using a resizing List in this scenario doesn t necessarily make things better since the arguments to a program don t change once we start the program.

4 Heaps & Hashing 3 Hash Table Basics 3. public class BadHashMap<K, V> implements Map<K, V> { private V[] array; public void put(k key, V value) { this.array[key.hashcode() % this.array.length] = value; interface Map<K, V> { boolean containskey(k key); V get(k key); void put(k key, V value); int size(); (a) Why do we use the % (modulo) operator? Allows us to take a large int and turn it into an index into the array. (b) What are collisions? What data structure can we use to address them? Collisions occur when two keys map to the same bucket, or index, in the array. We can use external chaining with a linked list, for example, to resolve the conflicts. Each bucket can be associated with more than one key. (c) Why is this a bad HashMap? BadHashMap doesn t handle collisions. If a given key produces a collision, the value will be completely overwritten rather than stored alongside the value currently there.

Heaps & Hashing 3. (a) Draw the diagram that results from the following operations on a Java HashMap. Integer::hashCode returns the integer s value. put(3, "monument"); put(8, "shrine"); put(3, "worker"); put(, "granary"); put(3, "worker"); "worker" replaces "monument" as their keys are the same. Each put must iterate through the entire external chain to ensure that a keyupdate is not necessary. (b) Suppose a resize occurs, doubling the array to size 0. What changes? The value of "shrine" and "granary" will move. Specifically, the new length of the array is 0. A key of 8 will force "shrine" to be placed in the 8th index. A key of will move "granary" to the th index. Everything else will remain the same.

Heaps & Hashing 4 Hash Codes Extra Practice 4. What does it mean for a hashcode to be valid? Consistency: Whenever it is invoked on the same object more than once, the hashcode method must consistently returns the same integer if nothing about the object changes. Equality constraint: If two objects are equal according to the equals( Object) method, then calling the hashcode method on each of the two objects must produce the same integer result. It is not required that if two objects are unequal according to the equals(object) method, then calling the hashcode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables. 4. Which of the following hashcodes are valid? Good? class Point { private int x, y; private static int count = 0; public Point(int x, int y) { this.x = x; this.y = y; count += ; (a) public void hashcode() { System.out.print(this.x + this.y); Invalid. Return type should be int. The method should override Java s hashcode method, but since it has the same signature with different return types, this would be a compile-time error (b) public int hashcode() { Random random = new Random(); return random.nextint(); Invalid. Not consistent. This method would compile because it does return an int. However, this does not obey the principle of consistency about returning the same integer every time, and so the HashMap would

Heaps & Hashing not work as expected. If we store an object at key, the next time around the object might have key, and we would be unable to find the object. (c) public int hashcode() { return this.x + this.y; Valid, but certain inputs may cause a significant number of collisions. This method would work, since it obeys all bullets under the contract. However, this will cause many collisions. All points that sum up to the same number will be mapped to the same hashcode such as (, 3), (4, 4), (, ), (, ), (, ), (, ),... (d) public int hashcode() { return count; Invalid. Not consistent. This method would not error, because it does return an int, but it does not obey the principle of consistency. count is a static variable. Consider what happens when we create a new Point (3, 4) and call the hashcode method on it the first time. We would get. If we then create a new Point(, ), calling the hashcode method on the earlier Point(3, 4) would now return, even though nothing about the original Point object changed. (e) public int hashcode() { return 4; Valid, but causes collisions on all inputs. While this method is valid, it collides on any input.