-
Notifications
You must be signed in to change notification settings - Fork 269
/
index.js
74 lines (60 loc) · 1.75 KB
/
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
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
const Node = require('./Node');
class Trie {
constructor() {
this.root = new Node('');
}
// 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);
}
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();
currentNode.increaseCount();
return true;
}
search(key) {
if (!key) {
return false;
}
// convert word to lower case
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]) {
return false;
}
currentNode = currentNode.children[index];
}
if (currentNode !== null && currentNode.isEndOfWord) {
return true;
}
return false;
}
}
// const words = ['bed', 'ball', 'apple', 'java', 'javascript'];
// const trie = new Trie();
// words.forEach(word => trie.insert(word));
// console.log(trie.root);
// console.log(trie.search(words[3]));
// console.log(trie.search('word'));
// console.log(trie.search(words[4]));
// console.log(trie.search('random'));
module.exports = Trie;