如何将文本合格数据转换为树
我的数据如下所示:
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,则应将其下的元素作为节点添加到该分支。我想到了以下内容:
- 解析由每个元素的点字符限定的每一行并创建一个有序列表。
- 对于列表中的每个项目,将其添加为当前位置的节点。
- 添加之前请检查以确保同一级别的其他项目不存在同名。
- 添加下一个元素,直到列表中的所有项目都完成为止,下一个元素是列表中第一个添加的项目的子元素。
我希望我已经说清楚了,如果需要进一步澄清,请告诉我。
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:
- Parse each line that is qualified by the dot character for each element and create an ordered list.
- For each item in the list add it as a node in the current location.
- Before adding check to make sure another item at the same level does not exist with the same name.
- 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以更改 Node 类来进行检查,如果您想要子节点列表等,请将其添加为 HashSet,这样您就可以轻松地检查唯一性。在 Node 类中添加一个方法来执行 AddChild 并对 HashSet 进行检查。
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.
我假设存在方法
CreateRootNode
和AddChildNode
。I'm assuming the existence of the methods
CreateRootNode
andAddChildNode
.您需要一种递归方法来添加所有这些内容。这是一个示例:
使用:
示例方法:
注意:上面的代码未经测试,可能需要调整
A recursive method to add all of these is what you need. Here's a sample:
Use:
Sample Method:
NOTE: code above is not tested, might need tweaking