hash tables dr. li jiang school of computer science, the university of adelaide

66
Hash Tables Dr. Li Jiang School of Computer Science, The University of Adelaide

Upload: csilla

Post on 15-Feb-2016

32 views

Category:

Documents


0 download

DESCRIPTION

Hash Tables Dr. Li Jiang School of Computer Science, The University of Adelaide. Overview. Hash Table Table ADT Direct addressing and its problem Hash Table Concept Hash Function Example of using a hash function Benefit and problem of using a hash function Hash table ADT operations - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Hash TablesDr. Li Jiang

School of Computer Science, The University of Adelaide

Page 2: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

OverviewHash Table

Table ADT• Direct addressing and its problem Hash Table Concept

Hash Function Example of using a hash function Benefit and problem of using a hash function

Hash table ADT operationsCollision and collision resolution Examples:

ADT operations and Using chaining approach to resolve collision

Page 3: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Learning Objectives

By the end of this lecture, you should be able to:• Understand and interpret the concepts of hash

table and hash function.• Define hash table function and hash table

operations for solving simple problem• Understand the collision and one of the collision

resolution approaches – chaining approach• Use chaining approach to solve collision problem

Page 4: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

An Example of A Table

BHM Birmingham International AirportLGB Long BeachLAX Los Angeles International AirportOAK OaklandIAD Washington, Dulles International AirportHNL Honolulu International AirportBOS Boston, Logan International AirportACY Atlantic City International AirportCLE Cleveland PDX Portland International Airport

(Key, Value)

Page 5: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

An Example of A Table

BHM Birmingham International AirportLGB Long BeachLAX Los Angeles International AirportOAK OaklandIAD Washington, Dulles International AirportHNL Honolulu International AirportBOS Boston, Logan International AirportACY Atlantic City International AirportCLE ClevelandPDX Portland International Airport

Key Associated Information (Airports name, or related information )

(cont.)

Page 6: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

An Example of A Table

BHM Birmingham International AirportLGB Long BeachLAX Los Angeles International AirportOAK OaklandIAD Washington, Dulles International AirportHNL Honolulu International AirportBOS Boston, Logan International AirportACY Atlantic City International AirportCLE ClevelandPDX Portland International Airport

Key Associated Information

12345678910

(cont.)

Page 7: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples 7

Direct Addressing Suppose there are n objects required to store in the table:

The range of keys is 0..n-1 , each number is (uniquely mapped to) the address of a bucket in the table

Keys are distinct The idea of the direct addressing:

Table is represented with an array, e.g. airportInfo[0..n-1] Efficiency of the algorithms implementing the operations of Table

ADT with direct addressing approach: Insert an object to the airport information table

1) airportInfo[i] = x if x airportInfo and key[x] = i2) airportInfo[i] = NULL otherwise

Insert operation takes O(1) time (no order requirement) Search operation takes O(n) time Delete operation takes O(n) time

Page 8: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples 8

Advantages of Direct Addressing

If number of objects and size of table is reasonably small:

Direct Addressing is an efficient way to access the data

It takes less time for any operation on direct addressing table.

Page 9: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples 9

Problems with Direct Addressing

When the size of table is very large:Using a table T of size N and N is a large

number (e.g. >10000), using direct addressing may be impractical, given the memory available on a typical computer.The number of the objects actually stored

may be so small relative to large space created. Thus, most of the space allocated for T would be wasted.

Page 10: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

An Example of A Table (cont.)

BHM Birmingham International AirportLGB Long BeachLAX Los Angeles International AirportOAK OaklandIAD Washington, Dulles International AirportHNL Honolulu International AirportBOS Boston, Logan International AirportACY Atlantic City International AirportCLE ClevelandPDX Portland International Airport

Key Associated Information (Airports name, or related information )

Page 11: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples 11

An Example of Table

Assume that Data items of 400 airports needs to be processed. The key: Airport code with three letters, used to

identify each airport.

If direct addressing approach is used,

• Number of different three letter combinations will be 26 × 26 × 26 =17576 (possible number of airports)

• The fraction of actual keys (Buckets) needed: 400/17576=2.2%• Percent of the memory allocated for table wasted, 97.8%• Again, the operations on the table will take: O(1) to O(n) time

BucketsThe data item of one airport

(cont.)

Page 12: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples 12

Another ExampleAssume that: The information about 50 students in a class is to be stored

in a table. The key is defined as 9 digit Student Identification Number,

used to identify each student.

If direct addressing approach is used, we will find that Number of possible keys with 9 digit number will be 109

The fraction of actual keys needed. 50/109, 0.000005% Percent of the memory allocated for table wasted,

99.999995%

A better way is necessary Hash Table

Page 13: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Hash Table

The hash table is a table of elements that have keys, usually represented as (Key, Element) pair

A hash function is used for locating a position in the table

13

Dictionary

Can be any type of object

h( key ) Location of the object containing the key

A hash table maps a huge set of possible keys into index of N buckets by applying a hash function to each hash code

Key S, where S is usually a huge set of possible keys

