Jump to content

Visualize a tree: Difference between revisions

Line 514:
`-- (null)</pre>
=={{header|Elena}}==
ELENA 3.2 :
<lang elena>#import system'routines.
#<lang elena>import extensionssystem'routines.
import extensions.
 
class Node
Line 522 ⟶ 523:
object theChildren.
constructor new : value &children:children
[
theValue := value.
Line 530 ⟶ 531:
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).
get = theValue.
Line 545 ⟶ 546:
extension treeOp
{
writeTree : node : prefix &subject: childrenProp
[
var children := node::~childrenProp get.
var length := children length.
children zip:(RangeEnumerator new &from:1 &to:length) &eachPair:forEach(:child:index)
[
self writeLine:printLine(prefix:,"|").
self writeLine:printLine(prefix:,"+---":(,child get).
var nodeLine := prefix + (index==length)iif:(" ":,"| ").
self writeTree:(child:,nodeLine &subject:,childrenProp).
].
Line 563 ⟶ 564:
]
writeTree : node &subject: childrenProp
= self~treeOp writeTree:(node:,"" &subject:,childrenProp).
}
 
program =
[
var tree := Node new &children:
(
Node new:"a" &children:
(
Node new:"b" &child:(Node new:"c"),
Node new:"d"
),
Line 579 ⟶ 580:
).
console writeTree:(tree, &subject:%children); readChar.
].</lang>
{{out}}
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.