Summary of Hashing in Data Structure

What is Hashing?

  • Hashing is the process of mapping large amount of data item to smaller table with the help of hashing function.
  • Hashing is also known as Hashing Algorithm or Message Digest Function.
  • It is a technique to convert a range of key values into a range of indexes of an array.
  • It is used to facilitate the next level searching method when compared with the linear or binary search.
  • Hashing allows to update and retrieve any data entry in a constant time O(1).
  • Constant time O(1) means the operation does not depend on the size of the data.
  • Hashing is used with a database to enable items to be retrieved more quickly.
  • It is used in the encryption and decryption of digital signatures.

What is Hash Function?

  • A fixed process converts a key to a hash key is known as a Hash Function.
  • This function takes a key and maps it to a value of a certain length which is called a Hash value or Hash.
  • Hash value represents the original string of characters, but it is normally smaller than the original.
  • It transfers the digital signature and then both hash value and signature are sent to the receiver. Receiver uses the same hash function to generate the hash value and then compares it to that received with the message.
  • If the hash values are same, the message is transmitted without errors.

What is Hash Table?

  • Hash table or hash map is a data structure used to store key-value pairs.
  • It is a collection of items stored to make it easy to find them later.
  • It uses a hash function to compute an index into an array of buckets or slots from which the desired value can be found.
  • It is an array of list where each list is known as bucket.
  • It contains value based on the key.
  • Hash table is used to implement the map interface and extends Dictionary class.
  • Hash table is synchronized and contains only unique elements.


struct DataItem {
   int data;   
   int key;
};

struct DataItem* hashArray[SIZE]; 
struct DataItem* dummyItem;
struct DataItem* item;

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

struct DataItem *search(int key) {
   //get the hash 
   int hashIndex = hashCode(key);  
 
   //move in array until an empty 
   while(hashArray[hashIndex] != NULL) {
 
      if(hashArray[hashIndex]->key == key)
         return hashArray[hashIndex]; 
   
      //go to next cell
      ++hashIndex;
  
      //wrap around the table
      hashIndex %= SIZE;
   }        
 
   return NULL;        
}

void insert(int key,int data) {

   struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem));
   item->data = data;  
   item->key = key;

   //get the hash 
   int hashIndex = hashCode(key);

   //move in array until an empty or deleted cell
   while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1) {
      //go to next cell
      ++hashIndex;
  
      //wrap around the table
      hashIndex %= SIZE;
   }
 
   hashArray[hashIndex] = item;
}

struct DataItem* delete(struct DataItem* item) {
   int key = item->key;

   //get the hash 
   int hashIndex = hashCode(key);

   //move in array until an empty
   while(hashArray[hashIndex] != NULL) {
 
      if(hashArray[hashIndex]->key == key) {
         struct DataItem* temp = hashArray[hashIndex]; 
   
         //assign a dummy item at deleted position
         hashArray[hashIndex] = dummyItem; 
         return temp;
      }
  
      //go to next cell
      ++hashIndex;
  
      //wrap around the table
      hashIndex %= SIZE;
   }      
 
   return NULL;        
}

void display() {
   int i = 0;
 
   for(i = 0; i<SIZE; i++) {
 
      if(hashArray[i] != NULL)
         printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);
      else
         printf(" ~~ ");
   }
 
   printf("\n");
}

int main() {
   dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem));
   dummyItem->data = -1;  
   dummyItem->key = -1; 

   insert(1, 20);
   insert(2, 70);
   insert(42, 80);
   insert(4, 25);
   insert(12, 44);
   insert(14, 32);
   insert(17, 11);
   insert(13, 78);
   insert(37, 97);

   display();
   item = search(37);

   if(item != NULL) {
      printf("Element found: %d\n", item->data);
   } else {
      printf("Element not found\n");
   }

   delete(item);
   item = search(37);

   if(item != NULL) {
      printf("Element found: %d\n", item->data);
   } else {
      printf("Element not found\n");
   }
}

source:


Comments

Popular posts from this blog

Pengertian kelas besar tentang head, curr, tail

AVL TREE

Binary Search Tree(BNS)