Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
20
Task/Visualize-a-tree/D/visualize-a-tree.d
Normal file
20
Task/Visualize-a-tree/D/visualize-a-tree.d
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import std.stdio, std.conv, std.algorithm, std.array;
|
||||
|
||||
struct Node(T) { T value; Node* left, right; }
|
||||
|
||||
string[] treeIndent(T)(in Node!T* t) pure nothrow @safe {
|
||||
if (!t) return ["-- (null)"];
|
||||
const tr = t.right.treeIndent;
|
||||
return "--" ~ t.value.text ~
|
||||
t.left.treeIndent.map!q{" |" ~ a}.array ~
|
||||
(" `" ~ tr[0]) ~ tr[1 .. $].map!q{" " ~ a}.array;
|
||||
}
|
||||
|
||||
void main () {
|
||||
static N(T)(T v, Node!T* l=null, Node!T* r=null) {
|
||||
return new Node!T(v, l, r);
|
||||
}
|
||||
|
||||
const tree = N(1, N(2, N(4, N(7)), N(5)), N(3, N(6, N(8), N(9))));
|
||||
writefln("%-(%s\n%)", tree.treeIndent);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue