Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
64
Task/Visualize-a-tree/Elena/visualize-a-tree.elena
Normal file
64
Task/Visualize-a-tree/Elena/visualize-a-tree.elena
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
import system'routines;
|
||||
import extensions;
|
||||
|
||||
class Node
|
||||
{
|
||||
string theValue;
|
||||
Node[] theChildren;
|
||||
|
||||
constructor new(string value, Node[] children)
|
||||
{
|
||||
theValue := value;
|
||||
|
||||
theChildren := children;
|
||||
}
|
||||
|
||||
constructor new(string value)
|
||||
<= new(value, new Node[](0));
|
||||
|
||||
constructor new(Node[] children)
|
||||
<= new(emptyString, children);
|
||||
|
||||
get() = theValue;
|
||||
|
||||
Children = theChildren;
|
||||
}
|
||||
|
||||
extension treeOp
|
||||
{
|
||||
writeTree(node, prefix)
|
||||
{
|
||||
var children := node.Children;
|
||||
var length := children.Length;
|
||||
|
||||
children.zipForEach(new Range(1, length), (child,index)
|
||||
{
|
||||
self.printLine(prefix,"|");
|
||||
self.printLine(prefix,"+---",child.get());
|
||||
|
||||
var nodeLine := prefix + (index==length).iif(" ","| ");
|
||||
|
||||
self.writeTree(child,nodeLine);
|
||||
});
|
||||
|
||||
^ self
|
||||
}
|
||||
|
||||
writeTree(node)
|
||||
= self.writeTree(node,"");
|
||||
}
|
||||
|
||||
public program()
|
||||
{
|
||||
var tree := Node.new(
|
||||
new Node[]{
|
||||
Node.new("a", new Node[]
|
||||
{
|
||||
Node.new("b", new Node[]{Node.new("c")}),
|
||||
Node.new("d")
|
||||
}),
|
||||
Node.new("e")
|
||||
});
|
||||
|
||||
console.writeTree(tree).readChar()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue