Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,
I am trying to automatically build a tree view by giving it the following values.
number of levels :Nlevels
number of children in each level: NchildrenAtEachLevel

Is this doable?

Any help would be highly appreciated.
Posted
Updated 28-Oct-10 22:28pm
v2
Comments
Arindam Tewary 29-Oct-10 2:59am    
This is achiveable/doable. :)
SuperAdministrator 29-Oct-10 3:01am    
can you provide me with any help regarding an algorithm ?
Dalek Dave 29-Oct-10 4:28am    
Edited for Grammar and Readability.

Equal No of Childs For Each Parent Node
Private Void FillTreeView(Treeview Tv,int NoofParents,int NoofChilds)
{
  for(int i=0;i< NoofParents;i++)
  {
    Tv.Nodes.Add(i);
    for(int j=0;i< NoofChilds;j++)
    {
       Tv.Nodes[i].Nodes.Add(j);
    }
  }
}
 
Share this answer
 
Comments
SuperAdministrator 29-Oct-10 3:25am    
This builds a tree with two levels ex: 2 parents with 3 children each . But i need to pass the number of levels so it has to be a kind of recursive method.
Try this. I modify the code posted by Mr. Pawan Kiran.

C#
private void FillTreeView(TreeView Tv, int NoOfLevels, int NoOfChildsOfEachLevel)
        {
            if (NoOfLevels < 0)
                return;
            for (int i = 0; i < NoOfChildsOfEachLevel; i++)
            {
                Tv.Nodes.Add(i.ToString());
                FillTreeNode(Tv.Nodes[i], NoOfLevels - 1, NoOfChildsOfEachLevel);
            }
        }

        private void FillTreeNode(TreeNode Tn, int NoOfLevels, int NoOfChildsOfEachLevel)
        {
            if (NoOfLevels < 0)
                return;
            for (int i = 0; i < NoOfChildsOfEachLevel; i++)
            {
                Tn.Nodes.Add(i.ToString());
                FillTreeNode(Tn.Nodes[i], NoOfLevels - 1, NoOfChildsOfEachLevel);
            }
        }
 
Share this answer
 
Comments
qontary 29-Oct-10 5:06am    
The restriction here is that, all nodes must have the same number of children.
SuperAdministrator 29-Oct-10 6:16am    
true , i will build on this and make the number of children in each level variable by :

private void FillTreeView(TreeView Tv, int NoOfLevels, int[] NoOfChildsOfEachLevel)
{
if (NoOfLevels < 0)
return;
for (int i = 0; i < NoOfChildsOfEachLevel[i]; i++)
{
Tv.Nodes.Add(i.ToString());
FillTreeNode(Tv.Nodes[i], NoOfLevels - 1, NoOfChildsOfLevel[i]);
}
}

private void FillTreeNode(TreeNode Tn, int NoOfLevels, int NoOfChildsOfEachLevel)
{
if (NoOfLevels < 0)
return;
for (int i = 0; i < NoOfChildsOfEachLevel; i++)
{
Tn.Nodes.Add(i.ToString());
FillTreeNode(Tn.Nodes[i], NoOfLevels - 1, NoOfChildsOfEachLevel);
}
}


You think this would fix it ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900