How do I print out a tree structure?
-
I'm trying to improve performance in our app. I've got performance information in the form of a tree of calls, with the following node class: public class Node { public string Name; // method name public decimal Time; // time spent in method public List<Node> Children; } I want to print out the tree such that I can see lines between the nodes - something like in http://stackoverflow.com/questions/1581559/ascii-library-for-creating-pretty-directory-trees. What's an algorithm I can use in C# for doing that? Edit: Obviously I need to use recursion - but my attempts keep putting the lines in the wrong places. What I'm asking for is a specific algorithm that will print the tree in a nice manner - the details of when to print a vertical line and when to print a horizontal one. Edit: It isn't sufficient just to use copies of a string to indent the nodes. I'm not looking for A |-B |-|-C |-|-D |-|-|-E |-F |-|-G it has to be A +-B | +-C | +-D | +-E +-F +-G or anything similar, so long as the tree structure is visible. Notice that C and D are indented differently to G - I can't just use a repeated string to indent the nodes.
-
Answer:
The trick is to pass a string as the indent and to treat the last child specially: class Node { public void PrintPretty(string indent, bool last) { Console.Write(indent); if (last) { Console.Write("\\-"); indent += " "; } else { Console.Write("|-"); indent += "| "; } Console.WriteLine(Name); for (int i = 0; i < Children.Count; i++) Children[i].PrintPretty(indent, i == Children.Count - 1); } } If called like this: root.PrintPretty("", true); will output in this style: \-root \-child |-child \-child |-child |-child \-child |-child |-child | |-child | \-child | |-child | |-child | |-child | \-child | \-child | \-child \-child |-child |-child |-child | \-child \-child \-child
Simon at Stack Overflow Visit the source
Other answers
Create PrintNode method and use recursion: class Node { public string Name; public decimal Time; public List<Node> Children = new List<Node>(); public void PrintNode(string prefix) { Console.WriteLine("{0} + {1} : {2}", prefix, this.Name, this.Time); foreach (Node n in Children) if (Children.IndexOf(n) == Children.Count - 1) n.PrintNode(prefix + " "); else n.PrintNode(prefix + " |"); } } ANd then to print the whole tree just execute: topNode.PrintNode(""); In my example it would give us something like that: + top : 123 | + Node 1 : 29 | | + subnode 0 : 90 | | + sdhasj : 232 | | + subnode 1 : 38 | | + subnode 2 : 49 | | + subnode 8 : 39 | + subnode 9 : 47 + Node 2 : 51 | + subnode 0 : 89 | + sdhasj : 232 | + subnode 1 : 33 + subnode 3 : 57
Gacek
If you happen to have a very deep tree and your call stack size is limited, you can instead do a static, non-recursive tree traversal like so: public static void PrintTree(Node tree) { List<Node> firstStack = new List<Node>(); firstStack.Add(tree); List<List<Node>> childListStack = new List<List<Node>>(); childListStack.Add(firstStack); while (childListStack.Count > 0) { List<Node> childStack = childListStack[childListStack.Count - 1]; if (childStack.Count == 0) { childListStack.RemoveAt(childListStack.Count - 1); } else { tree = childStack[0]; childStack.RemoveAt(0); string indent = ""; for (int i = 0; i < childListStack.Count - 1; i++) { indent += (childListStack[i].Count > 0) ? "| " : " "; } Console.WriteLine(indent + "+- " + tree.Name); if (tree.Children.Count > 0) { childListStack.Add(new List<Node>(tree.Children)); } } } } Which would output something like this: +- root +- branch-A | +- sibling-X | | +- grandchild-A | | +- grandchild-B | +- sibling-Y | | +- grandchild-C | | +- grandchild-D | +- sibling-Z | +- grandchild-E | +- grandchild-F +- branch-B +- sibling-J +- sibling-K
Joshua Stachowski
Related Q & A:
- How do I print out a list of my contacts email addresses?Best solution by Yahoo! Answers
- How can I print wirelessly from a laptop to a printer without a router?Best solution by answers.yahoo.com
- How do I print green color if I will use a yellow color paper?Best solution by ehow.com
- How do I change to a larger font when I print an email?Best solution by email.about.com
- How do I print off a brochure on Open Office?Best solution by openoffice.blogs.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.