-
Notifications
You must be signed in to change notification settings - Fork 0
/
trie.js
69 lines (62 loc) · 1.67 KB
/
trie.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
class TrieNode {
constructor(word, val, depth) {
word = word.toLowerCase();
// on trie init
if (!val) {
this.val = '';
this.depth = 1;
} else {
this.val = val;
this.depth = depth;
}
if (this.val === '') {
// case for when first initalizing the tree
this.isWord = false;
} else if (this.val === word && !this.isWord) {
this.isWord = true;
updateWordCount();
} else {
this.isWord = false;
this.addChild(word);
}
// this code is just for the UI, it's not relevant
// to the function or structure of the tree
updateNodeCount();
if (this.depth > window.highestTrieDepth) {
window.highestTrieDepth = this.depth;
updateTrieDepth();
}
}
addChild(word) {
// ensure all inputs are lowercase
word = word.toLowerCase();
let parentVal = this.val;
const [nextLetter, nextVal] = extract(word, parentVal);
if (!nextLetter && !this.isWord) {
this.isWord = true;
updateWordCount();
} else if (!nextLetter) {
this.isWord = true;
} else if (this[nextLetter]) {
this[nextLetter].addChild(word);
} else {
const nextDepth = this.depth + 1;
this[nextLetter] = new TrieNode(word, nextVal, nextDepth);
}
}
}
// helper function for trie
const extract = (word, parentVal) => {
let nextLetter, nextVal;
if (parentVal === '') {
nextLetter = word[0];
nextVal = nextLetter;
} else {
const wordSecondHalf = word.slice(parentVal.length);
nextLetter = wordSecondHalf[0];
nextVal = word.slice(0, parentVal.length + 1);
}
return [nextLetter, nextVal];
};
// initialize trie
window.trie = new TrieNode('');