using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Hncore.Infrastructure.Tree { public class DataNode { public DataNode(TData data) { this.Data = data; } public DataNode Parent { get; set; } public TData Data { get; set; } public List> Children { get; set; } = new List>(); public bool IsLeaf { get { return this.Children.Count()==0; } } public void Traverse(Action> act) { act(this); this.Children.ForEach(item => { item.Traverse(act); }); } public string GetFullPath(Func func, string separator = ".") { var parent = this.Parent; var names = new List { func(this.Data) }; while (parent != null) { names.Add(func(parent.Data)); parent = parent.Parent; } names.Reverse(); return string.Join(separator, names).TrimStart(separator.ToArray()); } public DataNode SortAsc(Func exp) { if (this.Children.Count > 0) { Func, Tkey> iierExp = m => exp(m.Data); this.Children.OrderBy(iierExp); this.Children.ForEach(item => { item.SortAsc(exp); }); } return this; } public DataNode SortDesc(Func exp) { if (this.Children.Count > 0) { Func, Tkey> iierExp = m => exp(m.Data); this.Children.OrderByDescending(iierExp); this.Children.ForEach(item => { item.SortDesc(exp); }); } return this; } } }