mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-12-08 19:06:00 +00:00
38 lines
962 B
JavaScript
38 lines
962 B
JavaScript
import HashTable from '../hash-table/HashTable';
|
|
|
|
export default class TrieNode {
|
|
constructor(character, isCompleteWord = false) {
|
|
this.character = character;
|
|
this.isCompleteWord = isCompleteWord;
|
|
this.children = new HashTable();
|
|
}
|
|
|
|
getChild(character) {
|
|
return this.children.get(character);
|
|
}
|
|
|
|
addChild(character, isCompleteWord = false) {
|
|
if (!this.children.has(character)) {
|
|
this.children.set(character, new TrieNode(character, isCompleteWord));
|
|
}
|
|
|
|
return this.children.get(character);
|
|
}
|
|
|
|
hasChild(character) {
|
|
return this.children.has(character);
|
|
}
|
|
|
|
suggestChildren() {
|
|
return [...this.children.getKeys()];
|
|
}
|
|
|
|
toString() {
|
|
let childrenAsString = this.suggestChildren().toString();
|
|
childrenAsString = childrenAsString ? `:${childrenAsString}` : '';
|
|
const isCompleteString = this.isCompleteWord ? '*' : '';
|
|
|
|
return `${this.character}${isCompleteString}${childrenAsString}`;
|
|
}
|
|
}
|