C# Creating Binary Tree Treeview from folders and files

Andrew Harkins 46 Reputation points
2022-04-25T19:50:02.64+00:00

I am trying to create my own treeview binary tree where it shows folders and children / parents for certain folders. I have a big list of folders then I have it in a custom object the Index, Parent Name, Children bool, and display name on the tree. I can create the tree but when it creates it all the children are under the first node and not under the correct ones. I see that treenode has level and parent if I could set those then this would be easy but I see they are readonly so I’m kinda stuck so far…

            List<TreeNodeEnd> lstTreeNod = new List<TreeNodeEnd>();
                Dictionary<int, TreeNode> valuePairs = new Dictionary<int, TreeNode>();
                        foreach (var node in lstTreeNod)
                        {
                            TreeNodeCollection items;
                            TreeNode treeNode;
                            if (node.TreeIndex == 0)
                            {
                                //node.DisplayName = "PDM Vault";
                                //TreeNode root2 = new TreeNode("PDM Vault", 3, 3, items);
                                //root.Name = "PDM Vault";
                                items = treeView1.Nodes;
                                //items = items.Add(node.DisplayName);
                                //treeView1.Nodes
                            }
                            else  //in this ELSE DETERMINE ABOUT FATHER & CHILDREN EQUALS AND NOT NULL
                            {       // ValuePairs = Final Tree ?

                                //items = valuePairs[node.TreeIndex - 1].Nodes;

                                items = valuePairs[node.TreeIndex].Nodes;

                                TreeNode treeNode = items.Add(node.DisplayName);


                            }

                            TreeNode treeNode = items.Add(node.DisplayName);

                            if (valuePairs.ContainsKey(node.TreeIndex))
                            {
                                if (node.HasChildren == true)
                                    valuePairs[node.TreeIndex] = treeNode;
                            }
                            else
                            {
                                valuePairs.Add(node.TreeIndex, treeNode);
                            }
}

public class TreeNodeEnd
{
    public string DisplayName { get; set; }
    public int TreeIndex { get; set; }
    public bool HasChildren { get; set; }
    public string Father { get; set; }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,315 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 57,166 Reputation points
    2022-04-25T22:38:09.72+00:00

    to build a Binary Tree, you need a tree data structure

         public class TreeNode  
         {  
             private List<TreeNode> _children = new List<TreeNode>();  
    
             public string DisplayName { get; set; }  
             public IEnumable<TreeNode> Children => _children;  
             public TreeNode? Father { get; set; } //null if root   
    
             public void AddChild (TreeNode child)   
             {  
                   child.Parent = this;  
                   _children.Add(child);  
             }  
             public void DeleteChild (TreeNode child)   
             {  
                   _children.Remove(child);  
             }  
        }  
    
    
    easy to fill recursively from directory.