如何将文本合格数据转换为树

发布于 2024-12-05 17:08:39 字数 480 浏览 0 评论 0原文

我的数据如下所示:

a.b.c.d.e.f.g
b.c.d.e.f.g.h.x
c.d.e.q.s.n.m.y
a.b.c

我需要获取这些数据并将每个级别转换为树视图中的一个节点。所以树看起来像这样:

a
  b
    c
      d
        e
...
b
  c
    d
....

如果例如在同一级别有另一个 a,则应将其下的元素作为节点添加到该分支。我想到了以下内容:

  1. 解析由每个元素的点字符限定的每一行并创建一个有序列表。
  2. 对于列表中的每个项目,将其添加为当前位置的节点。
  3. 添加之前请检查以确保同一级别的其他项目不存在同名。
  4. 添加下一个元素,直到列表中的所有项目都完成为止,下一个元素是列表中第一个添加的项目的子元素。

我希望我已经说清楚了,如果需要进一步澄清,请告诉我。

I have data that looks like the below:

a.b.c.d.e.f.g
b.c.d.e.f.g.h.x
c.d.e.q.s.n.m.y
a.b.c

I need to take this data and turn each and every level into a node in a treeview. So the tree looks something like:

a
  b
    c
      d
        e
...
b
  c
    d
....

if for example at the same leve there is another a, elements under this should be added as nodes to that branch. I have thought of the following:

  1. Parse each line that is qualified by the dot character for each element and create an ordered list.
  2. For each item in the list add it as a node in the current location.
  3. Before adding check to make sure another item at the same level does not exist with the same name.
  4. Add the next element until all items in the list are done, next elements being child to the first added item of the list.

I hope I was clear and let me know if it needs further clarification.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抚你发端 2024-12-12 17:08:39

您可以更改 Node 类来进行检查,如果您想要子节点列表等,请将其添加为 HashSet,这样您就可以轻松地检查唯一性。在 Node 类中添加一个方法来执行 AddChild 并对 HashSet 进行检查。

public class Main
    {
        public Main()
        {
            string treeStr = "";

            string[] strArr = { "a.b.c.d.e.f.g", "b.c.d.e.f.g.h.x" };

            List<Node> nodes = new List<Node>();
            Node currentNode;

            foreach (var str in strArr)
            {
                string[] split = str.Split('.');
                currentNode = null;

                for (int i = 0; i < split.Length; i++)
                {
                    var newNode = new Node { Value = str };

                    if (currentNode != null)
                    {
                        currentNode.Child = newNode;
                    }
                    else
                    {
                        nodes.Add(newNode);
                    }

                    currentNode = newNode;
                }
            }
        }


    }

    public class Node
    {
        public string Value { get; set; }
        public Node Child { get; set; }
    }

You can change the Node class to have the checks, if you want a list of children nodes, etc, add that as a HashSet, so you can easily make the check for uniqueness. Add a method in the Node class to do the AddChild and do the check on the HashSet.

public class Main
    {
        public Main()
        {
            string treeStr = "";

            string[] strArr = { "a.b.c.d.e.f.g", "b.c.d.e.f.g.h.x" };

            List<Node> nodes = new List<Node>();
            Node currentNode;

            foreach (var str in strArr)
            {
                string[] split = str.Split('.');
                currentNode = null;

                for (int i = 0; i < split.Length; i++)
                {
                    var newNode = new Node { Value = str };

                    if (currentNode != null)
                    {
                        currentNode.Child = newNode;
                    }
                    else
                    {
                        nodes.Add(newNode);
                    }

                    currentNode = newNode;
                }
            }
        }


    }

    public class Node
    {
        public string Value { get; set; }
        public Node Child { get; set; }
    }
挽袖吟 2024-12-12 17:08:39

我假设存在方法 CreateRootNodeAddChildNode

void ParseToTreeview(IEnumerable<string> data) {
    foreach (var line in data) {
        var names = line.Split('.');
        for (var i = 0; i < names.Length; i++) {
            TreeNode node = null;
            if (i == 0)
                node = CreateRootNode(name:names[i]);
            else
                node = AddChildNode(name:names[i], parentNode:node);
        }
    }
}

I'm assuming the existence of the methods CreateRootNode and AddChildNode.

void ParseToTreeview(IEnumerable<string> data) {
    foreach (var line in data) {
        var names = line.Split('.');
        for (var i = 0; i < names.Length; i++) {
            TreeNode node = null;
            if (i == 0)
                node = CreateRootNode(name:names[i]);
            else
                node = AddChildNode(name:names[i], parentNode:node);
        }
    }
}
万人眼中万个我 2024-12-12 17:08:39

您需要一种递归方法来添加所有这些内容。这是一个示例:

使用:

string[] yourListOfData = { "a.b.c.d.e.f.g", "b.c.d.e.f.g.h.x", "c.d.e.q.s.n.m.y", "a.b.c" };
foreach(string x in yourListOfData)
  PopulateTreeView(x, myTreeView.Nodes[0]);

示例方法:

  public void PopulateTreeView(string values, TreeNode parentNode )
  {
      string nodeValue = values;
      string additionalData = values.Substring(value.Length - (value.Length - 2));

      try
      {
         if (!string.IsNullOrEmpty(nodeValue))
         {
            TreeNode myNode = new TreeNode(nodeValue);
            parentNode.Nodes.Add(myNode);
            PopulateTreeView(additionalData, myNode);
         }
      } catch ( UnauthorizedAccessException ) {
        parentNode.Nodes.Add( "Access denied" );
      } // end catch
  }

注意:上面的代码未经测试,可能需要调整

A recursive method to add all of these is what you need. Here's a sample:

Use:

string[] yourListOfData = { "a.b.c.d.e.f.g", "b.c.d.e.f.g.h.x", "c.d.e.q.s.n.m.y", "a.b.c" };
foreach(string x in yourListOfData)
  PopulateTreeView(x, myTreeView.Nodes[0]);

Sample Method:

  public void PopulateTreeView(string values, TreeNode parentNode )
  {
      string nodeValue = values;
      string additionalData = values.Substring(value.Length - (value.Length - 2));

      try
      {
         if (!string.IsNullOrEmpty(nodeValue))
         {
            TreeNode myNode = new TreeNode(nodeValue);
            parentNode.Nodes.Add(myNode);
            PopulateTreeView(additionalData, myNode);
         }
      } catch ( UnauthorizedAccessException ) {
        parentNode.Nodes.Add( "Access denied" );
      } // end catch
  }

NOTE: code above is not tested, might need tweaking

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文