Jump to content

Visualize a tree: Difference between revisions

m (Added Sidef)
Line 317:
| `-- (null)
`-- (null)</pre>
=={{header|Elena}}==
<lang elena>#import system.
#import system'routines.
#import extensions.
 
#class Node
{
#field theValue.
#field theChildren.
#constructor new : value &children:children
[
theValue := value.
theChildren := children toArray.
]
#constructor new : value
<= new:value &children:nil.
#constructor new &children:children
<= new:emptyLiteralValue &children:children.
#constructor new : value &child:child
<= new:value &children:(Array new &object:child).
#method get = theValue.
#method children = theChildren.
}
 
#class(extension)treeOp
{
#method writeTree:node:prefix &subject:childrenProp
[
#var children := node::childrenProp get.
#var length := children length.
children zip:(RangeEnumerator new &from:1 &to:length) &eachPair:(:child:index)
[
self writeLine:prefix:"|".
self writeLine:prefix:"+---":(child get).
#var nodeLine := prefix + (index==length)iif:" ":"| ".
self writeTree:child:nodeLine &subject:childrenProp.
].
^ self.
]
#method writeTree:node &subject:childrenProp
= self writeTree:node:"" &subject:childrenProp.
}
 
#symbol program =
[
#var tree := Node new &children:
(
Node new:"a" &children:
(
Node new:"c" &child:(Node new:"d"),
Node new:"d"
),
Node new:"b").
console writeTree:tree &subject:%children.
].</lang>
{{out}}
<pre>
|
+---a
| |
| +---c
| | |
| | +---d
| |
| +---d
|
+---b
</pre>
 
=={{header|Erlang}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.