尾递归遍历节点树

用尾递归的形式找到节点的所有父节点,并且按照父节点到子节点的顺序排列。记录一下:

节点类:

public class Node
{
    public string Name { get; set; }
    public Node Parent { get; set; }
}

遍历:

private void traverse_node(Node node, Action<Node> con)
{
    if (node == null)
    {
        con(node);
        return;
    }
    traverse_node(node.Parent, (parent) =>
    {
        PlaceHolder1.Controls.Add(
            new Literal() { Text = node.Name + "<br />" });
        con(parent);
    });

}

调用:

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);

    Node node1 = new Node() { Name = "Node1" };
    Node node2 = new Node() { Name = "Node2" };
    Node node3 = new Node() { Name = "Node3" };
    Node node4 = new Node() { Name = "Node4" };
    Node node5 = new Node() { Name = "Node5" };

    node5.Parent = node4;
    node4.Parent = node3;
    node3.Parent = node2;
    node2.Parent = node1;

    traverse_node(node5, (n) => { });
}

输出的是:

Node1<br />
Node2<br />
Node3<br />
Node4<br />
Node5<br />

发表评论