This commit is contained in:
Ingy döt Net 2013-10-27 22:24:23 +00:00
parent 6f050a029e
commit 776bba907c
3887 changed files with 59894 additions and 7280 deletions

View file

@ -1,20 +1,20 @@
import std.stdio, std.conv, std.algorithm, std.array;
struct BTNode(T) { T value; BTNode* left, right; }
struct Node(T) { T value; Node* left, right; }
string[] treeIndent(T)(in BTNode!T* t) {
if (t is null) return ["-- (null)"];
const tr = treeIndent(t.right);
return text("--", t.value) ~
map!q{" |" ~ a}(treeIndent(t.left)).array() ~
(" `" ~ tr[0]) ~ map!q{" " ~ a}(tr[1..$]).array();
string[] treeIndent(T)(in Node!T* t) {
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, BTNode!T* l=null, BTNode!T* r=null) {
return new BTNode!T(v, l, r);
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());
writefln("%-(%s\n%)", tree.treeIndent);
}