Visualize a tree: Difference between revisions
Content added Content deleted
(promoted: no issues raised, plenty of examples, should be ok) |
(Go solution) |
||
Line 119: | Line 119: | ||
| `-- (null) |
| `-- (null) |
||
`-- (null)</pre> |
`-- (null)</pre> |
||
=={{header|Go}}== |
|||
Not the most economical output, but at least json.MarshalIndent is in the Go standard library. Note that the definition of Node has nothing JSON specific about it; it's an ordinary struct. |
|||
<lang Go>package main |
|||
import ( |
|||
"encoding/json" |
|||
"fmt" |
|||
"log" |
|||
) |
|||
type Node struct { |
|||
Name string |
|||
Children []*Node |
|||
} |
|||
func main() { |
|||
tree := &Node{"root", []*Node{ |
|||
&Node{"a", []*Node{ |
|||
&Node{"d", nil}, |
|||
&Node{"e", []*Node{ |
|||
&Node{"f", nil}, |
|||
}}}}, |
|||
&Node{"b", nil}, |
|||
&Node{"c", nil}, |
|||
}} |
|||
b, err := json.MarshalIndent(tree, "", " ") |
|||
if err != nil { |
|||
log.Fatal(err) |
|||
} |
|||
fmt.Println(string(b)) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
{ |
|||
"Name": "root", |
|||
"Children": [ |
|||
{ |
|||
"Name": "a", |
|||
"Children": [ |
|||
{ |
|||
"Name": "d", |
|||
"Children": null |
|||
}, |
|||
{ |
|||
"Name": "e", |
|||
"Children": [ |
|||
{ |
|||
"Name": "f", |
|||
"Children": null |
|||
} |
|||
] |
|||
} |
|||
] |
|||
}, |
|||
{ |
|||
"Name": "b", |
|||
"Children": null |
|||
}, |
|||
{ |
|||
"Name": "c", |
|||
"Children": null |
|||
} |
|||
] |
|||
} |
|||
</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 159: | Line 226: | ||
`-- (nil) |
`-- (nil) |
||
</pre> |
</pre> |
||
=={{header|J}}== |
=={{header|J}}== |
||