Hash Table

A hash table is a data structure that stores data associatively. Data is kept in an array format in a hash table, with each data value having its own unique index value. When we know the index of the needed data, we may get it extremely quickly.

As a result, it becomes a data structure in which insertion and search operations are extremely quick, regardless of the quantity of the data. Hash Tables use an array as a storage medium and employ the hash technique to establish an index from which an element is to be inserted or located.

Hashing

Hashing is a technique for converting a set of key values into a set of array indexes. To get a range of key values, we’ll utilize the modulo operator. Consider a hash table of size 20 with the following elements to be kept. Item is formatted as (key, value).

Hash Function
  • (1,20)
  • (2,70)
  • (42,80)
  • (4,25)
  • (12,44)
  • (14,32)
  • (17,11)
  • (13,78)
  • (37,98)
Sr.No.KeyHashArray Index
111 % 20 = 11
222 % 20 = 22
34242 % 20 = 22
444 % 20 = 44
51212 % 20 = 1212
61414 % 20 = 1414
71717 % 20 = 1717
81313 % 20 = 1313
93737 % 20 = 1717

Linear Probing

As we can see, the hashing approach can be utilised to generate an array index that has already been used. In this situation, we can look into the following cell until we reach an empty cell to discover the next empty spot in the array. This method is known as linear probing.

Sr.No.KeyHashArray IndexAfter Linear Probing, Array Index
111 % 20 = 111
222 % 20 = 222
34242 % 20 = 223
444 % 20 = 444
51212 % 20 = 121212
61414 % 20 = 141414
71717 % 20 = 171717
81313 % 20 = 131313
93737 % 20 = 171718

Basic Operations

Following are the basic primary operations of a hash table.

  • Search − Searches an element in a hash table.
  • Insert − inserts an element in a hash table.
  • delete − Deletes an element from a hash table.

Data Item

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.

int hashCode(int key){
   return key % SIZE;
}

Search Operation

When searching for an element, compute the hash code of the key supplied and locate the element in the array using that hash code as an index. If the element is not located at the computed hash code, use linear probing to advance it.

Insert Operation

When inserting an element, compute the hash code of the key supplied and identify the index in the array using that hash code as an index. If an element is located at the computed hash code, use linear probing to find an empty position.

Delete Operation

Whenever an element needs to be deleted, compute the hash code of the key supplied and find the index in the array using that hash code as an index. If an element is not located at the computed hash code, use linear probing to advance it. When discovered, place a dummy item there to maintain the hash table’s efficiency.

0

1 thought on “Hash Table”

Leave a Comment

Your email address will not be published. Required fields are marked *

four × 5 =