1 / 16

Hash Table

Data Structures. Hash Table. Consider storing data for 100 employees by their Social Security Numbers (SSN) SSN range: 000000000 – 999999999 A fast search: SSN is index of an array E.g. myItem is at position 123456789 myItem = myArray [123456789] Characteristics

maude
Download Presentation

Hash Table

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. Data Structures Hash Table

  2. Consider storing data for 100 employees by their Social Security Numbers (SSN) • SSN range: 000000000 – 999999999 • A fast search: SSN is index of an array • E.g. • myItem is at position 123456789 • myItem = myArray[123456789] • Characteristics • Good--Efficiency of O(1) • Bad-- Requires array of size 109, most of which is unoccupied • Can we have something that • Has efficiency of nearly O(1) and • Requires reasonable amount of space Why Hash Table?

  3. Keys (e.g., SSN) are “hashed” (mish-mashed) to another number, which can be used as index for a smaller array. E.g., • item1 key: 1234568989 myArray[89] = item1 • Item2 key: 1234567575 myArray[75] = item2 • Item3 key: 3457892121 myArray[21] = item3 • Item4 key: 9234572121 myArray[?] = item4 • Last 2 cases: collision, when two keys hash to the same hash value Hashing

  4. item1: 1234568989 item2: 1234567575 item3: 3457892121 item4: 9234572121 Item5: 7854387575 Item6: 5432192424 Item7: 3311222020 Resolving Collisions (Chaining) Key Hashed Index Data myTable 0 Ω 1 Ω … Ω 20 item7 21 item4 Ω item3 23 Ω item6 24 Ω … … Ω 75 item2 item5 76 Ω … 89 item1 Ω

  5. item1: 1234568989 item2: 1234567575 item3: 3457892121 item4: 9234572121 Item5: 7854387575 Item6: 5432192424 Item7: 3311222020 Resolving Collisions (Open Hashing) Key Hashed Index Data myTable 0 -1 1 -1 … 20 Item 7 21 Item3 Item4 22 23 -1 24 Item 6 … Item 2 75 76 Item 5 … 89 Item 1

  6. Given names as keys: • amy • tammy • frank • billy • Convert keys to array indices. • Max size of array: 10 • name(hashF1) integer (hashF2) index • E.g., • amyAscii(a) + Ascii(m) + Ascii(y) • = 97 + 109 + 121 = 327 327 % 10 = 7 • tammyAscii(t) + Ascii(a) + Ascii(m) + Ascii(m) + Ascii(y) = 116 + 97 + 109 + 109 + 121 = 552 552 % 10 = 2 Hashing Non-integer Keys myTable 0 Ω 1 Ω Ω 2 tammy … 6 Ω amy 7 Ω 8 Ω … Ω 10 …

  7. Given: key = 1234567 • h(key) = key % 100 • 1234567  67 • 3785421  21 • Given: key = beth • h1(key) = ascii(b) + ascii(e) + ascii(t) + ascii(h) = 98 + 121 + 116 + 104 = 419 • h2(419) = 415 % 100 = 19 • h(key) = h2(h1(key)) = 19 • Thus, “beth” -> 19 A Simple hash function

  8. void insert (elemType &item); • Inserts item in the table, • bool remove (keyType key); • Removes data item corresponding to key. Returns true when successful; false, otherwise. • bool search (keyType key); • Searches the table for item corresponding to key. Returns true when match found; false, otherwise. • elemType search(keyType key); • Searches and return the value corresponding to key • book isEmpty(); • void clear(); ADT Hash Table

  9. bool search (keyType key); • hash(key)  anIndex T(n) = c1 • IF array[anIndex] is not empty THEN T(n) = c2 + c3 search the linked listELSE value is not presentEND IF • return true/false T(n) = c4 • T(n) = c1 + c2 + c3 + c4O(1) almost Growth Rate (efficiency) of Hash Table operations

  10. Describe the data structure to represent a hash table Write a C++ code for the structure, to be included in the private section of the (hash) Table class. Your Turn

  11. Hash table data structure Ω 0 Array Struct 1 Ω 2 Ω Ω 3 4 Ω Ω 5 Ω 6 7 Ω Ω 8 9 Ω

  12. To represent a group of data elements of differenttypesas a single unit Contrast this with an array, whose elements must of the sametype. Is like a class, but every part is public by default. No operations is a struct. Struct

  13. Definition: struct athlete_t { string name; int weight; // in kg int height; // in cm}; STRUCT (Example) Usage: athlete_t ath; ath.name = “John”;ath.weight = 200;ath.height = 190; cout << ath.name << endl;cout << ath.weight << endl;cout << ath.height << endl; athlete_t team[MAX]; team[0] = ath;

  14. Data structure for Hash table private: struct nodeType { elemType data; nodeType *next; }; static const int MAX = 10; nodeType* list[MAX]; };

  15. ADT (Hash) table class Table { public: Table(); void insert(elemType item); bool remove(keyType key); bool retrieve(keyType key, elemType &item); void clear(); void print(); private: ... };

  16. Constructor • What needs to be done in the constructor? • Write a C++ code to implement Table(). • Insert • What are the steps necessary to insert an item into the Hash table? • Write a C++ code to implement insert(elemType item) in the Table class. Your turn

More Related