public static string Dump(Node root)
        {
            StringBuilder nodeDump = new StringBuilder();
            Dump(root, nodeDump, String.Empty);

            return nodeDump.ToString();
        }
Esempio n. 2
0
        public void RunProgram()
        {
            var root =
                new Node(
                    "a",
                    new Node(
                        "b",
                        new Node(
                            "c",
                            new Node("d")
                        ),
                        new Node(
                            "e",
                            new Node("f")
                        )
                    ),
                    new Node(
                        "g",
                        new Node(
                            "h",
                            new Node("i")
                        ),
                        new Node("j")
                    )
                );

            string output = DepthFirstDumper.Dump(root);
            File.WriteAllText(@"C:\tmp\treedump.txt", output);
            Console.WriteLine(output);
        }
        private static void Dump(Node node, StringBuilder nodeDump, string prefix)
        {
            nodeDump.AppendLine(node.Text);
            for (int i = 0; i < node.Children.Count; i++)
            {
                bool isLastChild = i + 1 == node.Children.Count;
                nodeDump.Append(prefix);
                nodeDump.Append(isLastChild ? '└' : '├');
                nodeDump.Append('─');

                Dump(node.Children[i], nodeDump, prefix + (isLastChild ? "  " : "│ "));
            }
        }