If you observe carefully, in a bala
If you observe carefully, in a bala
A hash function is a function that takes an input (or “message”) and returns a
fixed-size string of characters, which is typically a “digest” that is unique to
the unique values of the input.
Collision H
andling: Since a hash function gets us a small number for a key which is a big
integer or string, there is the possibility that two keys result in the same value.
The situation where a newly inserted key maps to an already occupied slot in the
hash table is called collision and must be handled using some collision handling
technique. Following are the ways to handle collisions:
Chaining: The idea is to make each cell of the hash table point to a linked list of
records that have the same hash function value. Chaining is simple but requires
additional memory outside the table.
Open Addressing: In open addressing, all elements are stored in the hash table
itself. Each table entry contains either a record or NIL. When searching for an
element, we one by one examine table slots until the desired element is found or it
is clear that the element is not in the table.
Hashing seems better than BST for all the operations. But in hashing, elements are
unordered and in BST elements are stored in an ordered manner. Also, BST is easy to
implement but hash functions can sometimes be very complex to generate. In BST, we
can also efficiently find floor and ceil of values.
Example: Hashing can be used to remove duplicates from a set of elements. Can also
be used to find the frequency of all items. For example, in web browsers, we can
check visited URLs using hashing. In firewalls, we can use hashing to detect spam.
We need to hash IP addresses. Hashing can be used in any situation where want
search() insert() and delete() in O(1) time.
This article is contributed by Abhiraj Smit. Please write comments if you find
anything incorrect, or you want to share more information about the topic discussed
above.