Skip to content

Commit 66b57b4

Browse files
committed
added chapter 7
1 parent 0a445cc commit 66b57b4

12 files changed

+450
-0
lines changed

chapter07/01-Dictionaries.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
function Dictionary(){
2+
3+
var items = {};
4+
5+
this.set = function(key, value){
6+
items[key] = value; //{1}
7+
};
8+
9+
this.remove = function(key){
10+
if (this.has(key)){
11+
delete items[key];
12+
return true;
13+
}
14+
return false;
15+
};
16+
17+
this.has = function(key){
18+
return key in items;
19+
};
20+
21+
this.get = function(key) {
22+
return this.has(key) ? items[key] : undefined;
23+
};
24+
25+
this.clear = function(){
26+
items = {};
27+
};
28+
29+
this.size = function(){
30+
return Object.keys(items).length;
31+
};
32+
33+
this.keys = function(){
34+
return Object.keys(items);
35+
};
36+
37+
this.values = function(){
38+
var values = [];
39+
for (var k in items) {
40+
if (this.has(k)) {
41+
values.push(items[k]);
42+
}
43+
}
44+
return values;
45+
};
46+
47+
this.each = function(fn) {
48+
for (var k in items) {
49+
if (this.has(k)) {
50+
fn(k, items[k]);
51+
}
52+
}
53+
};
54+
55+
this.getItems = function(){
56+
return items;
57+
}
58+
}

chapter07/02-UsingDictionaries.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="01-Dictionaries.js"></script>
9+
<script type="text/javascript" src="02-UsingDictionaries.js"></script>
10+
</body>
11+
</html>

chapter07/02-UsingDictionaries.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var dictionary = new Dictionary();
2+
3+
dictionary.set('Gandalf', 'gandalf@email.com');
4+
dictionary.set('John', 'johnsnow@email.com');
5+
dictionary.set('Tyrion', 'tyrion@email.com');
6+
7+
console.log(dictionary.has('Gandalf')); //outputs true
8+
console.log(dictionary.size()); //outputs 3
9+
10+
console.log(dictionary.keys()); //outputs ["Gandalf", "John", "Tyrion"]
11+
console.log(dictionary.values()); //outputs ["gandalf@email.com", "johnsnow@email.com", "tyrion@email.com"]
12+
console.log(dictionary.get('Tyrion')); //outputs tyrion@email.com
13+
14+
dictionary.remove('John');
15+
16+
console.log(dictionary.keys()); //outputs ["Gandalf", "Tyrion"]
17+
console.log(dictionary.values()); //outputs ["gandalf@email.com", "tyrion@email.com"]
18+
19+
console.log(dictionary.getItems()); //Object {Gandalf: "gandalf@email.com", Tyrion: "tyrion@email.com"}

chapter07/03-HashTable.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
function HashTable() {
2+
3+
var table = [];
4+
5+
var loseloseHashCode = function (key) {
6+
var hash = 0;
7+
for (var i = 0; i < key.length; i++) {
8+
hash += key.charCodeAt(i);
9+
}
10+
return hash % 37;
11+
};
12+
13+
var djb2HashCode = function (key) {
14+
var hash = 5381;
15+
for (var i = 0; i < key.length; i++) {
16+
hash = hash * 33 + key.charCodeAt(i);
17+
}
18+
return hash % 1013;
19+
};
20+
21+
var hashCode = function (key) {
22+
return loseloseHashCode(key);
23+
};
24+
25+
this.put = function (key, value) {
26+
var position = hashCode(key);
27+
console.log(position + ' - ' + key);
28+
table[position] = value;
29+
};
30+
31+
this.get = function (key) {
32+
return table[hashCode(key)];
33+
};
34+
35+
this.remove = function(key){
36+
table[hashCode(key)] = undefined;
37+
};
38+
39+
this.print = function () {
40+
for (var i = 0; i < table.length; ++i) {
41+
if (table[i] !== undefined) {
42+
console.log(i + ": " + table[i]);
43+
}
44+
}
45+
};
46+
}

chapter07/04-UsingHash.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="03-HashTable.js"></script>
9+
<script type="text/javascript" src="04-UsingHash.js"></script>
10+
</body>
11+
</html>

