views:

372

answers:

3

OK this is going to sound hilarious but still I have a class like so

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

So this is a N-Ary non sorted in any way Tree. A node can have 0-N children

OK so given this data structure how will you fill up a tree View with it Assume you have an array of TermNodes so that array form teh first level of the TreeVIew I am just not able to come up with a recursive way to do this

+1  A: 

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);
Tomalak
A: 

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

  }
Rahul
+1  A: 

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