Top Banner
Data Structures
16

Hash Table

Jan 05, 2016

Download

Documents

maude

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 - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Hash Table

Data Structures

Page 2: 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 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

Page 3: Hash Table

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: 1234568989 myArray[89] = item1• Item2 key: 1234567575 myArray[75] = item2• Item3 key: 3457892121 myArray[21] = item3• Item4 key: 9234572121 myArray[?] = item4

Last 2 cases: collision, when two keys hash to the same hash value

Page 4: Hash Table

item1: 1234568989

item2: 1234567575

item3: 3457892121

item4: 9234572121

Item5: 7854387575

Item6: 5432192424

Item7: 3311222020

0

1

20

23

75

76

89

21

myTable

24

Ω

Ω

Ω

Ω

Ω

Ω

Ω

Ω

Ω

item7

item6

item4item3

item5item2

item1

Data Key Hashed Index

Page 5: Hash Table

item1: 1234568989

item2: 1234567575

item3: 3457892121

item4: 9234572121

Item5: 7854387575

Item6: 5432192424

Item7: 3311222020

0

1

20

23

75

76

89

21

myTable

24

Data Key Hashed Index

Item 7

-1

-1

Item3

22 Item4

Item 6

-1

Item 2Item 5

Item 1

Page 6: Hash Table

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

amyAscii(a) + Ascii(m) + Ascii(y) = 97 + 109 + 121 = 327

327 % 10 = 7 tammyAscii(t) + Ascii(a) + Ascii(m) + Ascii(m)

+ Ascii(y) = 116 + 97 + 109 + 109 + 121 = 552 552 % 10 = 2

0

1

2

6

8

10

myTable

7

Ω

Ω

Ω

Ω

Ω

Ω

Ω

tammy

amy

Page 7: Hash Table

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

Page 8: Hash Table

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();

Page 9: Hash Table

bool search (keyType key);1. hash(key) anIndex T(n) = c12. IF array[anIndex] is not empty THEN T(n) = c2 + c3

search the linked listELSE value is not presentEND IF

3. return true/false T(n) = c4

1. T(n) = c1 + c2 + c3 + c4O(1) almost

Page 10: Hash Table

1. Describe the data structure to represent a hash table

2. Write a C++ code for the structure, to be included in the private section of the (hash) Table class.

Page 11: Hash Table

0

1

8

7

2

3

4

5

6

9

Ω

Ω

Ω

Ω

Ω

Ω

ΩΩ

Ω

Ω

Array Struct

Page 12: Hash Table

To represent a group of data elements of different types as a single unit

Contrast this with an array, whose elements must of the same type.

Is like a class, but every part is public by default. No operations is a struct.

Page 13: Hash Table

Definition:

struct athlete_t { string name; int weight; // in kg int height; // in cm};

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;

Page 14: Hash Table

private: struct nodeType { elemType data; nodeType *next; };

static const int MAX = 10; nodeType* list[MAX];};

Page 15: 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: ...};

Page 16: Hash Table

1. Constructor1. What needs to be done in the

constructor?2. Write a C++ code to implement Table().

2. Insert1. What are the steps necessary to insert an

item into the Hash table?2. Write a C++ code to implement

insert(elemType item) in the Table class.