Created
September 9, 2010 18:28
-
-
Save gmoothart/572285 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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