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 a47fbd7

Browse files
Hash Table using Linear Probing 🎉
1 parent 1c9e475 commit a47fbd7

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
class HashTable {
2+
constructor() {
3+
this.table = new Array(137);
4+
this.values = new Array(137);
5+
}
6+
7+
betterHash(word) {
8+
let total = 0;
9+
for(let i = 0; i < word.length; i++)
10+
total = 37 * total + word.toLowerCase().charCodeAt(i);
11+
return total % this.table.length;
12+
}
13+
14+
put(key, value) {
15+
const pos = this.betterHash(key);
16+
while(this.table[pos] != undefined)
17+
pos++;
18+
this.table[pos] = key;
19+
this.values[pos] = value;
20+
}
21+
22+
get(key) {
23+
const hash = this.betterHash(key);
24+
for(let i = hash;this.table[hash] != undefined; i++)
25+
if(this.table[hash] == key)
26+
break;
27+
return this.table[hash];
28+
}
29+
30+
showAll() {
31+
let str = "";
32+
for (let i = 0; i < this.table.length; i++)
33+
if(this.table[i] !=undefined)
34+
str += this.values[i] + " : " + this.table[i] + "\n";
35+
return str;
36+
}
37+
}
38+
39+
module.exports = HashTable;

‎examples/datastructures/hash-table-separate-chaining.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class HashTable {
3434

3535
showAll() {
3636
let str = "";
37-
for (let i = 0; i < this.table.length&&this.table[i]!=undefined; i++)
38-
for (let j = 0; j < this.table[i].length&&this.table[i][j]!=undefined; j++)
37+
for (let i = 0; i < this.table.length; i++)
38+
for (let j = 0; j < this.table[i].length; j++)
3939
str += i + " : " + this.table[i] + "\n";
4040
return str;
4141
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const expect = require('chai').expect;
2+
const HashTable = require('../../examples/datastructures/hash-table-linear-probing');
3+
4+
describe('=> HASH TABLE USING LINEAR PROBING', function() {
5+
let people;
6+
before(function () {
7+
people = new HashTable();
8+
});
9+
10+
it('should create an Empty Hash Table', function(done) {
11+
expect(people.table.length).to.deep.equal(137);
12+
done();
13+
});
14+
15+
it('should put value into the Hash Table',function() {
16+
people.put("Diana",13);
17+
people.put("Clayton",14);
18+
people.put("Raymond",47);
19+
people.put("Nadia",28);
20+
people.put("Afzar",51);
21+
expect(people.showAll()).to.equal("13 : Diana\n51 : Afzar\n14 : Clayton\n47 : Raymond\n28 : Nadia\n");
22+
});
23+
24+
it('should get value from the Hash Table',function() {
25+
expect(people.get("Nadia")).to.equal("Nadia");
26+
expect(people.get("Diana")).to.equal("Diana");
27+
expect(people.get("Azhar")).to.equal(undefined);
28+
});
29+
30+
});

0 commit comments

Comments
(0)

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