tethys.backend/resources/js/Components/SimplePasswordMeter/logic/Trie.ts
2024-08-07 14:22:36 +02:00

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;
}
}