Skip to content

@gmoothart /tree.cs
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
sealed class Dumper
{
static public string Dump(Node root) {
return Dump(root, new List<bool>{});
}
// isLastPath - for every node in the current path, is it the last child?
static internal string Dump(Node n, List<bool> isLastPath) {
string bar = "¦";
string junction = "+";
string line = "-";
string turn = "+";
string result = "";
int depth = (isLastPath.Count);
// ascii-art nodes
if (depth > 0) {
bool nodeIsLastChild = isLastPath.Last();
// leave out leaf node
IEnumerable<bool> ancestorsAreLast = isLastPath.Take(isLastPath.Count-1);
// draw lines for parents
foreach(bool isLast in ancestorsAreLast) {
if (isLast) {
result += " ";
}
else {
result += bar + " ";
}
}
// draw lines for child
if (nodeIsLastChild) {
result += turn + line;
}
else {
result += junction + line;
}
}
// draw text node
result += n.Text + "\n";
int num = n.Children.Count;
int j = 0;
foreach(Node child in n.Children) {
List<bool> newPath = new List<bool>();
newPath.AddRange(isLastPath);
newPath.Add( j == num-1 );
result += Dump(child, newPath);
j++;
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.