How do I print out a tree structure?

How do I print out a tree structure?
http://stackoverflow.com/questions/1649027/how-do-i-print-out-a-tree-structure

多謝熊神給的鏈接,圓滿的解決了我的問題。語法樹本來自己寫的醜的不忍直視。。現在好看多了,哈哈

其中最短小精悍的是節點中打印連線,很棒。
class Node
{
public string Name;
public decimal Time;
public List Children = new List();

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(“”);
這是C#,都是C嘛。
mark 留念~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章