Hash Table
Hash Table
Thus, it becomes a data structure in which insertion and search operations are very fast
irrespective of the size of the data. Hash Table uses an array as a storage medium and uses
hash technique to generate an index where an element is to be inserted or is to be located
from.
Hashing
Hashing is a technique to convert a range of key values into a range of indexes of an array.
We're going to use modulo operator to get a range of key values. Consider an example of
hash table of size 20, and the following items are to be stored. Item are in the (key,value)
format.
Hash Function
DataItem
Define a data item having some data and key, based on which the search is to be conducted in a
hash table.
struct DataItem {
int data;
int key;
};
Hash Method
Define a hashing method to compute the hash code of the key of the data item.
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
return NULL;
Hash_table DSA by amruta_navale
}
What is a Hash function?
The hash function creates a mapping between key and value, this is done through the use of
mathematical formulas known as hash functions. The result of the hash function is referred to as
a hash value or hash. The hash value is a representation of the original string of characters but
usually smaller than the original.
For example: Consider an array as a Map where the key is the index and the value is the value at
that index. So for an array A if we have index i which will be treated as the key then we can find
the value by simply looking at the value at A[i].
simply looking up A[i].
Let’s see step by step approach to how to solve the above problem:
• Step 1: First draw the empty hash table which will have a possible range of hash
values from 0 to 4 according to the hash function provided.
Insert 12
Insert 22
• Step 4: The next key is 15. It will map to slot number 0 because 15%5=0.
Insert 25
Hash table
• Step 2: Now insert all the keys in the hash table one by one. The first key is 50. It will
map to slot number 0 because 50%5=0. So insert it into slot number 0.
where
• i is a non-negative integer that indicates a collision number,
• k = element/key which is being hashed
• n = hash table size.
Complexity of the Double hashing algorithm:
Time complexity: O(n)
What is Rehashing?
As the name suggests, rehashing means hashing again. Basically, when the load factor
increases to more than its predefined value (the default value of the load factor is 0.75), the
complexity increases. So to overcome this, the size of the array is increased (doubled) and all
the values are hashed again and stored in the new double-sized array to maintain a low load
factor and low complexity.
The hash function used is h=(key)%(total number of keys). Inside the hash table, each node
has three fields:
INSERT(key): The insert Operation inserts the key according to the hash value of that key if
that hash value in the table is empty otherwise the key is inserted in first empty place from
the bottom of the hash table and the address of this empty place is mapped in NEXT field
of the previous pointing node of the chain.(Explained in example below).
DELETE(Key): The key if present is deleted. Also if the node to be deleted contains the
address of another node in hash table then this address is mapped in the NEXT field of the
node pointing to the node which is to be deleted
SEARCH(key): Returns True if key is present, otherwise return False.
Hash_table DSA by amruta_navale
Example:
n = 10
Input : {20, 35, 16, 40, 45, 25, 32, 37, 22, 55}
Hash function
h(key) = key%10
Steps:
i)Initially empty hash table is created with all NEXT field initialised with NULL and h(key) values
ranging from 0-9.
ii)Let’s start with inserting 20, as h(20)=0 and 0 index is empty so we insert 20 at 0 index.
iii)Next element to be inserted is 35, h(35)=5 and 5th index empty so we insert 35 there.
iv)Next we have 16, h(16)=6 which is empty so 16 is inserted at 6 index value.
v)Now we have to insert 40, h(40)=0 which is already occupied so we search for the first empty
block from the bottom and insert it there i.e 9 index value.Also the address of this newly
inserted node(from address we mean index value of a node) i.e(9 )is initialised in the next field
of 0th index value node.
vi) To insert 45, h(45)=5 which is occupied so again we search for the empty block from the
bottom i.e 8 index value and map the address of this newly inserted node i.e(8) to the Next
field of 5th index value node i.e in the next field of key=35.
viii)To insert 32, h(32)=2, which is empty so insert 32 at 2nd index value.
ix)To insert 37, h(37)=7 which is occupied so search for the first free block from bottom which
is 4th index value. So insert 37 at 4th index value and copy the address of this node in next
field of 7th index value node.
x)To insert 22, h(22)=2 which is occupied so insert it at 3rd index value and map the address of
this node in next field of 2nd index value node.
xi)Finally, to insert 55 h(55)=5 which is occupied and the only empty space is 1st index value
so insert 55 there. Now again to map the address of this new node we have to follow the
chain starting from 5th index value node until we get NULL in next field i.e from 5th index-
>8th index->7th index->4th index which contains NULL in Next field, and we insert the address
of newly inserted node at 4th index value node.
Case 1:
To delete key=37, first search for 37. If it is present then simply
delete the data value and if the node contains any address in next
field and the node to be deleted i.e 37 is itself pointed by some
other node(i.e key=25) then copy that address in the next field of
37 to the next field of node pointing to 37(i.e key=25) and
initialize the NEXT field of key=37 as NULL again and erase the
key=37.
this technique suppose our hash function h(x) ranging from 0 to 6. So for more than 7 elements,
there must be some elements, that will be places inside the same room. For that we will create a
list to store them accordingly. In each time we will add at the beginning of the list to perform
insertion in O(1) time
Let us see the following example to get better idea. If we have some elements like {15, 47, 23,
34, 85, 97, 65, 89, 70}. And our hash function is h(x) = x mod 7.
Keys are stored inside the hash All the keys are stored only inside
table as well as outside the hash the hash table.
table. No key is present outside the
hash table.