Search code examples
c#algorithmtreeviewtree-traversal

C# - TreeView - Data Algorithm


I work with C# WPF TreeView.

I have folowing input data: (like 1000+ lines that I need to process)

\Folder\Base\Data\dat.cfg
\Folder\Base\Data1\dat.cfg
\Folder\dat.cfg

Collection class - TreeItem. Used for TreView binding.

    public class TreeItem : MyViewModel
    {
        public TreeItem()
        {
            this.Children = new ObservableCollection<TreeItem>();
        }

        public string Name { get; set; }

        public ObservableCollection<TreeItem> Children { get; set; }
    }

I'm spending on this fews days and I'm not able to figure out algorithm how to obtain data to requested point.

Please I would really appreciate if somebody could give me help or just way how to achieve it.

Thank you in advance.

This is my class for collection

I need to acomplish input data to be parsed as following in my defined collection class - TreeItem. Basicaly just to be working in TreeView as nodes with levels.

Requested data structure


CollectionTreeItem
[0] Name - "Folder"
    Children
    [0] Name - "Base"
        Children 
            [0] Name - "Data"
            Children 
                [0] Name - "dat.cfg"
                Children 

Only example with procesed line - \Folder\Base\Data\dat.cfg. Of course there will be more child with another lines.


Solution

  • You could use a recursive algorithm to build the tree. treeObjects should hold the structure you're trying to achieve.

    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    
    
    class Program
    {
        static void Main()
        {
            string[] paths = new string[]
            {
                @"\Folder\Base\Data\dat.cfg",
                @"\Folder\Base\Data1\dat.cfg",
                @"\Folder\dat.cfg"
                // Rest of your paths
            };
    
            var root = new TreeItem { Name = "Root" };
    
            BuildTree(root, paths);
    
            var treeObjects = GetTreeObjects(root);
        }
    
        static void BuildTree(TreeItem root, string[] paths)
        {
            foreach (var path in paths)
            {
                AddPathToTree(root, path.Split('\\').Skip(1).ToArray());
            }
        }
    
        static void AddPathToTree(TreeItem root, string[] path)
        {
            TreeItem currentNode = root;
    
            foreach (var folder in path)
            {
                var existingNode = currentNode.Children.FirstOrDefault(c => c.Name == folder);
    
                if (existingNode == null)
                {
                    var newNode = new TreeItem { Name = folder };
                    currentNode.Children.Add(newNode);
                    currentNode = newNode;
                }
                else
                {
                    currentNode = existingNode;
                }
            }
        }
    
        static List<CollectionTreeItem> GetTreeObjects(TreeItem node)
        {
            var result = new List<CollectionTreeItem>
            {
                new CollectionTreeItem
                {
                    Name = node.Name,
                    Children = GetChildrenObjects(node.Children)
                }
            };
    
            return result;
        }
    
        static List<CollectionTreeItem> GetChildrenObjects(ObservableCollection<TreeItem> children)
        {
            var result = new List<CollectionTreeItem>();
    
            foreach (var child in children)
            {
                var childObject = new CollectionTreeItem
                {
                    Name = child.Name,
                    Children = GetTreeObjects(child)
                };
    
                result.Add(childObject);
            }
    
            return result;
        }
    }
    
    public class CollectionTreeItem
    {
        public string Name { get; set; }
    
        public List<CollectionTreeItem> Children { get; set; }
    }