排序谓词使节点按深度优先搜索顺序排序
我有一个节点列表,其中每个节点都属于一棵或多棵树。 (它们不一定具有共同的祖先)
我想按照在进行深度优先搜索时找到它们的相同顺序对节点进行排序。
假设我有一个将树根排序在一起的谓词,以及另一个将共同父级的子元素排序在一起的谓词。每个节点都有一个父访问器和一个子枚举器。出于性能原因(如果可能),我想避免使用 Children 枚举。
传递给排序函数的谓词的伪代码是什么(如果节点 1 小于节点 2,谓词将返回布尔值)。
I have a list of nodes, where each of the nodes belong to one or multiple trees. (they do not necessarily share a common ancestor)
I want to sort the nodes in the same order I would find them when doing a Depth First Search.
Let say I have a predicate for sorting tree roots together, and another predicate to sort children of a common parent together. Each node have a Parent accessor, and a children enumerator. I want to avoid using the Children enumeration for performance reasons (if possible).
What can be the pseudo code for a predicate to pass to a sort function (the predicate would return a boolean value if node 1 is less than node 2).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为,您需要在节点中存储有用的信息,因此谓词可以从一对未连接的节点中选择前一个节点。
这是我的尝试(可能不是很聪明,甚至不是有效):
I think, you need to store helpful information in nodes, so predicate can choose preceding node from pair of unconnected nodes.
Here's my (may be not very clever or, even, working) attempt:
我找到了一个简单的解决方案:
对于节点,有一个函数可以返回从根开始的路径。例如,在文件系统中,文件的路径可以是:c:\directory\file.txt,其中“C:”、“directory”和“file.txt”是父节点。
谓词只需将路径进行比较,就像简单的字符串比较一样。路径不需要是字符串,路径比较函数需要从根开始逐个比较路径元素,一旦路径元素不同就返回。
结果排序与深度优先搜索相同。
I found an easy working solution:
For a node, have a function that returns the path from the root. For example, in a file system, the path for a file could be : c:\directory\file.txt, where "C:", "directory" and "file.txt" are the parent nodes.
The predicate simply need to compare the paths together, as a simple string comparison would do. The path does not need to be a string, the path comparison function need to compare path elements one by one starting at the root, and return as soon a path element is different.
The resulting sort is the same as a Depth First Search.