Applications, Advantages and Disadvantages of Hash Data Structure
Hash or Hash table is a data structure that maps keys to values using a special function called a hash function. Hash stores the data in an associative manner. It stores data in an array where each data value has its own unique index. Hashing provides secure access and retrieval of the data.
Collision Resolution: Collision resolution in hash can be done by two methods:
- Open addressing and
- Closed addressing.
Open Addressing: Open addressing collision resolution technique involves generating a location for storing or searching the data called probe. It can be done in the following ways:
- Linear Probing: If there is a collision at i then we use the hash function – H(k, i ) = [H'(k) + i ] % m
where, i is the index, m is the size of hash table H( k, i ) and H'( k ) are hash functions.
- Quadratic Probing: If there is a collision at i then we use the hash function – H(k, i ) = [H'(k) + c1 * i + c2 * i2 ] % m
where, i is the index, m is the size of hash table H(k, i ) and H'( k ) are hash functions, c1 and c2 are constants.
- Double Hashing: If there is a collision at i then we use the hash function – H(k, i ) = [H1(k, i) + i * H2(k) ] % m
where, i is the index, m is the size of hash table H(k, i ), H1( k) = k % m and H2(k) = k % m’ are hash functions.
Closed addressing collision resolution technique involves chaining. Chaining in the hashing involves both array and linked list. In this method, we generate a probe with the help of the hash function and link the keys to the respective index one after the other in the same index. Hence, resolving the collision.
Applications of Hash:
- Hash is used in databases for indexing.
- Hash is used in disk based data structures.
Real-Time Applications of Hash:
- Hash is used for cache mapping for fast access of the data.
- Hash can be used for password verification.
- Hash is used in cryptography as a message digest.
Advantages of Hash:
- Hash provides better synchronization than other data structures.
- Hash tables are more efficient than search trees or other data structures.
- Hash provides constant time for searching, insertion and deletion operations on average.
Disadvantages of Hash:
- Hash is inefficient when there are many collisions.
- Hash collisions are practically not be avoided for large set of possible keys.
- Hash does not allow null values.