我有一个代表一棵树的类:
public class Tree
{
public String id { get; set; }
public String text { get; set; }
public List<Tree> item { get; set; }
public string im0 { get; set; }
public string im1 { get; set; }
public string im2 { get; set; }
public String parentId { get; set; }
public Tree()
{
id = "0";
text = "";
item = new List<Tree>();
}
}
树看起来像这样:
tree {Tree} Tree
id "0" string
im0 null string
im1 null string
im2 null string
item Count = 1 System.Collections.Generic.List<Tree>
[0] {Tree} Tree
id "F_1" string
im0 "fC.gif" string
im1 "fO.gif" string
im2 "fC.gif" string
item Count = 12 System.Collections.Generic.List<Tree>
parentId "0" string
text "ok" string
parentId null string
text "" string
如何删除 id = someId 的节点?
例如,如何删除 id = "F_123" 的节点?它的所有孩子也应该被删除。
我有一个在树中搜索给定 id 的方法。我尝试使用该方法,然后将节点设置为 null,但它不起作用。
这是我到目前为止得到的:
//This is the whole tree:
Tree tree = serializer.Deserialize<Tree>(someString);
//this is the tree whose root is the parent of the node I want to delete:
List<Tree> parentTree = Tree.Search("F_123", tree).First().item;
//This is the node I want to delete:
var child = parentTree.First(p => p.id == id);
如何从树中删除孩子?