Suffix tree: Difference between revisions
Content added Content deleted
No edit summary |
No edit summary |
||
Line 732: | Line 732: | ||
s += this.toString_f(children[children.length - 1], pre + ' '); |
s += this.toString_f(children[children.length - 1], pre + ' '); |
||
return s; |
return s; |
||
} |
|||
getLongestRepeatedSubString() { |
|||
return this.getLongestRepeatedSubString_f(0); |
|||
} |
|||
getLongestRepeatedSubString_f(n) { |
|||
let str = ''; |
|||
for (const n2 of this.nodes[n].children.filter(n2 => this.nodes[n2].children.length > 0)) { // ignore leaves |
|||
const temp = this.getLongestRepeatedSubString_f(n2); |
|||
if (temp.length > str.length) { |
|||
str = temp; |
|||
} |
|||
} |
|||
return this.nodes[n].sub + str; |
|||
} |
} |
||
} |
} |
||
const st = new SuffixTree('banana'); |
const st = new SuffixTree('banana'); |
||
console.log(st.toString()); |
console.log(st.toString());</lang> |
||
console.log('>', st.getLongestRepeatedSubString());</lang> |
|||
{{out}} |
{{out}} |
||
Line 762: | Line 747: | ||
└─┐ na |
└─┐ na |
||
└─- na |
└─- na |
||
> ana |
|||
</pre> |
</pre> |
||