Skip to content

added Hash Table #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Dec 1, 2019
Merged
Next Next commit
update: initial skeleton for Hash Table
  • Loading branch information
ashokdey committed Nov 29, 2019
commit d221391f6d83160846fa96833e79d0250dc19d23
9 changes: 9 additions & 0 deletions src/_DataStructures_/HashTable/HashEntry.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class HashEntry {
constructor(key, value) {
this.key = key;
this.value = value;
this.next = null;
}
}

module.exports = HashEntry;
109 changes: 109 additions & 0 deletions src/_DataStructures_/HashTable/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const { Node } = require('../LinkedList');

class HashTable {
constructor(slots) {
// init with a default set of slots
this.slot = slots || 17;
// size to hold the current size
// and help to resize when the table is half filled
this.size = 0;
// the main bucket
this.bucket = new Array(this.slot);

// fill the bucket with null
for (let i = 0; i < this.slot; i += 1) this.bucket[i] = null;
}

_hash(key) {
// convert the key to String;
const stringKey = String(key);

let index = 0;
const PRIME = 1801;

// loop till the length of the key or mamx 100
const loopTill = Math.min(stringKey.length, 100);

for (let i = 0; i < loopTill; i += 1) {
const char = stringKey[i];
const value = char.charCodeAt(0) - 96;
index = (index + PRIME + value) % this.bucket.length;
}
return index;
}

_push(index, value) {
/**
* Util to add a SSL to the index in case of more than once
* value for the same key exixts
*/
const node = new Node(value);
if (!this.bucket[index]) {
this.bucket[index] = node;
this.size += 1;
return index;
}

let head = this.bucket[index];

// traverse to the end
while (head.next !== null) {
head = head.next;
}
head.next = node;
this.size += 1;
return index;
}

_values(index, key) {
/**
* Util to return the values as an array
*/
const res = [];
let head = this.bucket[index];
while (head !== null) {
if (head.data.key === key) {
res.push(head.data.value);
}
head = head.next;
}
return res;
}

set(key, value) {
// eslint-disable-next-line no-underscore-dangle
const index = this._hash(key);
// storing value as an key-value pair
// eslint-disable-next-line no-underscore-dangle
this._push(index, { key, value });
}

get(key) {
// get the index
// eslint-disable-next-line no-underscore-dangle
const index = this._hash(key);
if (!this.bucket[index]) return null;
// eslint-disable-next-line no-underscore-dangle
return this._values(index, key);
}

getSize() {
return this.size;
}

isEmpty() {
return this.getSize() === 0;
}
}

// const ht = new HashTable();
// ht.set('hello', 'I am a new value');
// ht.set('hello', 'I am a yet another value');
// ht.set('maroon', 'I maroon');
// ht.set('yellow', 'I am yellow');

// console.log(ht.get('hello'));
// console.log(ht.get('maroon'));
// console.log(ht.bucket);

module.exports = HashTable;
4 changes: 2 additions & 2 deletions src/_DataStructures_/LinkedList/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// do not change the node class, you never know how many things it caan break! :)
// do not change the node class, you never know how many things it caan break! :)
class Node {
constructor(data, next) {
this.data = data;
this.next = next;
this.next = next || null;
}
}

Expand Down