使用 LINQ 从树数据结构获取级别的最佳方法是什么?
我有一个集合演示了树形数据结构,它的节点是:
Node:
Id
Name
ParentID
现在,我想获取该集合中每个节点的 level
,我尝试使用以下代码,但我想知道这是否是最好的方法来实现这一点。
Func<int, int> GetLevel = (nodeID) =>
{
int _level = 0;
var _node = source.First(p => p.Id == nodeID);
// while hasn't reached the root yet
while (_node .ParentID.HasValue)
{
_level++;
_node = source.First(p => p.Id == _node.ParentID);
}
return _level;
};
// source is a collection of Node.
var query = from node in source
select new
{
Id = node.Id,
Name = node.Name,
ParentID = node.ParentID,
Level = GetLevel(node.Id)
};
我认为 GetLevel 函数的开销能够减少。或者也许有更好的方法可以不用这个功能直接获取!
任何想法!
I have a collection demonstrates a tree data structure, it's node is:
Node:
Id
Name
ParentID
Now, I want to get the level
for each node in this collection, I tried with the following code but I wonder if it's the best way to implement that.
Func<int, int> GetLevel = (nodeID) =>
{
int _level = 0;
var _node = source.First(p => p.Id == nodeID);
// while hasn't reached the root yet
while (_node .ParentID.HasValue)
{
_level++;
_node = source.First(p => p.Id == _node.ParentID);
}
return _level;
};
// source is a collection of Node.
var query = from node in source
select new
{
Id = node.Id,
Name = node.Name,
ParentID = node.ParentID,
Level = GetLevel(node.Id)
};
I think that the overhead in GetLevel
function is able to decrease. or maybe there is a better way to get it directly without this function!
Any idea!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
通过这个 Node 类,您可以轻松做到这一点。
创建一棵树并显示每个节点的级别:
With this Node class you can do this easily.
Create a tree and show the level of each node:
您可以使用广度优先遍历在
n
步内自上而下地完成此操作。据我所知,你的方法是n*log(n)
?使用带有
Level
字段的节点类快速破解 LinqPad输出:
但这一切都取决于 Linq 部分的复杂性(.Where)
You could do it top-down in
n
steps with Breadth First Traversal. Your approach isn*log(n)
as far as I can see?A quick hack in LinqPad with a node class with
Level
fieldOutputs:
But this all depends on the complexity of the Linq parts (.Where)
既然您说您需要获取此集合中每个节点的级别,您可能会急切生成从节点到级别的映射。
通过适当的广度优先遍历,这可以在 O(n) 时间内完成。
(未经测试):
Since you say you need to get the level for each node in this collection, you might as produce a map from node to level eagerly.
This can be done in O(n) time with a proper breadth-first traversal.
(Untested):
下面是您正在做的事情的更紧凑版本,请注意,我在测试中使用了简化的数据结构,并且 Flatten 返回 IEnumerable <树>从变量树向下的树中的每个节点。如果您有权访问该源代码,我会将递归深度函数作为树类的一部分。如果您经常这样做或您的树很大(或两者兼而有之),我特别喜欢在字典或树结构本身中缓存深度的解决方案。如果你不经常这样做,这会很好用。我用它从 GUI 中浏览相对较小的树结构,没有人认为操作很慢。获取每个节点深度的平均复杂度为 O(N log N)。如果您想查看所有代码,我可以明天将其放入。
A more compact version of what you're doing is below, note I used a simplified data structure for my test and Flatten returns an IEnumerable < Tree > of every node in the tree from the variable tree on down. I would make the recursive depth function part of the tree class if you have access to that source. If you are doing this often or your trees are huge (or both) I'm particular to the solution of caching the depth in a dictionary or the tree structure itself. If you don't do it often this will work fine. I use it for going through relatively small tree structures from a GUI and no one has ever thought the operation was slow. Complexity is O(N log N) average case for getting the depth of every node. If you would like to see all of the code I can put it in tomorrow.
现在你正在多次处理很多事情。
我会递归地构建关卡。这样你就没有任何处理开销。
另外,如果您经常运行此功能,我会将级别作为变量放入节点类中,该变量在添加节点时自动计算。
源代码如下。
Right now you are processing a lot of things multiple times.
I would recursively build the levels. This way you don't have any processing overhead.
Also, if you run this functionality a lot, I would put in the level in the node class as a variable which is computed automatically when a node is added.
Source code follows.
尝试像这样使用
.ToDictionary
:这相当有效,因为您的最终查询将递归通过所有节点。因此,建立字典的费用很便宜。
根据节点的深度,您可能会发现在任何情况下构建字典都比进行多级强力搜索更快。
Try using
.ToDictionary
like this:This is fairly efficient since your final query is going to recurse thru all of the nodes. The expense to build a dictionary is therefore cheap.
Depending how deep your nodes go you might find that building a dictionary is quicker than doing multi-level brute force searches in any case.