chapter07/04-UsingHash.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
var hash = new HashTable();
2+
3+
hash.put('Gandalf', 'gandalf@email.com');
4+
hash.put('John', 'johnsnow@email.com');
5+
hash.put('Tyrion', 'tyrion@email.com');
6+
hash.put('Aaron', 'aaron@email.com');
7+
hash.put('Donnie', 'donnie@email.com');
8+
hash.put('Ana', 'ana@email.com');
9+
hash.put('Jonathan', 'jonathan@email.com');
10+
hash.put('Jamie', 'jamie@email.com');
11+
hash.put('Sue', 'sue@email.com');
12+
hash.put('Mindy', 'mindy@email.com');
13+
hash.put('Paul', 'paul@email.com');
14+
hash.put('Nathan', 'nathan@email.com');
15+
16+
console.log('**** Printing Hash **** ');
17+
18+
hash.print();
19+
20+
console.log('**** Get **** ');
21+
22+
console.log(hash.get('Gandalf'));
23+
console.log(hash.get('Loiane'));
24+
25+
console.log('**** Remove **** ');
26+
27+
hash.remove('Gandalf');
28+
console.log(hash.get('Gandalf'));
29+
hash.print();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
function HashTableSeparateChaining(){
2+
3+
var table = [];
4+
5+
var ValuePair = function(key, value){
6+
this.key = key;
7+
this.value = value;
8+
9+
this.toString = function() {
10+
return '[' + this.key + ' - ' + this.value + ']';
11+
}
12+
};
13+
14+
var loseloseHashCode = function (key) {
15+
var hash = 0;
16+
for (var i = 0; i < key.length; i++) {
17+
hash += key.charCodeAt(i);
18+
}
19+
return hash % 37;
20+
};
21+
22+
var hashCode = function(key){
23+
return loseloseHashCode(key);
24+
};
25+
26+
this.put = function(key, value){
27+
var position = hashCode(key);
28+
console.log(position + ' - ' + key);
29+
30+
if (table[position] == undefined) {
31+
table[position] = new LinkedList();
32+
}
33+
table[position].append(new ValuePair(key, value));
34+
};
35+
36+
this.get = function(key) {
37+
var position = hashCode(key);
38+
39+
if (table[position] !== undefined && !table[position].isEmpty()){
40+
41+
//iterate linked list to find key/value
42+
var current = table[position].getHead();
43+
44+
while(current.next){
45+
if (current.element.key === key){
46+
return current.element.value;
47+
}
48+
current = current.next;
49+
}
50+
51+
//check in case first or last element
52+
if (current.element.key === key){
53+
return current.element.value;
54+
}
55+
}
56+
return undefined;
57+
};
58+
59+
this.remove = function(key){
60+
61+
var position = hashCode(key);
62+
63+
if (table[position] !== undefined){
64+
65+
//iterate linked list to find key/value
66+
var current = table[position].getHead();
67+
68+
while(current.next){
69+
if (current.element.key === key){
70+
table[position].remove(current.element);
71+
if (table[position].isEmpty()){
72+
table[position] = undefined;
73+
}
74+
return true;
75+
}
76+
current = current.next;
77+
}
78+
79+
//check in case first or last element
80+
if (current.element.key === key){
81+
table[position].remove(current.element);
82+
if (table[position].isEmpty()){
83+
table[position] = undefined;
84+
}
85+
return true;
86+
}
87+
}
88+
89+
return false;
90+
};
91+
92+
this.print = function() {
93+
for (var i = 0; i < table.length; ++i) {
94+
if (table[i] !== undefined) {
95+
console.log(table[i].toString());
96+
}
97+
}
98+
};
99+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<script type="text/javascript" src="../chapter05/01-Linked-List.js"></script>
9+
<script type="text/javascript" src="05-HashCollisionSeparateChaining.js"></script>
10+
<script type="text/javascript" src="06-UsingHashCollisionSeparateChaining.js"></script>
11+
</body>
12+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
var hashTableSeparateChaining = new HashTableSeparateChaining();
2+
3+
hashTableSeparateChaining.put('Gandalf', 'gandalf@email.com');
4+
hashTableSeparateChaining.put('John', 'johnsnow@email.com');
5+
hashTableSeparateChaining.put('Tyrion', 'tyrion@email.com');
6+
hashTableSeparateChaining.put('Aaron', 'aaron@email.com');
7+
hashTableSeparateChaining.put('Donnie', 'donnie@email.com');
8+
hashTableSeparateChaining.put('Ana', 'ana@email.com');
9+
hashTableSeparateChaining.put('Jonathan', 'jonathan@email.com');
10+
hashTableSeparateChaining.put('Jamie', 'jamie@email.com');
11+
hashTableSeparateChaining.put('Sue', 'sue@email.com');
12+
hashTableSeparateChaining.put('Mindy', 'mindy@email.com');
13+
hashTableSeparateChaining.put('Paul', 'paul@email.com');
14+
hashTableSeparateChaining.put('Nathan', 'nathan@email.com');
15+
16+
console.log('**** Printing Hash **** ');
17+
18+
hashTableSeparateChaining.print();
19+
20+
console.log('**** Get **** ');
21+
22+
console.log(hashTableSeparateChaining.get('Jamie'));
23+
console.log(hashTableSeparateChaining.get('Sue'));
24+
console.log(hashTableSeparateChaining.get('Jonathan'));
25+
console.log(hashTableSeparateChaining.get('Loiane'));
26+
27+
console.log('**** Remove **** ');
28+
29+
console.log(hashTableSeparateChaining.remove('Gandalf'));
30+
console.log(hashTableSeparateChaining.get('Gandalf'));
31+
hashTableSeparateChaining.print();
32+
33+
console.log(hashTableSeparateChaining.remove('Sue'));
34+
hashTableSeparateChaining.print();
35+
36+
console.log(hashTableSeparateChaining.remove('Jamie'));
37+
hashTableSeparateChaining.print();
38+
39+
console.log(hashTableSeparateChaining.remove('Donnie'));
40+
hashTableSeparateChaining.print();

0 commit comments

Comments
 (0)