-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTRIE.JS
More file actions
55 lines (47 loc) · 984 Bytes
/
TRIE.JS
File metadata and controls
55 lines (47 loc) · 984 Bytes
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
class TrieNode{
constructor() {
this.children = {}
this.isEnd = false;
}
}
class Trie {
constructor() {
this.root = new TrieNode()
this.endSymbol = "*";
}
insert(word){
let node = this.root;
for(let char of word){
if(!node.children[char]){
node.children[char] = new TrieNode()
}
node = node.children[char]
}
node.isEnd = true
}
contains(word) {
let node = this.root;
for(let char of word){
if(!node.children[char]){
return false;
}
node = node.children[char]
}
return node.isEnd;
}
populateSuffixTree(word){
for(let i=0;i<word.length;i++){
this.insert(word.substring(i))
}
}
}
const trie = new Trie();
trie.populateSuffixTree('banana')
trie.populateSuffixTree('shabil')
trie.insert('apple')
trie.insert('shabil')
trie.insert("app")
trie.insert('banana')
console.log(trie);
console.log(trie.contains('bil'));
console.log(trie.contains('a'));