C# 生成层次结构的算法
我有一个如下所示的文本文件:
{ Id = 1, ParentId = 0, Position = 0, Title = "root" }
{ Id = 2, ParentId = 1, Position = 0, Title = "child 1" }
{ Id = 3, ParentId = 1, Position = 1, Title = "child 2" }
{ Id = 4, ParentId = 1, Position = 2, Title = "child 3" }
{ Id = 5, ParentId = 4, Position = 0, Title = "grandchild 1" }
我正在寻找一个通用的 C# 算法,它将由此创建一个对象层次结构。 如果您愿意的话,可以使用“层次化”功能,将这些数据转换为对象层次结构。
有任何想法吗?
编辑 我已经将文件解析为 .NET 对象:
class Node
{
public int Id { get; }
public int ParentId { get; }
public int Position { get; }
public string Title { get; }
}
现在我需要将对象实际排列到对象图中。
I've got a text file that looks like this:
{ Id = 1, ParentId = 0, Position = 0, Title = "root" }
{ Id = 2, ParentId = 1, Position = 0, Title = "child 1" }
{ Id = 3, ParentId = 1, Position = 1, Title = "child 2" }
{ Id = 4, ParentId = 1, Position = 2, Title = "child 3" }
{ Id = 5, ParentId = 4, Position = 0, Title = "grandchild 1" }
I'm looking for a generic C# algorithm that will create an object hierarchy from this. A "Hierarchize" function, if you will, that turns this data into an object hierarchy.
Any ideas?
edit I've already parsed the file into .NET objects:
class Node
{
public int Id { get; }
public int ParentId { get; }
public int Position { get; }
public string Title { get; }
}
Now I need to actually arrange the objects into an object graph.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我假设您的示例错误地为对象 #5 提供了错误的父 ID。 这应该覆盖它。 注意事项:假设“最顶层”节点的父 ID 始终为零。 忽略最终不是最顶层节点的后代的任何节点。 如果出现重复的 ID,行为将会很奇怪。
I assume that your example incorrectly gives the wrong parent ID to object #5. This should cover it. Caveats: Assumes the "topmost" node always has a parent ID of zero. Ignores any nodes that aren't eventually descended from the topmost node. Behavior will be odd if presented with duplicate IDs.
结果:
result:
你确定最后一行的 ParentID 是 1 吗? 标题说的是孙子,但如果我读得正确的话,它将是“根”的孩子。
Are you certain the last line's ParentID is 1? The title says grandchild, but it would be a child of "root" if i'm reading things correctly.
这是@baran 要求的示例:
Here is the example that @baran asked for:
非常感谢 Jon 和 mquander - 你们给了我足够的信息来帮助我以正确、通用的方式解决这个问题。 这是我的解决方案,一个将对象转换为层次结构形式的通用扩展方法:
利用这个小节点类:
它足够通用,可以解决各种问题,包括我的文本文件问题。 漂亮!
****更新****:使用方法如下:
Many thanks to Jon and to mquander - you guys gave me enough information to help me solve this in a proper, generic way. Here's my solution, a single generic extension method that converts objects into hierarchy form:
Utilizes this small node class:
It's generic enough to work for a variety of problems, including my text file issue. Nifty!
****UPDATE****: Here's how you'd use it:
嗯...我不太明白这是如何工作的。 2 和 5 怎么可能都有parent=1,position=0? 5 应该有父母 2、3 或 4 吗?
好的,这个新版本会遍历所有节点三次:
它没有很好地封装,很好的错误检查等 - 但它可以工作。
示例文本文件:
输出:
Hmm... I don't quite see how that works. How can 2 and 5 both have parent=1, position=0? Should 5 have parent 2, 3 or 4?
Okay, this new version goes through the all the nodes three times:
It's not well-encapsulated, nicely error checking etc - but it works.
Sample text file:
Output:
解析完文件后,您可以按照此 博客。
Once you have the file parsed in you can follow this blog on how to assemble the objects into a hierarchy using LINQ.