在树结构上实现IEnumerable

时间:2009-12-18 14:50:09

标签: c# collections tree ienumerable treenode

基于这些人的工作:

我正在尝试实现一个可以这样使用的TreeView助手:

<%= Html.TreeView("records", 
                  Library.Instance.Records, 
                  r => r.Children, 
                  r => r.ID) %>

树结构的定义如下:

public class Tree<T> : TreeNode<T> where T : TreeNode<T>
{ }


public class TreeNode<T> : IDisposable where T : TreeNode<T>
{
    public T Parent { get; set; }
    public TreeNodeList<T> Children { get; set; }
}


public class TreeNodeList<T> : List<TreeNode<T>> where T : TreeNode<T>
{
    public T Parent;

    public T Add(T node)
    {
        base.Add(node);
        node.Parent = (T)Parent;
        return node;
    }

    public void Remove(T node)
    {
        if (node != null)
            node.Parent = null;
        base.Remove(node);
    }
}

TreeView助手有这个签名:

public static string TreeView<T>(this HtmlHelper htmlHelper, string treeId,
   IEnumerable<T> rootItems, Func<T, IEnumerable<T>> childrenProperty, 
   Func<T, string> itemContent, bool includeJavascript, string emptyContent)
{
    ...
}   

因此,我需要我的Tree struture来实现IEnumerable,所以我可以将它与TreeView帮助器一起使用,这就产生了一个问题:在这种情况下我将在何处以及如何实现IEnumerable?

2 个答案:

答案 0 :(得分:5)

对于想要实现递归树迭代器的人来说,Wes Dyer的这篇文章很好读:

http://blogs.msdn.com/wesdyer/archive/2007/03/23/all-about-iterators.aspx

答案 1 :(得分:1)

我并不完全了解树结构的确切细节,但这是一个简单的实现,它采用通用的节点树并递归地将其呈现为html列表。

public static string TreeView<T>(IEnumerable<T> rootItems,
                                 Func<T, IEnumerable<T>> childrenProperty,
                                 Func<T, string> itemContent)
{
    if (rootItems == null || !rootItems.Any()) return null;

    var builder = new StringBuilder();
    builder.AppendLine("<ul>");

    foreach (var item in rootItems)
    {
        builder.Append("  <li>").Append(itemContent(item)).AppendLine("</li>");
        var childContent = htmlHelper.TreeView(treeId,
                                               childrenProperty(item),
                                               childrenProperty,
                                               itemContent);

        if (childContent != null)
        {
            var indented = childContent.Replace(Environment.NewLine,
                                                Environment.NewLine + "  ");
            builder.Append("  ").AppendLine(indented);
        }
    }

    builder.Append("</ul>");
    return builder.ToString();
}

我使用的节点类相对简单,只有两个属性。

public class Node<T>
{
    public Node(T data)
    {
        Data = data;
        Children = new List<Node<T>>();
    }

    public T Data { get; private set; }
    public ICollection<Node<T>> Children { get; private set; }
}

这是一些将树输出到控制台的测试代码。

var Records = new[] {
    new Node<string>("one") {
        Children = {
            new Node<string>("one-one") {
                Children = {
                    new Node<string>("one-one-one"),
                    new Node<string>("one-one-two"),
                    new Node<string>("one-one-three")
                }
            },
            new Node<string>("one-two"),
            new Node<string>("one-three")
        }
    },
    new Node<string>("two"),
    new Node<string>("three")
};
Console.WriteLine(TreeView(Records,
                           r => r.Children,
                           r => r.Data));

以下是上述代码的结果。

<ul>
  <li>one</li>
  <ul>
    <li>one-one</li>
    <ul>
      <li>one-one-one</li>
      <li>one-one-two</li>
      <li>one-one-three</li>
    </ul>
    <li>one-two</li>
    <li>one-three</li>
  </ul>
  <li>two</li>
  <li>three</li>
</ul>