Visualize a tree: Difference between revisions
Content deleted Content added
Added C# |
C# made it into an extension method. |
||
Line 1,019:
public static class VisualizeTree
{
public static void
"A".t(
"B0".t(
Line 1,041:
private static Tree t(this string value, params Tree[] children) => new Tree(value, children);
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);
▲ }
}
|