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

UN
Uni24h
0 次下载
13
0

资料内容

第 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

第 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

第 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

第 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?

第 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

第 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.

显示前 12 页。下载资料查看完整内容。

评论 (0)

登录 以加入讨论

暂无评论。快来抢沙发吧!