Tree datastructures: Difference between revisions
→{{header|Go}}: Changed 'golfing' to 'trolling'. Also round trip test now programmatic rather than just visual.
m (Withdrew the examples which use other sample outlines. I don't feel comfortable reproducing jokes about mocking and trolling.) |
(→{{header|Go}}: Changed 'golfing' to 'trolling'. Also round trip test now programmatic rather than just visual.) |
||
Line 45:
import (
"fmt"
"io"
"os"
"strings"
)
Line 58 ⟶ 60:
}
func printNest(n nNode, level int, w io.Writer) {
if level == 0 {
fmt.
}
fmt.
for _, c := range n.children {
fmt.
printNest(c, level+1, w)
}
}
Line 84 ⟶ 86:
}
func printIndent(iNodes []iNode, w io.Writer) {
fmt.
for _, n := range iNodes {
fmt.
}
}
Line 101 ⟶ 103:
n1 := nNode{"RosettaCode", nil}
n2 := nNode{"rocks", []nNode{{"code", nil}, {"comparison", nil}, {"wiki", nil}}}
n3 := nNode{"mocks", []nNode{{"
n1.children = append(n1.children, n2, n3)
printNest(n1, 0)▼
var sb strings.Builder
▲ printNest(n1, 0, &sb)
s1 := sb.String()
fmt.Print(s1)
var iNodes []iNode
toIndent(n1, 0, &iNodes)
printIndent(iNodes, os.Stdout)
var n nNode
toNest(iNodes, 0, 0, &n)
printNest(n, 0, &sb)
s2 := sb.String()
fmt.Print(s2)
fmt.Println("\nRound trip test satisfied? ", s1 == s2)
}</lang>
Line 122 ⟶ 135:
wiki
mocks
==Indent form==
Line 132 ⟶ 145:
2 wiki
1 mocks
2 trolling
==Nest form==
Line 142 ⟶ 155:
wiki
mocks
</pre>▼
Round trip test satisfied? true
▲</pre>
=={{header|Python}}==
|