Moli
2022-04-15 22477f0eca904ec628ab59cd749f959af35c1439
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using System.Collections.Generic;
using System.Text;
 
namespace PublicCode.FrameModel
{
    public class NodeModel
    {
        /// <summary>
        /// 是否为根节点
        /// </summary>
        public bool IsRoot { get; set; }
        /// <summary>
        /// 节点名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 节点内容
        /// </summary>
        public object value { get; set; }
        /// <summary>
        /// 父节点名称
        /// </summary>
        public string parentNodeName { get; set; }
#if false //不可用
        /// <summary>
        /// 父节点
        /// </summary>
        public NodeModel parentNode { get {
                return FindNode(parentNodeName);
            }
        }
 
        /// <summary>
        /// 子节点
        /// </summary>
        public List<NodeModel> childNode { get; set; }
 
        /// <summary>
        /// 递归查找节点
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <returns>返回查找到的节点</returns>
        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;
        }
        /// <summary>
        /// 递归查找节点
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <param name="node">输出查找到的节点,不存在则为null</param>
        /// <returns>若节点存在,返回true,不存在返回false</returns>
        public bool TryFindNode(string nodeName, out NodeModel node)
        {
            node = FindNode(nodeName);
            return node == null ? false : true;
        }
#endif
    }
 
    public class NodeModel<T>
    {
        public NodeModel()
        {
            id = Guid.NewGuid().ToString("N");
        }
        /// <summary>
        /// 唯一标识
        /// </summary>
        protected string id { get; set; }
        /// <summary>
        /// 节点唯一id(只读)
        /// </summary>
        public string nodeId { get { return id; } }
 
        /// <summary>
        /// 是否为根节点
        /// </summary>
        public bool IsRoot { get; set; } = false;
        /// <summary>
        /// 节点名称
        /// </summary>
        public string name { get; set; }
        /// <summary>
        /// 节点内容
        /// </summary>
        public T value { get; set; }
        /// <summary>
        /// 父节点名称
        /// </summary>
        public string parentNodeId { get; set; }
        /// <summary>
        /// 节点名称
        /// </summary>
        public string nodeName { get; set; }
 
        /// <summary>
        /// 子节点
        /// </summary>
        public List<NodeModel<T>> childNode { get; set; }
 
#if false  //暂不可用
        /// <summary>
        /// 父节点
        /// </summary>
        public NodeModel<T> parentNode
        {
            get
            {
            }
        }
 
        /// <summary>
        /// 递归查找子节点
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <returns>返回查找到的节点</returns>
        public NodeModel<T> FindNode(string nodeName)
        {
            NodeModel<T> 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;
        }
 
        /// <summary>
        /// 获取最长路径的第一个节点
        /// </summary>
        /// <returns></returns>
        public NodeModel<T> FindLastNodeFirst() {
            var node = this;
            var maxIndex = 0;
            FindLastNodeFirst(this, ref node, 0,ref maxIndex);
            return node;
        }
 
        /// <summary>
        /// 查找最长的节点
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private void FindLastNodeFirst(NodeModel<T> node,ref NodeModel<T> 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);
                }
            }
        }
 
        /// <summary>
        /// 递归查找节点
        /// </summary>
        /// <param name="nodeName">节点名称</param>
        /// <param name="node">输出查找到的节点,不存在则为null</param>
        /// <returns>若节点存在,返回true,不存在返回false</returns>
        public bool TryFindNode(string nodeName, out NodeModel<T> node)
        {
            node = FindNode(nodeName);
            return node == null ? false : true;
        }
#endif
    }
}