Skip to content

Commit cdca901

Browse files
committed
Adding chapter 8 and 9, Hashing and Set Data Strucutres
1 parent 675b6e3 commit cdca901

File tree

5 files changed

+333
-4
lines changed

5 files changed

+333
-4
lines changed

06-chapter-1-Linked-List.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
// Node Linked List member class example
12
class Node {
23
constructor(element = 'Head', next = null) {
34
this.element = element;
45
this.next = next;
56
}
67
}
78

9+
// Linked List data structure class example
810
class LList {
911
constructor(head = new Node()) {
1012
this.head = head;

08-chapter-Hashing.js

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// HashTable data structure class example
2+
class HashTable {
3+
constructor() {
4+
this.table = new Array(137);
5+
this.values = [];
6+
}
7+
8+
hash(string) {
9+
const H = 37;
10+
let total = 0;
11+
12+
for (var i = 0; i < string.length; i++) {
13+
total += H * total + string.charCodeAt(i);
14+
}
15+
total %= this.table.length;
16+
if (total < 1) {
17+
this.table.length -1
18+
}
19+
return parseInt(total);
20+
}
21+
22+
showDistro() {
23+
for (const key in this.table) {
24+
if(this.table[key] !== undefined) {
25+
console.log(key, ' : ', this.table[key]);
26+
}
27+
}
28+
}
29+
30+
put(data) {
31+
const pos = this.hash(data);
32+
this.table[pos] = data;
33+
}
34+
35+
get(key) {
36+
return this.table[this.hash(key)];
37+
}
38+
}
39+
40+
// HashTable with Build Chains technique class example for exercise 2
41+
class HashTableChains extends HashTable {
42+
constructor() {
43+
super();
44+
this.buildChains();
45+
}
46+
buildChains() {
47+
for (var i = 0; i < this.table.length; i++) {
48+
this.table[i] = new Array();
49+
}
50+
}
51+
52+
showDistro() {
53+
for (const key in this.table) {
54+
if(this.table[key][0] !== undefined) {
55+
console.log(key, ' : ', this.table[key]);
56+
}
57+
}
58+
}
59+
60+
put(key, data) {
61+
const pos = this.hash(key);
62+
let index = 0;
63+
if(this.table[pos][index] === undefined) {
64+
this.table[pos][index] = data;
65+
} else {
66+
++index;
67+
while (this.table[pos][index] !== undefined ) {
68+
index++;
69+
}
70+
this.table[pos][index] = data;
71+
}
72+
}
73+
74+
get(key) {
75+
const pos = this.hash(key);
76+
let index = 0;
77+
while (this.table[pos][index] != key) {
78+
if(this.table[pos][index] !== undefined) {
79+
return this.table[pos][index]
80+
} else {
81+
return undefined;
82+
}
83+
index++;
84+
}
85+
}
86+
}
87+
88+
// HashTable with Linear Probing technique class example for exercise 1
89+
class HashTableLinearP extends HashTable {
90+
constructor() {
91+
super();
92+
this.values = new Array();
93+
}
94+
95+
put(key, data) {
96+
const pos = this.hash(key);
97+
if(this.table[pos] === undefined) {
98+
this.table[pos] = key;
99+
this.values[pos] = data;
100+
} else {
101+
while(this.table[pos] !== undefined) {
102+
pos++;
103+
}
104+
this.table[pos] = key;
105+
this.values[pos] = data;
106+
}
107+
}
108+
109+
get(key) {
110+
const hash = this.hash(key);
111+
if (hash > -1) {
112+
for (let i = hash; this.table[i] !== undefined; i++) {
113+
if (this.table[i] === key) {
114+
return this.values[i];
115+
}
116+
}
117+
}
118+
return undefined;
119+
}
120+
121+
showDistro() {
122+
for (const key in this.table) {
123+
if(this.table[key] !== undefined) {
124+
console.log(key, ' : ', this.values[key]);
125+
}
126+
}
127+
}
128+
}
129+
130+
// Implementation
131+
// #################################################
132+
console.log('CHAPTER 8');
133+
134+
const readline = require('readline');
135+
const fs = require('fs');
136+
137+
const hash1 = new HashTableLinearP();
138+
const hash2 = new HashTableChains();
139+
140+
const rl = readline.createInterface({
141+
input: fs.createReadStream('words.txt')
142+
});
143+
144+
rl.on('line', (line) => {
145+
const split = line.split(':');
146+
hash1.put(split[0], split[1]);
147+
hash2.put(split[0], split[1]);
148+
});
149+
150+
rl.on('close', () => {
151+
/*
152+
1. Use linear probing to create a simple dictionary to store the definitions of words.
153+
Reads a text file that contains a list of words and definitions and stores them in a hash table.
154+
*/
155+
console.log('### Exercise 1');
156+
hash1.showDistro();
157+
/*
158+
2. Repeat exercise 1 using separate chaining.
159+
*/
160+
console.log('\n\n### Exercise 2');
161+
hash2.showDistro();
162+
163+
});

09-chapter-Set.js

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// Set data structure class example
2+
class Set {
3+
constructor(dataStore = []) {
4+
this.dataStore = dataStore;
5+
}
6+
7+
add(data) {
8+
if(this.dataStore.indexOf(data) < 0 ) {
9+
if(this.size() === 0) {
10+
this.dataStore.push(data);
11+
} else {
12+
let position = 0;
13+
for (let i = 0; i < this.dataStore.length; i++) {
14+
if(data > this.dataStore[i]) {
15+
position = i + 1;
16+
} else {
17+
break;
18+
}
19+
}
20+
this.dataStore.splice(position, 0, data);
21+
}
22+
return true;
23+
} else {
24+
console.log(`${data} is already on the set`);
25+
return false;
26+
}
27+
}
28+
29+
remove(data) {
30+
const pos = this.dataStore.findIndex(x => x === data);
31+
if(pos > 0) {
32+
this.dataStore.splice(pos,1);
33+
return true;
34+
} else {
35+
console.log(`${data} is not present on the set`);
36+
return false;
37+
}
38+
}
39+
40+
contains(data) {
41+
if(this.dataStore.findIndex(x => x === data) > 0) {
42+
return true;
43+
} else {
44+
return false;
45+
}
46+
}
47+
48+
size() {
49+
return this.dataStore.length;
50+
}
51+
52+
union(set) {
53+
const tempSet = new Set();
54+
tempSet.dataStore = Object.assign([], this.dataStore);
55+
for (const key in set.dataStore) {
56+
if (!tempSet.contains(set.dataStore[key])) {
57+
tempSet.dataStore.push(set.dataStore[key]);
58+
}
59+
}
60+
return tempSet;
61+
}
62+
63+
intersect(set) {
64+
const tempSet = new Set();
65+
for (const key in set.dataStore) {
66+
if (this.contains(set.dataStore[key])) {
67+
tempSet.add(set.dataStore[key]);
68+
}
69+
}
70+
return tempSet;
71+
}
72+
73+
difference(set) {
74+
const tempSet = new Set();
75+
for (const key in this.dataStore) {
76+
if(!set.contains(this.dataStore[key])) {
77+
tempSet.add(this.dataStore[key]);
78+
}
79+
}
80+
81+
return tempSet;
82+
}
83+
84+
show() {
85+
console.log(JSON.stringify(this.dataStore));
86+
}
87+
88+
higher(element) {
89+
const exists = this.dataStore.findIndex(x => x === element);
90+
if(exists >= 0 && this.dataStore[exists +1] !== undefined) {
91+
console.log(`Given the element: ${element}, A higher elemnt is: ${this.dataStore[exists + 1]}`);
92+
} else if(exists >= 0) {
93+
console.log(`There is no higher element than ${element}`);
94+
} else {
95+
console.log(`The element given: ${element} doesn't exists!`);
96+
}
97+
}
98+
99+
lower(element) {
100+
const exists = this.dataStore.findIndex(x => x === element);
101+
if(exists >= 0 && this.dataStore[exists - 1] !== undefined) {
102+
console.log(`Given the element: ${element}, A lower elemnt is: ${this.dataStore[exists - 1]}`);
103+
} else if(exists >= 0) {
104+
console.log(`There is no lower element than ${element}`);
105+
} else {
106+
console.log(`The element given: ${element} doesn't exists!`);
107+
}
108+
}
109+
}
110+
111+
// Implementation
112+
// ##############################################
113+
/*
114+
1. Modify the Set class so that the class stores its elements in sorted order.
115+
Write a program to test your implementation.
116+
*/
117+
console.log('CHAPTER 9');
118+
console.log('### Exercise 1');
119+
const cis = new Set();
120+
cis.add("Mike");
121+
cis.add("Clayton");
122+
cis.add("Jennifer");
123+
cis.add("Raymond");
124+
const dmp = new Set();
125+
dmp.add("Raymond");
126+
dmp.add("Cynthia");
127+
dmp.add("Jonathan");
128+
console.log('## Original Sets');
129+
cis.show();
130+
dmp.show();
131+
console.log('\n### Union to sets ###');
132+
const un = cis.union(dmp);
133+
un.show();
134+
console.log('\n### Intersect to sets ###');
135+
const int = cis.intersect(dmp);
136+
int.show();
137+
console.log('\n### Difference to sets ###');
138+
const dif = cis.difference(dmp);
139+
dif.show();
140+
141+
142+
/*
143+
2. Add the function higher(element) to the Set class. This function returns
144+
the least element in the set strictly greater than the given element.
145+
Test your function in a program.
146+
*/
147+
console.log('\n\n### Exercise 3');
148+
dmp.show();
149+
dmp.higher('Cynthia');
150+
dmp.higher('Raymond');
151+
/*
152+
3. Add the function lower(element) to the Set class.
153+
This function returns the greatest element in the set strictly less than the given element.
154+
Test your function in a program.
155+
*/
156+
console.log('\n\n### Exercise 4');
157+
dmp.show();
158+
dmp.lower('Cynthia');
159+
dmp.lower('Raymond');

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,13 @@ The purpose of this repo is to update the exercises to ES6 standards and to corr
1818
| 5.- | [Linked List](./06-chapter-1-Linked-List.js) | 4 | A `Linked list` is a collection of objects called nodes. Each node is linked to a successor node in the list using an object reference.
1919
| 6.- | [Double Linked List](./06-chapter-2-Double-Linked-List.js) | 4 | Traversing a `Double linked list` are more efficient, since we no longer have to search for the previous node.
2020
| 7.- | [Circular Linked List](./06-chapter-3-Circular-Linked-List.js) | 4 | The reason you might want to create a `Circular linked list` is if you want the ability to go backward through a list but don’t want the extra overhead of creating a doubly linked list.
21-
| 8.- | [Dictionaries](./07-chapter-Dictionaries.js) | 3 | A `Dictionary` is a data structure that stores data as key-value pairs
22-
21+
| 8.- | [Dictionaries](./07-chapter-Dictionaries.js) | 3 | A `Dictionary` is a data structure that stores data as key-value pairs.
22+
| 9.- | [Hashing](./08-chapter-Hashing.js) | 2 | `Hashing` is a common technique for storing data in such a way that the data can be inserted and retrieved very quickly. Hashing uses a data structure called a hash table. Although hash tables provide fast insertion, deletion, and retrieval, they perform poorly for operations that involve searching.
23+
| 10.- | [Set](./09-chapter-Set.js) | 4 | A `Set` is a collection of unique elements. The elements of a set are called members. The two most important properties of sets are that the members of a set are unordered and that no member can occur in a set more than once.
2324

2425

2526
### To run the examples we need the following:
2627

27-
- NodeJS Install
28-
- Open Any Terminal and position to the folder where the files are located
28+
- NodeJS Installed
29+
- Open Any Terminal and position it to the folder where the files are located
2930
- execute the command: `$ node [file-name.js]`

words.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
alfa: Nombre de la primera letra del alfabeto griego; se escribe Α/α;la alfa es una vocal que se transcribe como la 'a' latina.
2+
Alfabeto: El alfabeto o abecedario de una lengua o idioma es el conjunto ordenado de sus letras.
3+
Alfaro: Alfaro es una ciudad y municipio español perteneciente a la comunidad autónoma de La Rioja (España).
4+
alfaguara: Manantial de agua muy abundante que surge con violencia.

0 commit comments

Comments
 (0)