Files
SnowLuma/.pnpm-store/v11/files/17/35796ab1315e17bfdda1301e71764270a1ca76e79e6f1a3be8a0e6a84ccf9d4c098ba6da36415dffcf5d9d806141de2a5ad06db9796bf48cbdef5961986232
T
TanTan 2a69b23b47
Dev Build / cleanup (push) Waiting to run
Dev Build / typecheck (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:27:07 +08:00

43 lines
1009 B
Plaintext

export default class TrieNode {
children = new Map<string, TrieNode>()
subs?: string[]
constructor(public parents: string[] = []) {}
addSub(key: string, ...subs: string[]): this {
const firstChar = key.charAt(0)
if (!this.children.has(firstChar)) {
this.children.set(firstChar, new TrieNode([...this.parents, firstChar]))
}
let cur = this.children.get(firstChar)!
for (let i = 1; i < key.length; i += 1) {
const c = key.charAt(i)
if (!cur.hasChild(c)) {
cur.addChild(c)
}
cur = cur.getChild(c)!
}
cur.subs = (cur.subs || []).concat(subs)
return this
}
getChild(child: string): TrieNode | undefined {
return this.children.get(child)
}
isTerminal(): boolean {
return !!this.subs
}
addChild(child: string): void {
if (!this.hasChild(child)) {
this.children.set(child, new TrieNode([...this.parents, child]))
}
}
hasChild(child: string): boolean {
return this.children.has(child)
}
}