Skip to content

Commit d05deeb

Browse files
committed
Improving the documentation - How hash table works
1 parent 40dbe92 commit d05deeb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Data-Structures/HashTable/HashTable.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,95 @@
11
export default class HashTable {
22
constructor(limit = 100) {
3+
// Initialize the storage and limit variables
34
this.storage = new Array(limit)
45
this.limit = limit
56
}
67

8+
// Hash function:
79
_hash(key, max) {
10+
// Initialize the hash variable to 0
811
let hash = 0
12+
// Iterate through the key and add the character code at each iteration to the hash variable
913
for (let i = 0; i < key.length; i++) hash += key.charCodeAt(i)
14+
// Return the hash modulo the max
1015
return hash % max
1116
}
1217

18+
// Insert a key-value pair into the hash table
1319
set(key, value) {
20+
// Hash the key
1421
const index = this._hash(key, this.limit)
22+
// If the index is empty, insert the key-value pair and create a bucket
1523
if (this.storage[index] === undefined) this.storage[index] = [[key, value]]
1624
else {
25+
// If the index is not empty, iterate through the bucket (collision handling)
1726
let inserted = false
1827
for (let i = 0; i < this.storage[index].length; i++) {
1928
if (this.storage[index][i][0] === key) {
29+
// If the key exists, update the value
2030
this.storage[index][i][1] = value
2131
inserted = true
2232
}
2333
}
34+
// If the key does not exist, insert the key-value pair
2435
if (inserted === false) this.storage[index].push([key, value])
2536
}
2637
}
2738

2839
// Get a value from the hash table
2940
get(key) {
41+
// Hash the key
3042
const index = this._hash(key, this.limit)
43+
// If the index is empty, return undefined
3144
if (this.storage[index] === undefined) return undefined
3245
else {
46+
// If the index is not empty, iterate through the bucket
3347
for (let i = 0; i < this.storage[index].length; i++) {
48+
// If the key exists, return the value
3449
if (this.storage[index][i][0] === key) return this.storage[index][i][1]
3550
}
3651
}
3752
}
3853

54+
// Remove a key-value pair from the hash table
3955
remove(key) {
56+
// Hash the key
4057
const index = this._hash(key, this.limit)
58+
// Check if the bucket exists
4159
if (this.storage[index]) {
60+
// If the key matches the key at the index and there is only one item in the bucket, delete the bucket
4261
if (
4362
this.storage[index].length === 1 &&
4463
this.storage[index][0][0] === key
4564
) {
4665
delete this.storage[index]
4766
} else {
67+
// If the index is not empty, iterate through the bucket
4868
for (let i = 0; i < this.storage[index].length; i++) {
69+
// If the key exists, delete the key-value pair
4970
if (this.storage[index][i][0] === key) delete this.storage[index][i]
5071
}
5172
}
5273
}
5374
}
5475

76+
// Check if a key exists in the hash table
5577
has(key) {
78+
// Hash the key to find the index
5679
const index = this._hash(key, this.limit)
80+
// Check if the bucket at the index exists
5781
if (this.storage[index]) {
82+
// Iterate through the bucket's key-value pairs
5883
for (let i = 0; i < this.storage[index].length; i++) {
84+
// Compare the current key with the target key and if the key is found, return true
5985
if (this.storage[index][i][0] === key) return true
6086
}
6187
}
88+
// If the key is not found, return false
6289
return false
6390
}
6491

92+
// Print all keys/values in the table
6593
printTable() {
6694
for (let i = 0; i < this.storage.length; i++) {
6795
if (this.storage[i] !== undefined)
@@ -70,6 +98,7 @@ export default class HashTable {
7098
}
7199
}
72100

101+
// Clear all key/values
73102
clear() {
74103
this.storage = new Array(this.limit)
75104
}

0 commit comments

Comments
 (0)