Sunteți pe pagina 1din 10

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 Record Index Record

00 31300 00 31300

01 49001 01 49001

02 52202 02 52202
Key = 12704 03 Empty 03 12704

04 12704 04 65606
Hash Fun.
Key % 100 05 Empty 05

06 65606 06

… … … …

… … … …

98 … 98 …

99 99

Hashed Insertion Linear Insertion

(C) GOYANI MAHESH 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 Index Actual Scene
Insertion

00

01 14001
14001 02
00104 03 50003
50003 04 00104
77003 05 77003
42504 06 42504
33099 07

08

..

..

99 33099

(C) GOYANI MAHESH 6


DELETION

Index Actual Scene

00

01 14001
 Linear probing solves one problem but 02
generates another 03 50003
04 00104
05 77003
06 42504
07

08

..

..

99 33099

(C) GOYANI MAHESH 7


CLUSTERING

Index Actual Scene

00
 Record with key 03,04,05,06 and 07 would
01 14001
be inserted at array room 7, that is array room
7 is five times as likely as array room 8 to be 02
filled. 03 50003
 Clustering results in to inconsistent 04 00104
efficiency of insertion and retrieval. 05 77003
06 42504
07

08

..

..

99 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 RECORD

00  10100  10100

01

02

03  33303  11103  22203

04

05

06  33306  11106  22206

07

…..

99

(C) GOYANI MAHESH 10

S-ar putea să vă placă și