TreeCollections c# 树结构 多余二叉树的树结构

可变树

创建

所有可变树节点共享MutableEntityTreeNode<TNode, TId, TItem>基类。为了暂时保持进展,让我们使用内置的具体类创建我们的第一个可变树MutableEntityTreeNode<TId, TItem>

该树的构建和初始填充将与创建您的第一个实体树部分中显示的方式相同(我们的源数据树来自Up & Running部分)。

var rootItem = new Category(dataRoot.Id, dataRoot.Name);

var root = new MutableEntityTreeNode<int, Category>(c => c.CategoryId, rootItem);

root.Build(dataRoot, dataNode => new Category(dataNode.Id, dataNode.Name);

以下是可用于修改任何由MutableEntityTreeNode<TNode, TId, TItem>元素组成的树的所有主要操作。public显示了与这些操作对应的此节点类型的主要成员的签名:

####添加新节点 > ####

public virtual TNode AddChild(TItem item, int? insertIndex = null)
public virtual TNode AddAtAdjacentPosition(TItem item, Adjacency adjacency)

####移动节点 > ####

public virtual void MoveToParent(TId parentId, int? insertIndex = null)
public virtual void MoveToAdjacentPosition(TId targetId, Adjacency adjacency)
public virtual void IncrementSiblingPosition()
public virtual void DecrementSiblingPosition()

####分离节点 > ####

public virtual void Detach()
public virtual void DetachWhere(Func<TNode, bool> satisfiesCondition)
public virtual void DetachChildren()

####附加节点 > ####

public virtual void AttachChild(TNode node, int? insertionIndex = null)
public virtual void AttachAtAdjacentPosition(TNode node, Adjacency adjacency)

####重新排序子级 > ####

public virtual void OrderChildrenAscending<TOrderKey>(Func<TItem, TOrderKey> selectKey)
public virtual void OrderChildrenDescending<TOrderKey>(Func<TItem, TOrderKey> selectKey)
public virtual void OrderChildren(IComparer<TItem> itemComparer)
public virtual void OrderChildren(params TId[] preferredOrder)

 

 

基本查询

继续我们的第一个实体树(使用具体类型的节点构建ReadonlyEntityTreeNode<int, Category>),让我们看看我们可以执行的最常见的查询。

下面的所有示例都假设变量root包含我们的根节点。

按 ID 查找单个节点

如果我们想获得一个具有已知实体 id 的单个节点(在本例中,aCategoryId为 7),请使用如下索引器运算符:

var thingsNode = root[7];    

在这里,我们从根开始向下搜索实体 id 为 7 的第一个(表面上也是唯一的)节点。

如果没有节点满足这个条件,null则返回a;

ItemId属性_

在任何EntityTreeNode对象上,都可以通过Item属性访问核心实体对象。

例如:

var mathName = root[19].Item.Name;     // "Math";
var mathId = root[19].Item.CategoryId; // 19

EntityTreeNode类具有让上面的第二个示例更简单的快捷方式属性Id

var mathId = root[19].Id;   // 19

这之所以成为可能,是因为树知道(感谢我们的根节点构造函数)Category.CategoryId代表我们的核心实体 ID。

一般 LINQ 操作

正如前面实体树概述中所展示的,我们可以直接在树中的单个节点上调用任何 LINQ 操作以返回可枚举的节点序列。默认情况下,枚举器将从给定节点开始,并以前序(深度优先)遍历顺序继续后代节点。

例如,要在前序遍历中选择所有实体 id:

var allIds = root.Select(node => node.Id);   

备用参数化枚举器将在后面的部分中介绍。

分层属性和内置查询

以下代码片段说明了各种属性和一些有用的预定义查询:

var current = root.First(n => n.Item.Name == "Places");

var parent = current.Parent;
var children = current.Children;

var descendants = current.SelectDescendants();
var leaves = current.SelectLeaves();

var siblings = current.SelectSiblings();
var siblingsBefore = current.SelectSiblingsBefore();
var siblingsAfter = current.SelectSiblingsAfter();

var pathUpward = current.SelectPathUpward();       // all nodes from self to root
var pathDownward = current.SelectPathDownward();   // all nodes from root to self

var ancestorsUpward = current.SelectAncestorsUpward();
var ancestorsDownward = current.SelectAncestorsDownward();

bool isRoot = current.IsRoot;
bool isLeaf = current.IsLeaf;
int level = current.Level;  // note: root is level 0
int depth = root.Depth;    // i.e. max level of the tree

bool isDescendant = current.IsDescendantOf(root[22]);
bool isAncestor = current.IsAncestorOf(root[15]);
bool isSibling = current.IsSiblingOf(root[12]);

 

【转】https://github.com/davidwest/TreeCollections/wiki/Creating-Your-First-Entity-Tree

posted @ 2022-04-26 23:02  KissingTheFire  阅读(106)  评论(0)    收藏  举报