Notice : Ns = Card |S|, Ns is much larger than N, n is the actual number of objects that are processed

Ideally, n =N or n=a× N +b where a and b is small number

Page 14: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Hash Functions The input into a hash function is a key value The output from a hash function is an index of an array

(hash table) where the object containing the key is located

The most commonly used hash function is:

h( hashCode ) = hashCode mod N

Where the hashCode is the key of an element, N is the number of buckets that is actually used

Notice that the hashCode is not often obvious, building a model to compute it is required.

Page 15: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Examples of Hash Functions

(1) h( k ) = k % 101 if k is an integer and it is the key for the associated element

(2) What the hash function of the Airport Code will be for processing data items of up to 400 airports?

One of the answers will be: h(Ariport_code) =p(fitstChar) × p(secondChar) × p(thirdChar)%400

p is a position function which maps a character to its position value

Divisor is usually the size of the table, it is set to a prime when

the keys contains a lot of 0s

A B C D E F G H I J K L ……1 2 3 4 5 6 7 8 9 10 11 12 ……

h(CLE) =3 × 12 × 5%400=180

h(CLE)=?

Page 16: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

16

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Hash Table ADT Operations

Insert: to insert an element into a table Retrieve: to retrieve an element from the table Remove: to remove an element from the table Update: to update an element in the table Empty: to empty out the hash table

Page 17: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Inserting an Object in A HashTableThe following pseudo-code for the insert operation:

public: bool insert( key, object) {

1. Compute the key's hash code. 2. Compute the hash function to determine the index of bucket. 3. Insert the object into the bucket's chain with the index of the

bucket obtained from 2. }

Insertion is done in O( 1 ) time

Notice that here is bucket’s chain, instead of bucket.

Page 18: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

18

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Inserting an Object in A HashTable

An example of insert operation An element (Cleveland) is inserted into a

hash table.(suppose we only need to deal with 101 big airports)

1

……

80

……

2

79

……

……

……

……

Buckets

Clevelandh(CLE)=h(180)=180%101=79

What the hash function will be?

h( k ) = k % 101

– To find where an element is to be inserted, use the hash function on its key

– If the key value is 180, the element is to be stored in index 79 of the array

– Insertion is done in O( 1 ) time

Page 19: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

19

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

The Benefit of Using a Hash Function

Using a hash table, we simply have a function which provides us with the index of the array where the object containing the key is located Other alternative is expensive

If we have millions of objects with (key, values) structure, it may take a long time to search a regular array or a linked list for a specific part number (on average, we might compare 500,000 key values)

Page 20: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

The Problem of Using a Hash Function Consider the hash function

h( k ) = k % 100

20

• a key value of 114 is used for a second object; the result of the hash function is 14, but index 14 is already occupied,

– This is called a collision

How do we solve this problem?

Collision is the circumstance where several keys hash to the same bucket. This happens when: h( hashCode1 ) == h( hashCode2 )

Suppose that • a key value of 214 is used for an object, and the

object is stored at index 14

Page 21: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

21

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

How are Collisions Resolved?

The most popular way to resolve collisions is by chaining Instead of having an array of objects, we

have an array of linked lists, each node of which contains an object

An element is inserted by using the hash function -- the hash function provides an index of a linked list, and the element is inserted at the front of that (usually short) linked list

When searching for an element, the hash function is used to get the correct linked list, then the linked list is searched for the key (with the element) If we had 500,000 keys, this approach

is still much faster than comparing 500,000 keys with other approaches)

9

20

46

42

36

2

31

0

1

2

3

4

5

6

Note: The whole object is stored but only the key value is shown

Value

Page 22: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

22

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

An Example of Searching an Object in A HashTable

Pseudo-code for the retrieve (search, find) operation

• A search for an element can be done in O( 1 ) time.

The following pseudo-code for the retrieve (find) operation:public: bool retrieve( DataType & key) { 1. Hash the key

• find the hash code and compute hash function with the given key to obtain the index of the bucket.

2. Search through the linked list specified by the bucket index number.

3. If you find the entry with the right key you return it; otherwise return null.

}

Page 23: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

An Example of HashTable Class

template <class DataType> class HashTable { public:

HashTable( int (*hf)(const DataType &), int s ); bool insert( const DataType & newObject ); // returns true if successful; // returns false if invalid index was returned from hash function bool retrieve( DataType & retrieved ); // retrieve the item for the given key bool remove( DataType & removed ); // remove the item for the given key bool update( DataType & updateObject ); // update the item for the given key void makeEmpty( ); // empty out the hash table

private: Array< LinkedList<DataType> > table; int (*hashfunc)(const DataType &); // pointer to hash function

};

Page 24: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

24

An Example of Using Chaining

0

1

2

3

4

5

6

A hash table which is initially empty.

Every element is a LinkedList object. Only the start pointer of the LinkedList object is shown, which is set to NULL at the beginning.

The hash function is: h( k ) = k % 7

Page 25: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

25

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

An Example of Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 31

The hash function is: h( k ) = k % 7

Page 26: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

