-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphviz.go
39 lines (28 loc) · 852 Bytes
/
graphviz.go
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
31
32
33
34
35
36
37
38
39
package art
import (
"fmt"
"strings"
)
// Graphviz returns a graphviz formatted string of all the nodes in the tree
// this should only be run on trees with relatively few nodes
func Graphviz(r *ART) string {
var gvzc int
gvoutput := []string{"digraph G {"}
graphviz(r, &gvoutput, &gvzc, "[-1] ROOT", r.root)
gvoutput = append(gvoutput, "}")
return strings.Join(gvoutput, "\n")
}
func graphviz(r *ART, gvoutput *[]string, gvzc *int, previous string, n *node) {
if n.edges == nil {
return
}
for i := 0; i < 256; i++ {
next := n.next(byte(i))
if next == nil {
continue
}
(*gvzc)++
(*gvoutput) = append((*gvoutput), fmt.Sprintf(" \"%s\" -> \"[%d] %s\" [label=\"%s\"]", previous, *gvzc, string(next.prefix), string(byte(i))))
graphviz(r, gvoutput, gvzc, fmt.Sprintf("[%d] %s", *gvzc, string(next.prefix)), next)
}
}