forked from geolba/tethys.backend
31 lines
717 B
TypeScript
31 lines
717 B
TypeScript
import TrieNode from './TieNode';
|
|
|
|
export default class Trie {
|
|
private root: TrieNode;
|
|
constructor() {
|
|
this.root = new TrieNode();
|
|
}
|
|
|
|
insert(word: string) {
|
|
let node: TrieNode = this.root;
|
|
for (let char of word) {
|
|
if (!node.children[char]) {
|
|
node.children[char] = new TrieNode();
|
|
}
|
|
node = node.children[char];
|
|
}
|
|
node.isEndOfWord = true;
|
|
}
|
|
|
|
search(word: string) {
|
|
let node = this.root;
|
|
for (let char of word) {
|
|
if (!node.children[char]) {
|
|
return false;
|
|
}
|
|
node = node.children[char];
|
|
}
|
|
return node.isEndOfWord;
|
|
}
|
|
}
|