Skip to content

Instantly share code, notes, and snippets.

@gmoothart
Created September 9, 2010 18:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gmoothart/572285 to your computer and use it in GitHub Desktop.
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