# DATA STRUCTURES

MAHESH GOYANI
MAHATMA GANDHI INSTITUE OF TECHNICAL EDUCATION & RESEARCH CENTER
mgoyani@rediffmail.com

(C) GOYANI MAHESH

1

HASHING
(C) GOYANI MAHESH 2

TERMINOLOGY 

Hashing provides O(1) constant time complexity for searching.  Generally, array index works as an searching index if there is one to one correspondence between key and element.  In practice, it is very tough to maintain 1-1 relationship between key and element if data is too large, like ID = 12345.

(C) GOYANI MAHESH

3

COMPARISION

Index
00 01 02

Record
31300 49001 52202 Empty 12704 Empty 65606

Index
00 01 02 03 04 05 06

Record
31300 49001 52202 12704 65606

Key = 12704 Hash Fun. Key % 100

03 04 05 06

98 99

98 99

Hashed Insertion
(C) GOYANI MAHESH

Linear Insertion
4

COLLISION 

If two number appears with the same hash value, like 12345 and 23645, than both has to the same location ARRAY [45]. This is known as collision.  Collision is one of the problem while designing the good hash function.  Minimization of collision is also too difficult.

(C) GOYANI MAHESH

5

LINEAR PROBING

A simple approach to resolving collisions is to store the colliding record in to next available record.
Order of Insertion Index Actual Scene

00 01

14001 50003 00104 77003 42504

14001 00104 50003 77003 42504 33099

02 03 04 05 06 07 08 .. .. 99

33099
6

(C) GOYANI MAHESH

DELETION

Index

Actual Scene

00 01

14001 50003 00104 77003 42504 

Linear probing solves one problem but generates another

02 03 04 05 06 07 08 .. .. 99

33099

(C) GOYANI MAHESH

7

CLUSTERING

Index

Actual Scene

00 

Record with key 03,04,05,06 and 07 would be inserted at array room 7, that is array room 7 is five times as likely as array room 8 to be filled.  Clustering results in to inconsistent efficiency of insertion and retrieval.

01 02 03 04 05 06 07 08 .. .. 99

14001 50003 00104 77003 42504

33099

(C) GOYANI MAHESH

8

REHASHING 

If the hash function produces a collision, the hash value is used as the input to rehash function.  In Linear probing : (hash value + 1) % array size  For rehashing with linear probing: (Hash value + constant) % array size  constant and Array Size should be relatively prime, so that it can cover all the odd and even digit  Quadratic Probing : (Hash Value + i2) % Array Size  Pseudo random Generator probing : (Hash Value + random()) % Array Size

(C) GOYANI MAHESH

9

BUCKET & CHAINING

INDEX 00 01 02 03 04 05 06 07 .. 99

RECORD  10100  10100 

33303 

11103 

22203 

33306 

11106 

22206

(C) GOYANI MAHESH

10