-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTrieNode.js
More file actions
30 lines (29 loc) · 936 Bytes
/
TrieNode.js
File metadata and controls
30 lines (29 loc) · 936 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
import HasTable from '../hash-table/HashTable'
export default class TrieNode {
constructor(character, isCompleteWord = false) {
this.character = character
this.isCompleteWord = isCompleteWord
this.children = new HasTable()
}
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 isCompleteWord = this.isCompleteWord ? '*' : ''
return `${this.character}${isCompleteWord}${childrenAsString}`
}
}