Visualize a tree: Difference between revisions

C# made it into an extension method.
(Added C#)
(C# made it into an extension method.)
Line 1,019:
public static class VisualizeTree
{
public static void Main2Main() {
"A".t(
"B0".t(
Line 1,041:
 
private static Tree t(this string value, params Tree[] children) => new Tree(value, children);
 
private publicstatic void Print(this Tree tree) => tree.Print(true, "");
 
private static void Print(this Tree tree, bool last, string prefix) {
(string mycurrent, string next) = last
? (prefix + "└─" + tree.Value, prefix + " ")
: (prefix + "├─" + tree.Value, prefix + "| ");
Console.WriteLine(mycurrent[2..]);
for (int c = 0; c < tree.Children.Length; c++) Children[c].Print(c == Children.Length - 1, next);{
tree.Children[c].Print(c == tree.Children.Length - 1, next);
}
}
 
class Tree
Line 1,048 ⟶ 1,060:
public string Value { get; }
public Tree[] Children { get; }
 
public void Print() => Print(true, "");
 
private void Print(bool last, string prefix) {
(string my, string next) = last
? (prefix + "└─" + Value, prefix + " ")
: (prefix + "├─" + Value, prefix + "| ");
Console.WriteLine(my[2..]);
for (int c = 0; c < Children.Length; c++) Children[c].Print(c == Children.Length - 1, next);
}
}
196

edits