26

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

An Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 31

The hash function is: h( k ) = k % 7

h(31)=31 % 7= 3

Page 27: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

27

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

Assumed that the hash function is: h( k ) = k % 7

31

0

1

2

3

4

5

6Note: The whole object is stored but only the key value is shown

Page 28: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

28

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

The hash function is: h( k ) = k % 7

INSERT object with key 931

Page 29: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

29

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 9

9 % 7 = 231

The hash function is: h( k ) = k % 7

Page 30: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

30

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

9 INSERT object with key 9

9 % 7 is 231

The hash function is: h( k ) = k % 7

Page 31: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

31

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 36

36 % 7 is 1

9

31

Page 32: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

32

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

36

INSERT object with key 36

36 % 7 is 1

9

31

Page 33: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

33

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 42

36

9

31

Page 34: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

34

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 42

42 % 7 is 0

36

9

31

Page 35: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

35

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

42

INSERT object with key 42

42 % 7 is 0

36

9

31

Page 36: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

36

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 46

42

36

9

31

Page 37: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

37

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 46

46 % 7 is 4

42

36

9

31

Page 38: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

38

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

46

INSERT object with key 46

46 % 7 is 4

42

36

9

31

Page 39: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

39

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 20

46

42

36

9

31

Page 40: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

40

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 20

20 % 7 is 646

42

36

9

31

Page 41: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

41

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6 20

INSERT object with key 20

20 % 7 is 646

42

36

9

31

Page 42: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

42

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 2

20

46

42

36

9

31

Page 43: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

43

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 2

2 % 7 is 2

20

46

42

36

9

31

Page 44: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

44

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

COLLISION occurs !!

INSERT object with key 2

2 % 7 is 2

20

46

42

36

9

31

But an object has been inserted in the location with index 2 of the linked list before

Inserts the new element at the BEGINNING of the list

How to resolve this?

Page 45: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

45

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 2

2 % 7 is 2

20

46

42

36

9

31

Page 46: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

46

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 2

2 % 7 is 2

20

46

42

36

9

31

Page 47: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

47

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 2

2 % 7 is 2

20

46

42

36

9

31

Page 48: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

48

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 2

2 % 7 is 2

20

46

42

36

9

31

Page 49: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

49

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

9INSERT object with key 2

2 % 7 is 2

20

46

42

36

2

31

Page 50: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

50

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 24 9

20

46

42

36

2

31

Page 51: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

51

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 24

24 % 7 is 3

9

20

46

42

36

2

31

Page 52: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

52

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 24

24 % 7 is 3

9

20

46

42

36

2

31

Page 53: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

53

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 24

24 % 7 is 3

9

20

46

42

36

2

31

Page 54: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

54

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 24

24 % 7 is 3

9

20

46

42

36

2

31

Page 55: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

55

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

INSERT object with key 24

24 % 7 is 3

9

20

46

42

36

2

31

Page 56: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

56

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

31 INSERT object with key 24

24 % 7 is 3

9

20

46

42

36

2

24

Page 57: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

57

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining (cont.)

0

1

2

3

4

5

6

e.g. FIND the object with key 9

31

9

20

46

42

36

2

24

Supposed that all objects were stored in the linked list.

How to Find an object?

Page 58: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

58

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining(cont.)

0

1

2

3

4

5

6

FIND the object with key 9

9 % 7 is 2

31

9

20

46

42

36

2

24

Page 59: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

59

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining(cont.)

0

1

2

3

4

5

6

We search this linked list for the object with key 9

FIND the object with key 9

9 % 7 is 2

31

9

20

46

42

36

2

24

Page 60: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

60

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining(cont.)

0

1

2

3

4

5

6

Remember…the whole object is stored, only the key is shown

31

9

20

46

42

36

2

24

Page 61: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

61

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining(cont.)

0

1

2

3

4

5

6

Does this object contain key 9?

31

9

20

46

42

36

2

24

Page 62: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

62

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining(cont.)

0

1

2

3

4

5

6

FIND the object with key 9Does this object contain key 9?

No, so go on to the next object.

31

9

20

46

42

36

2

24

Page 63: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

63

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining(cont.) FIND the object with key 9

Does this object contain key 9?

31

9

20

46

42

36

2

24

0

1

2

3

4

5

6

Page 64: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

64

Table ADT

Hash table concepts and Hash Function

Hash table ADT

Collision

Collision resolution

Examples

Example Using Chaining(cont.)

0

1

2

3

4

5

6

Does this object contain key 9? YES, found it! Return the object.

31

9

20

46

42

36

2

24

FIND the object with key 9

Page 65: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

SummaryHash Table

Table ADT• Direct addressing and its problem Hash Table Concept

Hash Function Example of using a hash function Benefit and problem of using a hash function

Collision and collision resolution Hash table ADT operationsExamples:

ADT operations and Using chaining approach to resolve collision

Page 66: Hash Tables Dr. Li Jiang School of Computer Science,  The University of Adelaide

END

Thank You !

Look Forward To Seeing You Again !