如何过滤树形结构
本文关键字:结构 过滤 何过滤 | 更新日期: 2023-09-27 18:02:41
我有一个对象树MyNode
:
public class MyNode
{
public int Type { get; set; }
public List<MyNode> Children { get; set; }
}
MyNode myRootNode;
// initializing a tree structure
所以我需要删除除
之外的所有节点具有
Type
属性的节点等于int myType
在其子节点中包含具有
Type
属性的任何级别节点等于int myType
的节点
我的方法:
bool Filter(MyNode node, MyNode parentNode, int type)
{
bool isFound = false;
if (node.Type == type)
isFound = true; // There's type
foreach (MyNode child in node.Children)
{
if (FilterTree(child, node, type))
isFound = true; // There is child node who has this type
}
// If there aren't this type neither any of its children has it
if (!isFound)
{
parentNode.Children.Remove(node);
}
return isFound;
}
我有一个例外:Collection was modified; enumeration operation may not execute.
我认为这是因为我在列表中删除元素。有没有一种正确的方法?或者我做错了什么?
假设始终保留根节点,则可以删除方法中的子节点,而不是节点本身。
bool Filter(MyNode node,int type)
{
//remove children
foreach(MyNode child in node.Children.Where(c=>!Filter(c, type)).ToArray())
node.Children.Remove(child);
//return if should be retained
return node.Type==type || node.Children.Count>0;
}
Linq来救你:
public static void RemoveNodesRecursive(this MyNode node, Predicate<MyNode> predicate)
{
node.Children.RemoveAll(predicate);
foreach (var n in node.Children)
{
RemoveNodes(n);
}
}
然后从根节点开始:
myRootNode.RemoveNodesRecursive(n => n.node.Type == myType)