forked from HarshCasper/NeoAlgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hash.js
236 lines (192 loc) · 6.71 KB
/
Hash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
A JavaScript program for creating a well known data structure "Hash Table". A hash table (often called a hash map) is a data structure
that maps keys to values. Hash tables combine lookup, insert, and delete operations in an efficient way. Hash tables can perform in constant time.
In the worst-case scenario, the performance of hash tables can be as low as O(n).
In this program, user can provide a choice to perform operations on a Hash Table. It is created using a ES6 class syntax.
prompt & colors are node packages for taking user input and displaying text in different colors respectively.
*/
const prompt = require("prompt-sync")({ sigint: true });
const colors = require("colors");
// Setting the Color schema
colors.setTheme({
display: "green",
insert: "white",
delete: "red",
leave: "yellow",
end: "rainbow",
wrong: "america",
});
class HashTable {
constructor(size) {
// Set Hash Table size
this.data = new Array(size);
}
// Generate a location using simple hash logic
_hash(key) {
let hash = 0;
for (let i = 0; i < key.length; i++) {
// Location mapping
hash = (hash + key.charCodeAt(i) + i) % this.data.length;
}
return hash;
}
// Set the location of item in hash
set(key, value) {
let address = this._hash(key);
if (!this.data[address]) {
this.data[address] = [];
}
// Insert item at the desired location with [key, value] pair
this.data[address].push([key, value]);
return this.data;
}
// Get the location of item in hash table
get(key) {
let address = this._hash(key);
const currentBucket = this.data[address];
// Item at the location is not found , return undefined
if (!currentBucket) return undefined;
if (currentBucket.length) {
for (let i = 0; i < currentBucket.length; i++) {
if (currentBucket[i][0] === key) {
return {
data: [currentBucket[i][0], currentBucket[i][1]],
location: address,
};
}
}
}
return undefined;
}
// Delete item
deleteItem(value) {
let item = this.get(value);
// Item is not present in hash table
if (item === undefined)
return console.log("Can't delete, no such item found");
// Delete item present at that location from hash table
delete this.data[item.location];
return console.log("Deleted", item.data[0]);
}
// Display all items
keys() {
const keysArray = [];
for (let i = 0; i < this.data.length; i++) {
if (this.data[i]) keysArray.push(this.data[i][0][0]);
}
return keysArray;
}
}
// Instantiating the HashTable
const myHashTable = new HashTable(100);
// Insert item in Hash Table
myHashTable.set("grapes", 1000); // Output - [['grapes', 1000]]
myHashTable.set("apple", 100); // Output - [['apple', 100], ['grapes', 1000]]
myHashTable.set("oranges", 50); // Output - [['apple', 100], ['grapes', 1000], ['oranges', 50]]
myHashTable.deleteItem("apple"); // Output - [['grapes', 1000], ['oranges', 50]]
myHashTable.deleteItem("fruits"); // Output - Can't delete, no such item found
let choice = 0;
let value, index;
// Asking user input for performing operations on Hash Table
do {
console.log("\nWelcome to the Hash Table");
console.log("Follow the instructions to get in the show");
console.log("0. To exit without doing anything".yellow);
console.log("1. Display all elements of the Hash Table".display);
console.log("2. Insert element in the Hash Table".insert);
console.log("3. Find element in Hash Table".display);
console.log("4. Delete element from Hash Table".delete);
choice = +prompt("Enter your choice - ");
switch (choice) {
case 0:
console.log("Hmm, matter of choice!\n".rainbow);
return;
case 1:
console.log("Hash Table - ", myHashTable.keys());
break;
case 2:
value = prompt("Enter Value ( first value ) to Insert element - ");
index = +prompt("Enter amount ( second value) to Insert element - ");
if (index <= 0)
return console.log("You know this is wrong, Bbye!\n".wrong);
myHashTable.set(value, index);
console.log("After Insertion Hash Table - ", myHashTable.keys());
break;
case 3:
if (myHashTable.data.length === 0) {
return console.log("Hash Table in empty".wrong);
}
value = prompt("Enter Value to find the element - ");
let result = myHashTable.get(value);
if (!result) console.log("Element not found");
else console.log("Element ", result);
break;
case 4:
if (myHashTable.data.length === 0) {
return console.log(
"404, you can't remove anything from empty hash table".wrong
);
}
value = prompt("Enter Value to delete - ");
myHashTable.deleteItem(value);
console.log("After Deletion Hash Table - ", myHashTable.keys());
break;
default:
console.log("You know this is wrong, Bbye!\n".wrong);
return;
}
} while (choice !== 0);
// Sample I/O of the above program -
/*
> node Hash
Welcome to the Hash Table
Follow the instructions to get in the show
0. To exit without doing anything
1. Display all elements of the Hash Table
2. Insert element in the Hash Table
3. Find element in Hash Table
4. Delete element from Hash Table
Enter your choice - 1
Hash Table - [ 'grapes', 'oranges' ]
Welcome to the Hash Table
Follow the instructions to get in the show
0. To exit without doing anything
1. Display all elements of the Hash Table
2. Insert element in the Hash Table
3. Find element in Hash Table
4. Delete element from Hash Table
Enter your choice - 2
Enter Value ( first value ) to Insert element - Icecream
Enter amount ( second value) to Insert element - 250
After Insertion Hash Table - [ 'Icecream', 'grapes', 'oranges' ]
Welcome to the Hash Table
Follow the instructions to get in the show
0. To exit without doing anything
1. Display all elements of the Hash Table
2. Insert element in the Hash Table
3. Find element in Hash Table
4. Delete element from Hash Table
Enter your choice - 3
Enter Value to find the element - grapes
Element { data: [ 'grapes', 1000 ], location: 57 }
Welcome to the Hash Table
Follow the instructions to get in the show
0. To exit without doing anything
1. Display all elements of the Hash Table
2. Insert element in the Hash Table
3. Find element in Hash Table
4. Delete element from Hash Table
Enter your choice - 4
Enter Value to delete - grapes
Deleted grapes
After Deletion Hash Table - [ 'Icecream', 'oranges' ]
Welcome to the Hash Table
Follow the instructions to get in the show
0. To exit without doing anything
1. Display all elements of the Hash Table
2. Insert element in the Hash Table
3. Find element in Hash Table
4. Delete element from Hash Table
Enter your choice - 0
Hmm, matter of choice!
*/