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
Prev Previous commit
Next Next commit
fixes: better hash function, improved comments
  • Loading branch information
ashokdey committed Nov 30, 2019
commit 44c158a6e429e879b03e7ebacae2139f22c10dcd
37 changes: 21 additions & 16 deletions src/_DataStructures_/HashTable/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class HashTable {
// and help to resize when the table is half filled
this.size = 0;
// threshold (let it be 70%)
this.threshold = 0.7;
this.threshold = 0.8;
// the main bucket
this.bucket = new Array(this.slot);

Expand All @@ -21,15 +21,17 @@ class HashTable {
const stringKey = String(key);

let index = 0;
const PRIME = 1801;
const PRIME_MULTIPLIER = 1801; // Random prime number
const PRIME_ADDER = 2029; // Random prime number

// loop till the length of the key or mamx 100
// loop till the length of the key or max 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;
index = (index * PRIME_MULTIPLIER + value) % this.bucket.length;
index = (index + PRIME_ADDER) % this.bucket.length;
}
return index;
}
Expand All @@ -47,7 +49,6 @@ class HashTable {

for (let i = 0; i < oldSlot; i += 1) {
if (oldBucket[i]) {
// get all the nodes associated here
let head = oldBucket[i];

while (head !== null) {
Expand All @@ -64,8 +65,8 @@ class HashTable {

_push(index, value) {
/**
* Util to add a SSL to the index in case of more than once
* value for the same key exixts
* Utility to add a SLL to the index in case of more than one
* key hashes to the same index
*/
const node = new HashEntry(value);
if (!this.bucket[index]) {
Expand All @@ -87,7 +88,7 @@ class HashTable {

_values(index, key) {
/**
* Util to return the values as an array
* Utility to return the values as an array for a given key
*/
const res = [];
let head = this.bucket[index];
Expand All @@ -111,7 +112,7 @@ class HashTable {
* calculate the load factor, if it's greater than threshold
* resize the hash table
*/
const loadFactor = (this.size / this.slot).toFixed(1);
const loadFactor = Number((this.size / this.slot).toFixed(1));
if (loadFactor > this.threshold) {
// console.log('Resizing hash table');
// eslint-disable-next-line no-underscore-dangle
Expand All @@ -120,7 +121,7 @@ class HashTable {
}

get(key) {
// get the index
// get the index for the given key
// eslint-disable-next-line no-underscore-dangle
const index = this._hash(key);
if (!this.bucket[index]) return null;
Expand All @@ -137,14 +138,18 @@ class HashTable {
}
}

// const ht = new HashTable(5);
// 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');
const ht = new HashTable(5);
console.log('HT slots = ', ht.slot);
ht.set('maroon', 'I maroon');
ht.set('hello', 'I am a new value');
console.log(ht.bucket);
ht.set('hell', 'Bad value');
ht.set('hello', 'I am a yet another value');
console.log('HT slots = ', ht.slot);
ht.set('yellow', 'I am yellow');

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

module.exports = HashTable;