在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
delegate void TreeVisitor<T>(T nodeData); class NTree<T> { private T data; private LinkedList<NTree<T>> children; public NTree(T data) { this.data = data; children = new LinkedList<NTree<T>>(); } public void AddChild(T data) { children.AddFirst(new NTree<T>(data)); } public NTree<T> GetChild(int i) { foreach (NTree<T> n in children) if (--i == 0) return n; return null; } public void Traverse(NTree<T> node, TreeVisitor<T> visitor) { visitor(node.data); foreach (NTree<T> kid in node.children) Traverse(kid, visitor); } }
namespace Overby.Collections { public class TreeNode<T> { private readonly T _value; private readonly List<TreeNode<T>> _children = new List<TreeNode<T>>(); public TreeNode(T value) { _value = value; } public TreeNode<T> this[int i] { get { return _children[i]; } } public TreeNode<T> Parent { get; private set; } public T Value { get { return _value; } } public ReadOnlyCollection<TreeNode<T>> Children { get { return _children.AsReadOnly(); } } public TreeNode<T> AddChild(T value) { var node = new TreeNode<T>(value) {Parent = this}; _children.Add(node); return node; } public TreeNode<T>[] AddChildren(params T[] values) { return values.Select(AddChild).ToArray(); } public bool RemoveChild(TreeNode<T> node) { return _children.Remove(node); } public void Traverse(Action<T> action) { action(Value); foreach (var child in _children) child.Traverse(action); } public IEnumerable<T> Flatten() { return new[] {Value}.Concat(_children.SelectMany(x => x.Flatten())); } } }
|
请发表评论