Rahul
Rahul

Reputation: 2234

Filling up a TreeView control

I have a N-Ary non sorted in any way tree and each node can have 0-N children. Given the data structure below, how can I fill the tree view assuming you have an array of TermNodes and that array is the first level of the TreeView? I have not been able to come up with a recursive way to do this.

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

Upvotes: 1

Views: 746

Answers (3)

Gishu
Gishu

Reputation: 136653

Just took out Generics for a spin.. Worked nicely. Worth a look at...

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);
       }
    }

Upvotes: 1

Rahul
Rahul

Reputation: 2234

Thanks All I was getting confused because I did not realize that for a given TreeNode tn, tn.Nodes.Add would return the added TreeNode Once you know that the solution is straight forward like so

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);               
    }
 }

and to start of the recursive call

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);
          }
     }

  }

Upvotes: 0

Tomalak
Tomalak

Reputation: 338326

Here is a bit of code to get you started with the recursion. It's not tested (I can't right now), but you should get the idea:

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);

Upvotes: 1

Related Questions