34
223
17
后起之秀
使用道具 举报
32
651
8
中流砥柱
72
2726
9
社区元老
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace NFox.Collections { /// /// 深度优先遍历模式 /// public enum DepthSearchModeType { /// /// 前序遍历 /// Preorder, /// /// 中序遍历 /// //Inorder, /// /// 后序遍历 /// Postorder } public class Tree : List> { public T Value { get; set; } public Tree Parent { private set; get; } public bool IsRoot { get { return Parent == null; } } public bool IsLeaf { get { return Count == 0; } } public Tree Root { get { var node = this; while (node.Parent != null) { node = node.Parent; } return node; } } public Tree(){ } public Tree(T value) { Value = value; } public bool IsAncestorOf(Tree other) { Tree node = other; while (node.Parent != null) { node = node.Parent; if (this == node) return true; } return false; } public bool IsChildOf(Tree other) { return other.IsAncestorOf(this); } public List> Path { get { List> lst = new List> { this }; Tree node = this; while (node.Parent != null) { node = node.Parent; lst.Insert(0, node); } return lst; } } public int Depth { get { return Path.Count - 1; } } public new void Insert(int index, Tree node) { node.Parent = this; base.Insert(index, node); } public void InsertBefore(Tree node, T value) { Insert(IndexOf(node), new Tree(value)); } public void InsertAfter(Tree node, T value) { Insert(IndexOf(node) + 1, new Tree(value)); } public new void Add(Tree node) { node.Parent = this; base.Add(node); } public void Add(T value) { Add(new Tree(value)); } public new void AddRange(IEnumerable> collection) { foreach (var tree in collection) tree.Parent = this; base.AddRange(collection); } public void AddRange(IEnumerable collection)