question

BigBear avatar image
0 Votes"
BigBear asked BigBear commented

C# Creating Binary Tree Treeview from folders and files

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; }
 }
dotnet-csharp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered BigBear commented

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.  

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I built an object that has the parent name if it's a child and the index where the node should be (level) with these 3 things I was thinking that is all I would need to easily build the tree. but I am having trouble thinking of a solution/method to build and use these properties.

0 Votes 0 ·