填写TreeView控件

时间:2008-11-24 07:12:58

标签: c# data-structures treeview

我有一个N-Ary没有以任何方式排序树,每个节点可以有0-N个孩子。鉴于下面的数据结构,假设您有一个TermNode数组并且该数组是TreeView的第一级,我如何填充树视图?我无法想出一种递归方式来做到这一点。

class TermNode
{
   public string Name;
   public string Definition;
   public List<TermNode> Children
}

3 个答案:

答案 0 :(得分:1)

这里有一些代码可以帮助您开始递归。它没有经过测试(我现在不能),但你应该明白这个想法:

public static void BuildTreeView(TreeNodeCollection Parent, List<TermNode> TermNodeList)
{
  foreach (TermNode n in TermNodeList)
  {
    TreeNode CurrentNode = Parent.Add(n.Name);
    // no need to recurse on empty list
    if (n.List.Count > 0) BuildTreeView(CurrentNode.Nodes, n.List);
  }
}

// initial call
List<TermNode> AllTermNodes = /* all your nodes at root level */;

BuildTreeView(treeView1.Nodes, AllTermNodes);

答案 1 :(得分:1)

刚拿出Generics旋转..工作得很好。值得一看...

public interface INode<T>
{
    List<T> Children { get; }
}
class TermNode:INode<TermNode>
{
   public string Name;
   public string Definition;
   public List<TermNode> Children { get; set; }
   public TermNode()
   {
       this.Children = new List<TermNode>();
   }
}

public class TreeBuilder<T> where T : INode<T>
{
    public Func<T, TreeNode> obCreateNodeFunc;

    public void AddNode(TreeView obTreeView, T obNodeToAdd, TreeNode obParentNodeIfAny) 
    {
        TreeNodeCollection obNodes;
        if (obParentNodeIfAny == null)
        {
            obNodes = obTreeView.Nodes;
        }
        else
        {
            obNodes = obParentNodeIfAny.Nodes;
        }
        int iNewNodeIndex = obNodes.Add(obCreateNodeFunc(obNodeToAdd));
        TreeNode obNewNode = obNodes[iNewNodeIndex];

        foreach (T child in obNodeToAdd.Children)
        {
            AddNode(obTreeView, child, obNewNode);
        }
    }
}

// calling code - Some class
    static TreeNode GetTreeNodeFor(TermNode t)
    {
        return new TreeNode(t.Name);  // or any logic that returns corr TreeNode for T
    }

    void Main()...
    {
       TermNode[] arrNodesList;    
       // populate list with nodes

       TreeBuilder<TermNode> tb = new TreeBuilder<TermNode>();
       tb.obCreateNodeFunc = GetTreeNodeFor;
       foreach (TermNode obNode in arrNodesList)
       {
           tb.AddNode(treeView, obNode, null);
       }
    }

答案 2 :(得分:0)

谢谢所有我感到困惑,因为我没有意识到对于给定的TreeNode tn,tn.Nodes.Add将返回添加的TreeNode 一旦你知道解决方案是直截了当的

private void /*TreeNode*/ RecursiveAdd(OntologyNode on, TreeNode tn)
{
    if (on.Children.Count == 0)
    {
        return;             
    }
    foreach (OntologyNode child in on.Children)
    {
        TreeNode tCur = tn.Nodes.Add(child.Name);
        tCur.Tag = child;//optional for some selected node events
        RecursiveAdd(child, tCur);               
    }
 }

并开始递归调用

foreach( OntologyNode on in Nodes )
 {
     if (on.IsTopLevelNode == true)// internal not pertinent to this code snippet
     {
          TreeNode tn = tvOntoBrowser.Nodes.Add(on.Name);
          tn.Tag = on;
          if (on.Children.Count > 0)
          {
               RecursiveAdd(on, tn);
          }
     }

  }