-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
117 lines (100 loc) · 2.9 KB
/
index.js
File metadata and controls
117 lines (100 loc) · 2.9 KB
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
class Node {
constructor(value = null) {
this.value = value;
this.children = {};
this.completeWord = {
timesChosen: 0,
word: null
};
}
}
class Trie {
constructor() {
this.root = new Node();
this.count = 0;
this.suggestions = [];
}
insert(word, node = this.root, arr = []) {
word = word.toLowerCase();
const newArr = arr;
if (node.children[word[0]] && word.length === 1) {
if (node.children[word[0]].completeWord.word === null) {
newArr.push(word[0]);
node.children[word[0]].completeWord.word = newArr.join('');
this.count++;
} else {
return 'That word already exists'
}
} else if (node.children[word[0]]) {
newArr.push(word[0]);
this.insert(word.slice(1), node.children[word[0]], newArr);
} else {
node.children[word[0]] = new Node(word[0]);
if (word.length === 1 && node.children[word[0]].completeWord.word === null) {
newArr.push(word[0]);
node.children[word[0]].completeWord.word = newArr.join('');
this.count++;
} else {
newArr.push(word[0]);
this.insert(word.slice(1), node.children[word[0]], newArr);
}
}
}
populate(arr) {
arr.forEach(el => {
this.insert(el);
});
}
suggest(str) {
this.suggestions = [];
const splitStr = [...str.toLowerCase()];
let currentNode = this.root;
for (let i = 0; i < splitStr.length; i++) {
if (currentNode === undefined) {
return this.suggestions;
}
currentNode = currentNode.children[splitStr[i]];
}
this.findTheWords(currentNode);
this.suggestions.sort((a, b) => b.timesChosen - a.timesChosen);
this.suggestions = this.suggestions.map(({ word }) => word);
return this.suggestions;
}
findTheWords(currentNode) {
if (currentNode === undefined || currentNode.children === undefined) {
return ''
}
const childrenKeys = Object.keys(currentNode.children);
childrenKeys.forEach(key => {
if (currentNode.children[key].completeWord.word) {
this.suggestions.push(currentNode.children[key].completeWord);
}
this.findTheWords(currentNode.children[key]);
});
}
delete(str) {
let splitStr = [...str.toLowerCase()];
let currentNode = this.root;
for (let i = 0; i < splitStr.length; i++) {
if (currentNode === undefined) {
return 'That\'s not a word.';
}
currentNode = currentNode.children[splitStr[i]];
}
if (currentNode.completeWord.word) {
currentNode.completeWord.word = null;
this.count--;
} else {
return 'That\'s not a word.';
}
}
select(word) {
let splitStr = [...word.toLowerCase()];
let currentNode = this.root;
for (let i = 0; i < splitStr.length; i++) {
currentNode = currentNode.children[splitStr[i]];
}
currentNode.completeWord.timesChosen++;
}
}
module.exports = Trie;