xxxxxxxxxx
makeHash(key) {
const asciicodeSum = key.split("").reduce((acc, cur) => {
return acc + cur.charCodeAt(0);
}, 0);
const multiPrime = asciicodeSum * 599;
const theIndex = multiPrime % this.size;
console.log(theIndex)
return theIndex;
}
xxxxxxxxxx
In computing, a hash table (hash map) is a data structure that implements an
associative array abstract data type, a structure that can map keys to values.
A hash table uses a hash function to compute an index, also called a hash code,
into an array of buckets or slots, from which the desired value can be found.
xxxxxxxxxx
// Implementing hash table in C++
#include <iostream>
#include <list>
using namespace std;
class HashTable
{
int capacity;
list<int> *table;
public:
HashTable(int V);
void insertItem(int key, int data);
void deleteItem(int key);
int checkPrime(int n)
{
int i;
if (n == 1 || n == 0)
{
return 0;
}
for (i = 2; i < n / 2; i++)
{
if (n % i == 0)
{
return 0;
}
}
return 1;
}
int getPrime(int n)
{
if (n % 2 == 0)
{
n++;
}
while (!checkPrime(n))
{
n += 2;
}
return n;
}
int hashFunction(int key)
{
return (key % capacity);
}
void displayHash();
};
HashTable::HashTable(int c)
{
int size = getPrime(c);
this->capacity = size;
table = new list<int>[capacity];
}
void HashTable::insertItem(int key, int data)
{
int index = hashFunction(key);
table[index].push_back(data);
}
void HashTable::deleteItem(int key)
{
int index = hashFunction(key);
list<int>::iterator i;
for (i = table[index].begin();
i != table[index].end(); i++)
{
if (*i == key)
break;
}
if (i != table[index].end())
table[index].erase(i);
}
void HashTable::displayHash()
{
for (int i = 0; i < capacity; i++)
{
cout << "table[" << i << "]";
for (auto x : table[i])
cout << " --> " << x;
cout << endl;
}
}
int main()
{
int key[] = {231, 321, 212, 321, 433, 262};
int data[] = {123, 432, 523, 43, 423, 111};
int size = sizeof(key) / sizeof(key[0]);
HashTable h(size);
for (int i = 0; i < size; i++)
h.insertItem(key[i], data[i]);
h.deleteItem(12);
h.displayHash();
}
xxxxxxxxxx
//The hash table is famous for it's constant time complexity
package main
import (
"fmt"
)
const aSize = 5
//The structure of the hash table is an array
type HashTable struct {
array [aSize]*Bucket
}
//The bucket is the linked list appeded to the hash table
type Bucket struct {
head *BucketNode
}
//The keys will be stored in the bucket and will be a bucket node
type BucketNode struct {
key string
next *BucketNode
}
//The insert function for the hash table (array)
func (h *HashTable) Insert(key string) {
index := hash(key)
h.array[index].InsertBucket(key)
}
//The search function for the hash table (array)
func (h *HashTable) Search(key string) bool{
index := hash(key)
return h.array[index].SearchBucket(key)
}
//The delete function for the hash table (array)
func (h *HashTable) Delete(key string) {
index := hash(key)
h.array[index].DeleteBucket(key)
}
//These three functions edit the bucket
//The insert function for the bucket(linked keys)
func (b *Bucket)InsertBucket(k string) {
if !b.SearchBucket(k){
newNode := &BucketNode{key:k}
newNode.next = b.head
b.head = newNode
}else {
fmt.Println(k, "already exist")
}
}
//The search function for the bucket (linked keys)
func (b *Bucket)SearchBucket(k string) bool {
cNode := b.head
for cNode != nil {
if cNode.key == k {
return true
}
cNode = cNode.next
}
return false
}
//The delete function for the bucket (linked keys)
func (b *Bucket)DeleteBucket(k string) {
if b.head.key == k{
b.head = b.head.next
return
}
pNode := b.head
for pNode != nil{
if pNode.next.key == k{
pNode.next =pNode.next.next
}
pNode = pNode.next
}
}
//This function turns the string to ascii int and divided by the size
//of the array to get it's index
func hash(key string) int {
r := 0
for _, v := range key {
r+=int(v)
}
return r % aSize
}
//This function creates a hash table
func Create() *HashTable {
r := &HashTable{}
for i := range r.array {
r.array[i] = &Bucket{}
}
return r
}
func main() {
myHashTable := Create()//Creating a hash table
keys := []string{
"mike",
"sam",
"ben",
"ham",
"jake",
}
for _, v := range keys {//Inserting keys to the hash table
myHashTable.Insert(v)
}
fmt.Println(myHashTable.Search("mike"))//Searching for mike
myHashTable.Delete("mike")//Deleting mike
fmt.Println(myHashTable.Search("mike"))//Searching again for mike
}
xxxxxxxxxx
//Java Hashtable
//--------------
import java.io.*;
import java.util.*;
class AddElementsToHashtable {
public static void main(String args[])
{
Hashtable<Integer, String> h1 = new Hashtable<>();
// Initialization of a Hashtable using Generics
Hashtable<Integer, String> h2 = new Hashtable<Integer, String>();
// Inserting the Elements using put() method
// ------------
ht1.put(1, "Chris");
ht1.put(2, "Tony");
ht1.put(3, "Peter");
ht2.put(4, "Steven");
// Print mappings to the console (Output)
System.out.println("Mappings of Hash Table 1 : " + h1);
System.out.println("Mappings of Hash Table 2 : " + h2);
}
}