Min Chen School of Computer Science and Engineering Seoul National University Data Structure: Chapter 10
Analogy of Hash Tables Definition of Hash Table Visualization of Hash Table Problem in Hash Table Chaining Keys in Hash Table Compress Function Design
Word and Definition Word is a key that addresses the definition Number of keys: ▪ 26×26=676 Map the word to a list of integer will increase the searching process
Public Class Word { public static final int LETTER=26; public static final int WORDS= LETTER* LETTER; private String word; public int hashCode() { return LETTERS*(word.charAt(0)-’a’)+ (word.charAt(1)-’a’); }
Public Class Dictonary { private Definition[] defTable = new Definition[Word.WORDS]; public void insert(Word w, Definition d) { defTable[w.hashCode()]=d; } public Definition find(Word w) { return defTable[w.hashCode()]; } If Maximum length of words is 45 characters, we need length to store the keys
A hash table or hash map is a data structure that uses a hash function to efficiently map certain identifiers or keys to associated values.
n: number of keys(words) stored Table of N buckets: N is a bit larger than n A hash table maps huge set of possible keys into N buckets by applying a compression function to each hash code …… Buckets N …… n
Redundant buckets If the compress function is not well designed, there may be many redundant buckets in the table Collision In order to decrease the size of the table, the compress function may lead several keys hash to a same bucket Solution: Chaining Keys
Compress Function
How to design a good compress function?