Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d221391

Browse files
committed
update: initial skeleton for Hash Table
1 parent a47787d commit d221391

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class HashEntry {
2+
constructor(key, value) {
3+
this.key = key;
4+
this.value = value;
5+
this.next = null;
6+
}
7+
}
8+
9+
module.exports = HashEntry;
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
const { Node } = require('../LinkedList');
2+
3+
class HashTable {
4+
constructor(slots) {
5+
// init with a default set of slots
6+
this.slot = slots || 17;
7+
// size to hold the current size
8+
// and help to resize when the table is half filled
9+
this.size = 0;
10+
// the main bucket
11+
this.bucket = new Array(this.slot);
12+
13+
// fill the bucket with null
14+
for (let i = 0; i < this.slot; i += 1) this.bucket[i] = null;
15+
}
16+
17+
_hash(key) {
18+
// convert the key to String;
19+
const stringKey = String(key);
20+
21+
let index = 0;
22+
const PRIME = 1801;
23+
24+
// loop till the length of the key or mamx 100
25+
const loopTill = Math.min(stringKey.length, 100);
26+
27+
for (let i = 0; i < loopTill; i += 1) {
28+
const char = stringKey[i];
29+
const value = char.charCodeAt(0) - 96;
30+
index = (index + PRIME + value) % this.bucket.length;
31+
}
32+
return index;
33+
}
34+
35+
_push(index, value) {
36+
/**
37+
* Util to add a SSL to the index in case of more than once
38+
* value for the same key exixts
39+
*/
40+
const node = new Node(value);
41+
if (!this.bucket[index]) {
42+
this.bucket[index] = node;
43+
this.size += 1;
44+
return index;
45+
}
46+
47+
let head = this.bucket[index];
48+
49+
// traverse to the end
50+
while (head.next !== null) {
51+
head = head.next;
52+
}
53+
head.next = node;
54+
this.size += 1;
55+
return index;
56+
}
57+
58+
_values(index, key) {
59+
/**
60+
* Util to return the values as an array
61+
*/
62+
const res = [];
63+
let head = this.bucket[index];
64+
while (head !== null) {
65+
if (head.data.key === key) {
66+
res.push(head.data.value);
67+
}
68+
head = head.next;
69+
}
70+
return res;
71+
}
72+
73+
set(key, value) {
74+
// eslint-disable-next-line no-underscore-dangle
75+
const index = this._hash(key);
76+
// storing value as an key-value pair
77+
// eslint-disable-next-line no-underscore-dangle
78+
this._push(index, { key, value });
79+
}
80+
81+
get(key) {
82+
// get the index
83+
// eslint-disable-next-line no-underscore-dangle
84+
const index = this._hash(key);
85+
if (!this.bucket[index]) return null;
86+
// eslint-disable-next-line no-underscore-dangle
87+
return this._values(index, key);
88+
}
89+
90+
getSize() {
91+
return this.size;
92+
}
93+
94+
isEmpty() {
95+
return this.getSize() === 0;
96+
}
97+
}
98+
99+
// const ht = new HashTable();
100+
// ht.set('hello', 'I am a new value');
101+
// ht.set('hello', 'I am a yet another value');
102+
// ht.set('maroon', 'I maroon');
103+
// ht.set('yellow', 'I am yellow');
104+
105+
// console.log(ht.get('hello'));
106+
// console.log(ht.get('maroon'));
107+
// console.log(ht.bucket);
108+
109+
module.exports = HashTable;

‎src/_DataStructures_/LinkedList/index.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
// do not change the node class, you never know how many things it caan break! :)
1+
// do not change the node class, you never know how many things it caan break! :)
22
class Node {
33
constructor(data, next) {
44
this.data = data;
5-
this.next = next;
5+
this.next = next||null;
66
}
77
}
88

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /