Heaps of Hashing (Discussion 9) (Heap, bảng băm và mã băm) - Christine Zhou

UN
Uni24h
0 downloads
13
0

Document content

Trang 3

Announcements - Mid-semester survey due this Friday - Less time to work on the problems by yourself - Goes a little too slowly - Don’t really need to talk to each other to discuss the problem - Make sure to go through the harder problems - Midterm coming up next week - Start studying early! - Advising sessions are updated weekly! - tinyurl.com/cs61b-advising - Project 2 - Take a look at spec for extra credit opportunities! - Start early! We’ll have extra office hours just like we did for Proj1 - Refetch from shared to get an updated col/row

Trang 4

Heaps - Sometimes we want to prioritize elements in a certain way, always get the minimum - Use a heap! - Complete: missing items only at the bottom level and pushed as far left on the bottom level as possible - (Min/Max) heap property: every node’s value is (less/greater) than or equal to its children’s value

Trang 5

Common Methods of Heaps - Insert - Put new item in first free bottom leftmost position, then bubble up - removeMin/removeMax - Replace root item with the bottom rightmost item, then bubble down the root - getMin/getMax - Return the value at the root

Trang 9

HashMaps - Use an array to represent your data - For each key-value pair, assign each key a “hash code” - mod (%) the hash code by the length of the array (this gives you a number between 0 and array.length - 1), use this as the index! - Be careful about negative numbers when modding! - N = num elements, M = num buckets, C = some constant - If N/M > C, then increase M! - N/M is called the load factor - If we have multiple elements that go to same place in the array, usually we will keep track of a list - This is called external chaining - What is the runtime of .contains and .insert?

Trang 11

Hash Codes - What is necessary for a valid hash code? - If A.equals(B) is true, then A.hashCode() == B.hashCode() - The opposite is not necessarily true - What is necessary for a good hash code? - Spread things out nicely/evenly in our hash table - Default: use Object class’s hashCode method - Returns the address of the object - We can override this method

Trang 12

3 Hash Functions Here are three potential implementations of the Integer’s hashCode() function. Categorize each as either a valid or an invalid hash function. If it is invalid, explain why. If it is valid, point out a flaw/disadvantage. Note: A “valid” hashCode() means that any two Integers that are .equals() to each other should also return the same hash code value. Another note: the Integer class extends the Number class, a direct subclass of Object. The Number class’ hashCode() method directly calls the Object class’ hashCode() method.

Showing the first 12 pages. Download the document to see it all.

Comments (0)

Sign in to join the discussion

No comments yet. Be the first!