160 likes | 307 Views
Hash Tables http://www.cse.unt.edu/~Nielsen/1040/ ../~sweany/CSCE1040F13/Home.html. Meetings 7-8 — Sep 23+, 2013 CSCE 1040: Computer Science II Rodney Nielsen. Schedule. Today: Hash Tables & Recursion Lab 3 – This week – Thu, Sept 26 (F270) Recursion Lab 4 – Fri, Sept 27 – Thu, Oct 3
E N D
Hash Tableshttp://www.cse.unt.edu/~Nielsen/1040/../~sweany/CSCE1040F13/Home.html Meetings 7-8 — Sep 23+, 2013 CSCE 1040: Computer Science II Rodney Nielsen
Schedule • Today: Hash Tables & Recursion • Lab 3 – This week – Thu, Sept 26 (F270) • Recursion • Lab 4 – Fri, Sept 27 – Thu, Oct 3 • Hash tables plus… • Next week: Pointers and Strings
Polish Notation • (5 − 6) * 7 • * - 5 6 7 • 5 – 6 * 7 • - 5 * 6 7
Polish Notation double parsePolish() { // TODO Add error handling char *val; scanf("%s", val); if (strlen(val) == 1) { switch (val[0]) { case '+': return parsePolish() + parsePolish(); case '-': return parsePolish() - parsePolish(); … default: … } } return strtod(val, NULL); // TODO Add error handling }
Polish Notation double parsePolish() { char *val; scanf("%s", val); if (strlen(val) == 1) { switch (val[0]) { case '+': return parsePolish() + parsePolish(); … default: if ((val[0] >= '0') && (val[0] <= '9')) return strtod(val, NULL); exit(1); } } return strtod(val, NULL); // TODO Add error handling }
Polish Notation double parsePolish() { char *val; scanf("%s", val); if (strlen(val) == 1) { switch (val[0]) { case '+': return parsePolish() + parsePolish(); case '-': return parsePolish() - parsePolish(); case '*': return parsePolish() * parsePolish(); case '/': return parsePolish() * parsePolish(); default: if ((val[0] >= '0') && (val[0] <= '9')) return strtod(val, NULL); exit(1); } } return strtod(val, NULL); // TODO Add error handling }
Hash Tables • Why do we need to store the key?
Hash Tables #include "stdio.h” Entry insert(____ key, ____ value); main() { … }
Hash Tables ____ insert(____ key, ____ value) { // compute the key’s hash code // convert (compress) the hash code to compute index // insert the new entry into the table at index }
Hash Tables ____ get(____ key, ____ value) { // compute the key’s hash code // convert (compress) the hash code to compute index // search chain at index to find key // return value if found, else null }
Hash Tables ____ remove(____ key) { // compute the key’s hash code // convert (compress) the hash code to compute index // search chain at index to find key // if found, remove entry and return value // else return null }
Hash Tables What if the key already exists in the hash table? ____ add(____ key, ____ value) { // compute the key’s hash code // convert (compress) the hash code to compute index // search chain at index to find key // if found, replace value… // else add the new entry to the end of the chain }
Hash Tables Load factor of a hash table: n / N n = the number of entries in the table N = the length of the array holding the entries If: load factor is low, and the hash code and compression are good, and you don’t allow duplicate keys, then entry lists are very short and it takes very little time to find an entry (basically constant time – that is the same amount of time regardless of the size of n)
Hash Tables • Ideally, though not realistically, you want to map each key to a different index.