using System; using System.Collections.Generic; using System.Text; namespace PublicCode.FrameModel { public class NodeModel { /// /// 是否为根节点 /// public bool IsRoot { get; set; } /// /// 节点名称 /// public string name { get; set; } /// /// 节点内容 /// public object value { get; set; } /// /// 父节点名称 /// public string parentNodeName { get; set; } #if false //不可用 /// /// 父节点 /// public NodeModel parentNode { get { return FindNode(parentNodeName); } } /// /// 子节点 /// public List childNode { get; set; } /// /// 递归查找节点 /// /// 节点名称 /// 返回查找到的节点 public NodeModel FindNode(string nodeName) { NodeModel res = null; foreach (var a in childNode) { if (a.name == nodeName) { res = a; } } if (res == null && childNode != null && childNode.Count > 0) { res = FindNode(nodeName); } return res; } /// /// 递归查找节点 /// /// 节点名称 /// 输出查找到的节点,不存在则为null /// 若节点存在,返回true,不存在返回false public bool TryFindNode(string nodeName, out NodeModel node) { node = FindNode(nodeName); return node == null ? false : true; } #endif } public class NodeModel { public NodeModel() { id = Guid.NewGuid().ToString("N"); } /// /// 唯一标识 /// protected string id { get; set; } /// /// 节点唯一id(只读) /// public string nodeId { get { return id; } } /// /// 是否为根节点 /// public bool IsRoot { get; set; } = false; /// /// 节点名称 /// public string name { get; set; } /// /// 节点内容 /// public T value { get; set; } /// /// 父节点名称 /// public string parentNodeId { get; set; } /// /// 节点名称 /// public string nodeName { get; set; } /// /// 子节点 /// public List> childNode { get; set; } #if false //暂不可用 /// /// 父节点 /// public NodeModel parentNode { get { } } /// /// 递归查找子节点 /// /// 节点名称 /// 返回查找到的节点 public NodeModel FindNode(string nodeName) { NodeModel res = null; foreach (var a in childNode) { if (a.name == nodeName) { res = a; } } if (res == null && childNode != null && childNode.Count > 0) { res = FindNode(nodeName); } return res; } /// /// 获取最长路径的第一个节点 /// /// public NodeModel FindLastNodeFirst() { var node = this; var maxIndex = 0; FindLastNodeFirst(this, ref node, 0,ref maxIndex); return node; } /// /// 查找最长的节点 /// /// /// private void FindLastNodeFirst(NodeModel node,ref NodeModel lastNode, int index,ref int maxIndex) { if (maxIndex < index && lastNode != node) { //如果最长路径超出了原最长的路径,更新为最长路径的节点 lastNode = node; maxIndex = index; } if (this.childNode != null && this.childNode.Count > 0) { //如果还存在下级节点,递归遍历子节点 foreach (var a in this.childNode) { index++; FindLastNodeFirst(a, ref lastNode, index, ref maxIndex); } } } /// /// 递归查找节点 /// /// 节点名称 /// 输出查找到的节点,不存在则为null /// 若节点存在,返回true,不存在返回false public bool TryFindNode(string nodeName, out NodeModel node) { node = FindNode(nodeName); return node == null ? false : true; } #endif } }