1 / 15

Hash Tables cse.unt/~Nielsen/1040/ ../~sweany/CSCE1040F13/Home.html

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

Download Presentation

Hash Tables cse.unt/~Nielsen/1040/ ../~sweany/CSCE1040F13/Home.html

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. Polish Notation • (5 − 6) * 7 • * - 5 6 7 • 5 – 6 * 7 • - 5 * 6 7

  4. 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 }

  5. 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 }

  6. 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 }

  7. Hash Tables • Why do we need to store the key?

  8. Hash Tables #include "stdio.h” Entry insert(____ key, ____ value); main() { … }

  9. 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 }

  10. 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 }

  11. 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 }

  12. 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 }

  13. 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)

  14. Hash Tables • Ideally, though not realistically, you want to map each key to a different index.

  15. Search and Rescue

More Related