Suffix tree: Difference between revisions

Go solution
(→‎{{header|Racket}}: banana now used, prettier tree output)
(Go solution)
Line 18:
 
The computation time for an efficient algorithm should be <math>O(n)</math>, but such an algorithm might be difficult to implement. An easier, <math>O(n^2)</math> algorithm is accepted.
 
=={{header|Go}}==
Vis function from [[Visualize_a_tree#Unicode]].
<lang go>package main
 
import "fmt"
 
func main() {
vis(buildTree("banana$"))
}
 
type tree []node
 
type node struct {
sub string // a substring of the input string
ch []int // list of child nodes
}
 
func buildTree(s string) tree {
t := tree{node{}} // root node
for i := range s {
t = t.addSuffix(s[i:])
}
return t
}
 
func (t tree) addSuffix(suf string) tree {
n := 0
for i := 0; i < len(suf); {
b := suf[i]
ch := t[n].ch
var x2, n2 int
for ; ; x2++ {
if x2 == len(ch) {
// no matching child, remainder of suf becomes new node.
n2 = len(t)
t = append(t, node{sub: suf[i:]})
t[n].ch = append(t[n].ch, n2)
return t
}
n2 = ch[x2]
if t[n2].sub[0] == b {
break
}
}
// find prefix of remaining suffix in common with child
sub2 := t[n2].sub
j := 0
for ; j < len(sub2); j++ {
if suf[i+j] != sub2[j] {
// split n2
n3 := n2
// new node for the part in common
n2 = len(t)
t = append(t, node{sub2[:j], []int{n3}})
t[n3].sub = sub2[j:] // old node loses the part in common
t[n].ch[x2] = n2
break // continue down the tree
}
}
i += j // advance past part in common
n = n2 // continue down the tree
}
return t
}
 
func vis(t tree) {
if len(t) == 0 {
fmt.Println("<empty>")
return
}
var f func(int, string)
f = func(n int, pre string) {
children := t[n].ch
if len(children) == 0 {
fmt.Println("╴", t[n].sub)
return
}
fmt.Println("┐", t[n].sub)
last := len(children) - 1
for _, ch := range children[:last] {
fmt.Print(pre, "├─")
f(ch, pre+"│ ")
}
fmt.Print(pre, "└─")
f(children[last], pre+" ")
}
f(0, "")
}</lang>
{{out}}
<pre>
├─╴ banana$
├─┐ a
│ ├─┐ na
│ │ ├─╴ na$
│ │ └─╴ $
│ └─╴ $
├─┐ na
│ ├─╴ na$
│ └─╴ $
└─╴ $
</pre>
 
=={{header|Perl}}==
1,707

edits