×

Hashing

Hashing: Hashing is a process in which a large amount of data is mapped to a small table with the help of hashing function. It is a searching technique.

Hash table

We can understand the hash table better based on the following points:

  1. In a data structure, the hash table is used to store key-value pairs.
  2. The data items are stored in an array format in it, where each data value has its unique index value.
  3. It is a collection of stored data items that you can easily search later.
  4. In this, the hash function is used to compute the index of the array, from which you can find your desired value.
  5. It has a list of the array where each list is called a bucket.
  6. It contains the value based on the key.
  7. The hash table is used to implement the map interface and extend the dictionary class.
  8. The hash table is a synchronized table, and it contains unique elements only.
Hashing
  • The figure above shows the hash table whose size is n = 10. Each position of the hash table is called a slot. This hash table has not contained any item, so each slot is empty.

Basic operations

There is the following type of basic operation in the hash table:

  1. Search operation
  2. Delete operation
  3. Insert operation

Search operation: The search operation is used to search a particular value in a hash table.

Delete operation: The delete operation is used to delete a particular value in a hash table.

Insert operation: The Insert operation is used to add particular value in a hash table.

Hash function

The hash function is a type of function that is applied to a key from which an integer is obtained. This integer is used as the address of the hash table. This integer is called a hash key.

Type of hash functions

There are various types of hash functions in the data structure:

  1. Mid Square Hash Function
  2. Division Hash Function

Division hash function

In this function, the hash function depends on the remainder of the division. If the list size is a prime number, there is less collision.

The formula of division hash function is h(k) = k mod n

Where k is key, and n is list size.

For example: Suppose we have this record (101, 103, 107, 109), and the table size is 10.

Solution: Record is (101, 103, 107, 109)

                Table size is 10.

                Put value in the formula h(k) = k mod n

                                        1 = 101 mod 10

                                        3 = 103 mod 10

                                        7 = 107 mod 10

                                        9 = 109 mod 10

Hashing

Mid square hash function

In this function, firstly hash function key is squared, and then the middle part of the square is selected as the index.

For example: Suppose we have this record 96.

                        96 = 962 = 9216                              // middle part of the square is index address    

                        The address of index is 21 

Characteristics of a good hash function

To get a good hashing mechanism, we need a good hash function. Therefore, we describe below the characteristic of a good hash function:

  1. The hash function should generate different hash values ??for the same type of string.
  2. A good hash function reduces the chance of collisions.
  3. It should be easy to compute.
  4. The hash function is perfect when it uses all input data.
  5. The hash function should generate such keys that are easy to distribute.

Related Topics

Vertical Order Traversal of Binary Tree

Implementation #include <iostream> #include <vector> #include <map> using namespace std; // representing the primary model of a binary tree node. struct _nod { int ky; _nod *Lft, *Rt; }; // establishing a new function representing the new binary tree node. struct _nod*...

5 minutes read.

Given a Perfect Binary Tree, Reverse Alternate Levels

Implementation //writing a program in C++ language to see how to approach it. #include <bits/stdc++.h> using namespace std; // creating a tree node. struct Nod { char ky; struct Nod *Lft, *Rt; }; // creating a new utility function...

9 minutes read.

Digital Search Tree in Data Structures

What is a digital search Tree in Data Structures? The Digital search tree is known for its application and diversity in the way it has impacted our world in the field...

3 minutes read.

Asynchronous advantage actor-critic (A3C) Algorithm

The Asynchronous advantage actor-critic (A3C) Algorithm is one of the latest algorithms developed by the Artificial Intelligence division, Deep Mind at Google. It is used for the Deep Reinforcement Learning...

3 minutes read.

Serialize and Deserialize a Binary Tree

Implementation // Writing a C++ program to check the serialization and deserialization of binary tree.   #include <iosstream> /* A binary tree node contains a key and a pointer to the left and right...

4 minutes read.

Blowfish algorithm

The Blowfish algorithm is the very first encryption algorithm which is symmetric. It was firstly used as an alternate algorithm for the DES algorithm. It was designed by Bruce Steiner...

3 minutes read.

Remove duplicates from an unsorted Linked List

Remove duplicates from an unsorted Linked List This article will explain how we can remove duplicates from unsorted linked lists. Here we have given an unsorted singly linked list and will...

3 minutes read.

Binary tree deletion

This article will discuss the deletion operation's implementation in the binary tree. The deletion operation helps us eliminate an element from the tree. Implementation #include <bits/stdc++.h> using namespace std; /* A binary tree node...

4 minutes read.

Winner tree in Data Structures

Tree Data structure A tree is a hierarchical and non-linear data structure with nodes. Each node in the Tree contains a message value and stores the name passed to another ("child")...

6 minutes read.

Data Structures Algorithms

What is an Algorithm? An algorithm is a sequence of steps used to complete a job or get a desired result. It is similar to programming building elements that let cell...

4 minutes read.

Sum of Nodes in a Binary Tree

In this article, we will see the sample problems that will help us understand the concept and summation of all the nodes in the binary tree. Implementation /* creating a program that...

4 minutes read.

Queue Implementation using stacks Data Structure

Queue Implementation using stacks In this problem, we have stack data structure which supports only push() and pop() operations. We are required to implement a queue data structure using the instances...

4 minutes read.

Application of 2D array - Sparse Matrix

2D Arrays Application - Sparse Matrix A matrix is a two-dimensional data item consisting of m rows and n columns, with a total of m x n values. A sparse matrix...

7 minutes read.

Find Number of Minimum Insertion to Make a String Palindrome

You have been given a string. You have to find out the number of minimum insertions to make this string palindrome. The string will contain only lower case alphabets. Note:What is...

4 minutes read.

Queue Data Structure

Queue in DS: The queue is a non-primitive and linear data structure. It works on the principle of FIFO (First In First Out). That is, the element that is added...

4 minutes read.

Extended Binary Tree

A form of binary tree known as an extended binary tree replaces all of the original tree's null subtrees with special nodes known as external nodes, while the remaining nodes...

4 minutes read.

Selection Sort

In each iteration of the selection sort algorithm, the smallest item from an unsorted list is chosen and placed at the top of the unsorted list. Algorithm of Selection Sorting In order...

3 minutes read.

Merge Conflicts and ways to handle them

Merge Conflicts Whenever dealing with the Git merge operations, conflicts will be the frequently occurred. When more than two developers work on the same file on different systems using Git, they...

4 minutes read.

Extended Binary Tree

An extended binary tree is a binary tree in which all the NILL subtrees present mainly in the original trees are exchanged with the special nodes that are primarily known...

3 minutes read.

Bubble Sort vs Merge Sort

In this article, we are going to compare two sorting techniques, Bubble sort and Merge Sort. In starting, we will first discuss the idea of sorting an array using bubble...

7 minutes read.