public static void Print(ITree tree, IConsole console, DisplayConfiguration config = null) { config = config ?? new DisplayConfiguration(); var stack = new Stack <(ITree tree, int depth, List <bool> last)>(); stack.Push((tree, 0, new List <bool>())); while (stack.Count > 0) { (var t, var currentDepth, var last) = stack.Pop(); var line = config.ShowLines ? GetLineWithLines(currentDepth, last, t.RootValue) : GetLineWithoutLines(currentDepth, t.RootValue); console.WriteLine(line.ToString()); if (config.MaxDepth > currentDepth) { var st = t.SubTrees.ToList(); for (var i = st.Count - 1; i >= 0; i--) { var newLast = new List <bool>(last); // TODO, this is quite wasteful newLast.Add(i == st.Count - 1); stack.Push((st[i], currentDepth + 1, newLast)); } } else if (t.SubTrees.Any()) { var newLast = new List <bool>(last); newLast.Add(true); stack.Push((new AdHocTree("(...)"), currentDepth + 1, newLast)); } } }
public static void Print(ITree tree, DisplayConfiguration config = null) { Print(tree, new InMemoryConsole(), config); }