Skip to content

Commit 5ec1287

Browse files
committed
chapter 07: [Dictionaries and Hashes]
1 parent 188bbc2 commit 5ec1287

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+4370
-8
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"no-console": 0,
2525
"import/prefer-default-export": 0,
2626
"comma-dangle": 0,
27-
"no-underscore-dangle": 0
27+
"no-underscore-dangle": 0,
28+
"no-param-reassign": 0
2829
}
2930
}

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Work in Progress.
1616
* 04: [Queues and Deques](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter04)
1717
* 05: [LinkedLists](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter05)
1818
* 06: [Sets](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter06)
19+
* 07: [Dictionaries and Hashes](https://github.com/loiane/javascript-datastructures-algorithms/tree/third-edition/examples/chapter07)
1920

2021
### Third Edition Updates
2122

@@ -27,6 +28,26 @@ Work in Progress.
2728
* Algorithms tested with Mocha + Chai (test code available in `test` directory)
2829
* **TypeScript** version of the source code included (library and tests)
2930

31+
## Project Structure
32+
33+
`src/js/index.js` file contains all the data structures and algorithms listed by chapter.
34+
35+
```
36+
|_examples (how to use each data structure and algorithm, organized by chapter)
37+
|_src
38+
|___js (source code: JavaScript version)
39+
|_____data-structures
40+
|_______models (classes used by DS: Node, ValuePair, ...)
41+
|_____others (other algorithms such as palindome checker, hanoi tower)
42+
|___ts (source code: TypeScript version)
43+
|_____data-structures
44+
|_______models
45+
|_____others
46+
|_test (unit tests with Mocha and Chai for src)
47+
|___js (tests for JavaScript code)
48+
|___ts (tests for TypeScript code)
49+
```
50+
3051
## Installing and running the book examples With Node
3152

3253
* Install [Node](https://nodejs.org)

examples/PacktDataStructuresAlgorithms.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
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 src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="01-Dictionaries.js"></script>
10+
</body>
11+
</html>

examples/chapter07/01-Dictionaries.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { Dictionary } = PacktDataStructuresAlgorithms;
2+
3+
const dictionary = new Dictionary();
4+
5+
dictionary.set('Gandalf', '[email protected]');
6+
dictionary.set('John', '[email protected]');
7+
dictionary.set('Tyrion', '[email protected]');
8+
9+
console.log(dictionary.hasKey('Gandalf')); // true
10+
console.log(dictionary.size()); // 3
11+
12+
console.log(dictionary.keys()); // ["Gandalf", "John", "Tyrion"]
13+
console.log(dictionary.values()); // ["[email protected]", "[email protected]", "[email protected]"]
14+
console.log(dictionary.get('Tyrion')); // tyrion@email.com
15+
16+
dictionary.remove('John');
17+
18+
console.log(dictionary.keys()); // ["Gandalf", "Tyrion"]
19+
console.log(dictionary.values()); // ["[email protected]", "[email protected]"]
20+
21+
console.log(dictionary.keyValues()); // [{key: "Gandalf", value: "gandalf@email.com"}, {key: "Tyrion", value: "tyrion@email.com"}]
22+
console.log(dictionary.toString()); // [#Gandalf: gandalf@email.com],[#Tyrion: [email protected]]

examples/chapter07/02-HashTable.html

Lines changed: 11 additions & 0 deletions
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 src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="02-HashTable.js"></script>
10+
</body>
11+
</html>

examples/chapter07/02-HashTable.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
const { HashTable } = PacktDataStructuresAlgorithms;
2+
3+
const hash = new HashTable();
4+
5+
hash.put('Gandalf', '[email protected]');
6+
hash.put('John', '[email protected]');
7+
hash.put('Tyrion', '[email protected]');
8+
hash.put('Aaron', '[email protected]');
9+
hash.put('Donnie', '[email protected]');
10+
hash.put('Ana', '[email protected]');
11+
hash.put('Jonathan', '[email protected]');
12+
hash.put('Jamie', '[email protected]');
13+
hash.put('Sue', '[email protected]');
14+
hash.put('Mindy', '[email protected]');
15+
hash.put('Paul', '[email protected]');
16+
hash.put('Nathan', '[email protected]');
17+
18+
console.log('**** Printing Hash **** ');
19+
20+
console.log(hash.toString());
21+
// {5 => [#Sue: sue@email.com]},{10 => [#Nathan: nathan@email.com]},{13 => [#Ana: ana@email.com]},{16 => [#Aaron: aaron@email.com]},{19 => [#Gandalf: gandalf@email.com]},{29 => [#John: johnsnow@email.com]},{32 => [#Paul: paul@email.com]}
22+
23+
console.log('**** Get **** ');
24+
25+
console.log(hash.get('Gandalf')); // gandalf@email.com
26+
console.log(hash.get('Loiane')); // undefined
27+
28+
console.log('**** Remove **** ');
29+
30+
hash.remove('Gandalf');
31+
console.log(hash.get('Gandalf')); // undefined
32+
33+
console.log(hash.toString());
34+
// {5 => [#Sue: sue@email.com]},{10 => [#Nathan: nathan@email.com]},{13 => [#Ana: ana@email.com]},{16 => [#Aaron: aaron@email.com]},{29 => [#John: johnsnow@email.com]},{32 => [#Paul: paul@email.com]}
Lines changed: 11 additions & 0 deletions
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 src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="03-HashCollisionSeparateChaining.js"></script>
10+
</body>
11+
</html>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const { HashTableSeparateChaining } = PacktDataStructuresAlgorithms;
2+
3+
const hashTable = new HashTableSeparateChaining();
4+
5+
hashTable.put('Gandalf', '[email protected]');
6+
hashTable.put('John', '[email protected]');
7+
hashTable.put('Tyrion', '[email protected]');
8+
hashTable.put('Aaron', '[email protected]');
9+
hashTable.put('Donnie', '[email protected]');
10+
hashTable.put('Ana', '[email protected]');
11+
hashTable.put('Jonathan', '[email protected]');
12+
hashTable.put('Jamie', '[email protected]');
13+
hashTable.put('Sue', '[email protected]');
14+
hashTable.put('Mindy', '[email protected]');
15+
hashTable.put('Paul', '[email protected]');
16+
hashTable.put('Nathan', '[email protected]');
17+
18+
console.log('**** Printing Hash **** ');
19+
20+
console.log(hashTable.toString());
21+
// {5 => [#Jonathan: jonathan@email.com],[#Jamie: [email protected]],[#Sue: [email protected]]},{10 => [#Nathan: nathan@email.com]},{13 => [#Donnie: donnie@email.com],[#Ana: [email protected]]},{16 => [#Tyrion: tyrion@email.com],[#Aaron: [email protected]]},{19 => [#Gandalf: gandalf@email.com]},{29 => [#John: johnsnow@email.com]},{32 => [#Mindy: mindy@email.com],[#Paul: [email protected]]}
22+
23+
console.log('**** Get **** ');
24+
25+
console.log(hashTable.get('Jamie')); // jamie@email.com
26+
console.log(hashTable.get('Sue')); // sue@email.com
27+
console.log(hashTable.get('Jonathan')); // jonathan@email.com
28+
console.log(hashTable.get('Loiane')); // undefined
29+
30+
console.log('**** Remove **** ');
31+
32+
console.log(hashTable.remove('Gandalf')); // true
33+
console.log(hashTable.get('Gandalf')); // undefined
34+
console.log(hashTable.toString());
35+
// {5 => [#Jonathan: jonathan@email.com],[#Jamie: [email protected]],[#Sue: [email protected]]},{10 => [#Nathan: nathan@email.com]},{13 => [#Donnie: donnie@email.com],[#Ana: [email protected]]},{16 => [#Tyrion: tyrion@email.com],[#Aaron: [email protected]]},{29 => [#John: johnsnow@email.com]},{32 => [#Mindy: mindy@email.com],[#Paul: [email protected]]}
36+
37+
console.log(hashTable.remove('Sue')); // true
38+
console.log(hashTable.toString());
39+
// {5 => [#Jonathan: jonathan@email.com],[#Jamie: [email protected]]},{10 => [#Nathan: nathan@email.com]},{13 => [#Donnie: donnie@email.com],[#Ana: [email protected]]},{16 => [#Tyrion: tyrion@email.com],[#Aaron: [email protected]]},{29 => [#John: johnsnow@email.com]},{32 => [#Mindy: mindy@email.com],[#Paul: [email protected]]}
40+
41+
console.log(hashTable.remove('Jamie')); // true
42+
console.log(hashTable.toString());
43+
// {5 => [#Jonathan: jonathan@email.com]},{10 => [#Nathan: nathan@email.com]},{13 => [#Donnie: donnie@email.com],[#Ana: [email protected]]},{16 => [#Tyrion: tyrion@email.com],[#Aaron: [email protected]]},{29 => [#John: johnsnow@email.com]},{32 => [#Mindy: mindy@email.com],[#Paul: [email protected]]}
44+
45+
console.log(hashTable.remove('Donnie')); // true
46+
console.log(hashTable.toString());
47+
// {5 => [#Jonathan: jonathan@email.com]},{10 => [#Nathan: nathan@email.com]},{13 => [#Ana: ana@email.com]},{16 => [#Tyrion: tyrion@email.com],[#Aaron: [email protected]]},{29 => [#John: johnsnow@email.com]},{32 => [#Mindy: mindy@email.com],[#Paul: [email protected]]}
Lines changed: 11 additions & 0 deletions
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 src="./../PacktDataStructuresAlgorithms.min.js"></script>
9+
<script src="04-HashCollisionLinearProbing.js"></script>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)