29
Hashing, Hashing Tables Chapter 8

Hashing, Hashing Tables Chapter 8. Class Hierarchy

Embed Size (px)

Citation preview

Page 1: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Hashing, Hashing Tables

Chapter 8

Page 2: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Class Hierarchy

Page 3: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Introduction

• Definition:– Key: a key is a field or composite of fields that

uniquely identifies an entry in a table.

Page 4: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Example

• Table of students in a course sorted by name

--------------------------------------------------------------

Name Year Mark

--------------------------------------------------------------

Adams, Keith 3 94

Davis, Susan 1 75

Jordan, Ann 1 86

Patterson, Lynn 4 73

Williams, George 1 65

Page 5: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Insert Function of ListAsArray

Page 6: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Find Function of ListAsArray

Page 7: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Insert Function of ListAsLinkedList

Page 8: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Find Function of ListAsLinkedList

Page 9: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Insert Function of SortedListAsArray

Page 10: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Binary Search

Page 11: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Hashing

• The implementation of hash tables is called Hashing.

• Hashing is a technique used for performing insertions and finds in constant average time.

• Efficient removal of items not required

Page 12: Hashing, Hashing Tables Chapter 8. Class Hierarchy

The General Idea

– Array of some fixed size, containing items.

Page 13: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Example

Page 14: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Keys and Hash Functions

• Each key is mapped into some number in the range 0 to TableSize-1 and placed in the appropriate cell.

• The mapping is called a hash function

Page 15: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Keys and Hash Functions

• Characteristics of a good hash function– Avoids collisions

– Spread keys evenly in the array

– Easy to compute

Page 16: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Avoid Collisions• Ideal situation

– Given a set of n<=M distinct keys {k1,k2,…,kn}, the set of hash values {h(k1),h(k2),…,h(kn)} contains no duplicates

• We can only try to reduce the likelihood of a collision using knowledge about the keys

• E.g. if we know the telephone numbers are all from the same district, so the district number will have little use in our hash function

Page 17: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Spreading Keys Evenly • We need to know the distribution of the

keys

• An equal number of keys should map into each array position

Page 18: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Ease of Computation• The running time of the hash function

should be O(1) (Jumping immediately to the desired record is a direct access approach, much like direct access of data on a disk)

Page 19: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Hashing Methods• We are dealing with integer values first,

K=Z

• The value of the hash function falls between 0 and M-1

Page 20: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Division Method• The simplest method of hashing an integer

• The division method of hashing

h(x) = x mod M.

Page 21: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Choice of M

• Generally, any M is good– we often choose M to be a prime number

Page 22: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Implementation

Unsigned int const M = 1031; // a prime

Unsigned int h(unsigned int x)

{ return x%M; }

Page 23: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Middle Square Method• Avoid division • Making use of the fact that computer does finite-

precision integer arithmetic– All arithmetic is done modulo W, where W=2w, w is

the word size of the computer

• M=2k, W=2w

• Meaning:– Multiply x by itself, then shift to the right k bits.

Page 24: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Implementation• unsigned int const k = 10; // M==1024 • unsigned int const w = bitsizeof (unsigned

int); • unsigned int h (unsigned int x) • { return (x * x) >> (w - k); }

Page 25: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Multiplication Method• We multiply the key by a

Page 26: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Implementationunsigned int const k = 10; // M==1024

unsigned int const w = bitsizeof (unsigned int);

unsigned int const a = 2654435769U;

unsigned int h (unsigned int x)

{ return (x * a) >> (w - k); }

}

Page 27: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Hash Tables

Page 28: Hashing, Hashing Tables Chapter 8. Class Hierarchy

HashTable Class Definition

Page 29: Hashing, Hashing Tables Chapter 8. Class Hierarchy

Separate Chaining