forked from knaxus/problem-solving-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
39 lines (32 loc) · 927 Bytes
/
index.js
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
const Node = require('./Node');
class Trie {
constructor() {
this.root = new Node('');
}
insert(key) {
if (!key) {
return false;
}
// convert to lower case
// keys are basically words
const word = key.toLowerCase();
let currentNode = this.root;
for (let level = 0; level < word.length; level += 1) {
const index = this.getIndexOfChar(word[level]);
if (!currentNode.children[index]) {
currentNode.children[index] = new Node(word[level]);
}
currentNode = currentNode.children[index];
}
// when we are done with inserting all the character of the word,
// mark the node as end leaf
currentNode.markAsLeaf();
return true;
}
// helper to get the index of a character
// eslint-disable-next-line class-methods-use-this
getIndexOfChar(char) {
return char.charCodeAt(0) - 'a'.charCodeAt(0);
}
}
module.exports = Trie;