using System; using System.Collections.Generic; using System.Linq; namespace TreeNodes { public interface INodeInfomation { string DisplayName { get; set; } string Path { get; set; } } class HierarchyObject<T> where T : INodeInfomation { public HierarchyObject() { Id = Guid.NewGuid(); Children = new List<HierarchyObject<T>>(); Parent = null; } public void AddChild(HierarchyObject<T> item) { if (ReferenceEquals(this, item)) return; item.Parent = this; Children.Add(item); } public List<HierarchyObject<T>> Children { get; set; } public HierarchyObject<T> Parent { get; set; } private string _path; public string Path { get { if (Parent == null) return Data.DisplayName; _path = Parent.Path + "/" + Data.DisplayName; return _path; } } public Guid Id { get; set; } public T Data { get; set; } public bool IsLeaf { get { return !Children.Any(); } } } internal static class HierarchyObjectExtension {
public static int TotalCount<T>(this HierarchyObject<T> obj) where T : INodeInfomation
{
int count = 0;
obj.GetTotalCount(ref count);
return count;
}
public static void GetTotalCount<T>(this HierarchyObject<T> obj, ref int num) where T : INodeInfomation
{
num++;
foreach (var child in obj.Children)
{
child.GetTotalCount(ref num);
}
}
public static List<T> ToList<T>(this HierarchyObject<T> obj) where T : INodeInfomation
{
var result = new List<T>();
obj.GetAllNodes(result);
return result;
}
public static void GetAllNodes<T>(this HierarchyObject<T> obj, List<T> outResult) where T : INodeInfomation
{
obj.Data.Path = obj.Path;
outResult.Add(obj.Data);
foreach (var child in obj.Children)
{
child.GetAllNodes(outResult);
}
}
}
namespace TreeNodes { public class MyClass : INodeInfomation { public string DisplayName { get; set; } public string Path { get; set; } } class Program { static void Main(string[] args) { HierarchyObject<MyClass> Province = new HierarchyObject<MyClass>(); Province.Data = new MyClass() { DisplayName = "省份" }; HierarchyObject<MyClass> Province1 = new HierarchyObject<MyClass>(); Province1.Data = new MyClass() { DisplayName = "江西" }; HierarchyObject<MyClass> Province2 = new HierarchyObject<MyClass>(); Province2.Data = new MyClass() { DisplayName = "浙江" }; HierarchyObject<MyClass> City1 = new HierarchyObject<MyClass>(); City1.Data = new MyClass() { DisplayName = "新余" }; HierarchyObject<MyClass> City2 = new HierarchyObject<MyClass>(); City2.Data = new MyClass() { DisplayName = "宜春" }; Province1.AddChild(City1); Province1.AddChild(City2); HierarchyObject<MyClass> City3 = new HierarchyObject<MyClass>(); City3.Data = new MyClass() { DisplayName = "杭州" }; HierarchyObject<MyClass> City4 = new HierarchyObject<MyClass>(); City4.Data = new MyClass() { DisplayName = "义乌" }; Province2.AddChild(City3); Province2.AddChild(City4); Province.AddChild(Province1); Province.AddChild(Province2); // Console.WriteLine(City4.Path); var list = Province.ToList(); foreach (var myClass in list) { Console.WriteLine(myClass.Path); } Console.Read(); } } }
class Program { static void Main() { List<MyClass> result = new List<MyClass>(); result.Add(new MyClass() { DisplayName = "江西", Key = 1, Owner = 0 }); result.Add(new MyClass() { DisplayName = "浙江", Key = 2, Owner = 0 }); result.Add(new MyClass() { DisplayName = "新余", Key = 3, Owner = 1 }); result.Add(new MyClass() { DisplayName = "宜春", Key = 4, Owner = 1 }); result.Add(new MyClass() { DisplayName = "杭州", Key = 5, Owner = 2 }); result.Add(new MyClass() { DisplayName = "义乌", Key = 6, Owner = 2 }); var root = new HierarchyObject<MyClass>() { Data = new MyClass() { DisplayName = "Root", Key = 0 } }; BuildTree(null, root, result); } public static void BuildTree(HierarchyObject<MyClass> parentNode, HierarchyObject<MyClass> root, List<MyClass> list) { int parentId = parentNode == null ? 0 : parentNode.Data.Key; var filterdList = list.FindAll(item => item.Owner == parentId); foreach (var item in filterdList) { var node = new HierarchyObject<MyClass> {Data = item}; if (parentNode == null) { root.Children.Add(node); BuildTree(node, root, list); } else { parentNode.Children.Add(node); BuildTree(node, root, list); } } } }