-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTreeNode.cs
More file actions
418 lines (379 loc) · 15.8 KB
/
Copy pathTreeNode.cs
File metadata and controls
418 lines (379 loc) · 15.8 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Collections.Extensions;
using RapidField.SolidInstruments.Core.ArgumentValidation;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace RapidField.SolidInstruments.Collections
{
/// <summary>
/// Represents a node in a tree structure.
/// </summary>
/// <remarks>
/// <see cref="TreeNode{T, TChildNode}" /> is the default implementation of <see cref="ITreeNode{T, TChildNode}" />.
/// </remarks>
/// <typeparam name="T">
/// The value type of the node.
/// </typeparam>
/// <typeparam name="TChildNode">
/// The type of the node's children.
/// </typeparam>
public class TreeNode<T, TChildNode> : TreeNode<T>, ITreeNode<T, TChildNode>
where TChildNode : TreeNode<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="TreeNode{T, TChildNode}" /> class.
/// </summary>
public TreeNode()
: base()
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="TreeNode{T, TChildNode}" /> class.
/// </summary>
/// <param name="value">
/// The value of the node.
/// </param>
public TreeNode(T value)
: base(value)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="TreeNode{T, TChildNode}" /> class.
/// </summary>
/// <param name="value">
/// The value of the node.
/// </param>
/// <param name="capacity">
/// The maximum number of children permitted by the node, or -1 if no limit is imposed. The default value is -1.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="capacity" /> is less than -1.
/// </exception>
public TreeNode(T value, Int32 capacity)
: base(capacity)
{
Value = value;
}
/// <summary>
/// Initializes a new instance of the <see cref="TreeNode{T}" /> class.
/// </summary>
/// <param name="value">
/// The value of the node.
/// </param>
/// <param name="children">
/// The child nodes of the new node, or <see langword="null" /> if the new node is a terminal node. The default value is
/// <see langword="null" />.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="children" /> contains a null, duplicate or invalid node.
/// </exception>
public TreeNode(T value, IEnumerable<TChildNode> children)
: base(value, children)
{
return;
}
/// <summary>
/// Adds the specified node to <see cref="TreeNode{T}.Children" /> and sets its <see cref="TreeNode{T}.Parent" /> to the
/// current node.
/// </summary>
/// <param name="childNode">
/// The child node to add.
/// </param>
/// <returns>
/// <see langword="true" /> if the specified node was added to <see cref="TreeNode{T}.Children" />, otherwise
/// <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="childNode" /> is <see langword="null" />.
/// </exception>
public Boolean AddChild(TChildNode childNode) => AddChild(childNode, true);
/// <summary>
/// Destroys all references to and from associated tree nodes and sets <see cref="TreeNode{T}.Value" /> equal to the default
/// value.
/// </summary>
/// <exception cref="InvalidOperationException">
/// The state of the node or the associated tree was changed during the destroy operation.
/// </exception>
public void Destroy()
{
if (IsRoot == false)
{
((TreeNode<T>)Parent).RemoveChild(this);
}
var children = Children.ToArray();
var childrenLength = children.Length;
for (var i = 0; i < childrenLength; i++)
{
try
{
if (RemoveChild(children.ElementAt(i) as TreeNode<T>))
{
continue;
}
}
catch (ArgumentNullException exception)
{
throw new InvalidOperationException("The tree node is in an invalid state.", exception);
}
throw new InvalidOperationException("The tree structure has become corrupt.");
}
Value = default;
}
}
/// <summary>
/// Represents a node in a tree structure.
/// </summary>
/// <remarks>
/// <see cref="TreeNode{T}" /> is the default implementation of <see cref="ITreeNode{T}" />.
/// </remarks>
/// <typeparam name="T">
/// The value type of the node.
/// </typeparam>
public class TreeNode<T> : ITreeNode<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="TreeNode{T}" /> class.
/// </summary>
public TreeNode()
: this(default(T))
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="TreeNode{T}" /> class.
/// </summary>
/// <param name="value">
/// The value of the node.
/// </param>
public TreeNode(T value)
: this(value, null)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="TreeNode{T}" /> class.
/// </summary>
/// <param name="value">
/// The value of the node.
/// </param>
/// <param name="children">
/// The child nodes of the new node, or <see langword="null" /> if the new node is a terminal node. The default value is
/// <see langword="null" />.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="children" /> contains a null, duplicate or invalid node.
/// </exception>
public TreeNode(T value, IEnumerable<TreeNode<T>> children)
: this(children is null ? UnlimitedCapacityValue : children.Count())
{
Value = value;
if (children is null)
{
return;
}
foreach (var node in children)
{
if (node is null)
{
throw new ArgumentException("The specified collection contains a null node.", nameof(children));
}
else if (AddChild(node, true) == false)
{
throw new ArgumentException("The specified collection contains a duplicate or invalid node.", nameof(children));
}
}
}
/// <summary>
/// Initializes a new instance of the <see cref="TreeNode{T}" /> class.
/// </summary>
/// <param name="capacity">
/// The maximum number of children permitted by the node, or -1 if no limit is imposed. The default value is -1.
/// </param>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="capacity" /> is less than -1.
/// </exception>
protected TreeNode(Int32 capacity)
{
Capacity = capacity.RejectIf().IsLessThan(UnlimitedCapacityValue, nameof(capacity));
ChildrenReference = Capacity == UnlimitedCapacityValue ? new List<TreeNode<T>>() : new List<TreeNode<T>>(Capacity);
}
/// <summary>
/// Returns an enumerator that iterates through the child nodes of the current <see cref="TreeNode{T}" />.
/// </summary>
/// <returns>
/// An enumerator that iterates through the child nodes of the current <see cref="TreeNode{T}" />.
/// </returns>
public IEnumerator<ITreeNode<T>> GetEnumerator() => Children.GetEnumerator();
/// <summary>
/// Returns an enumerator that iterates through the child nodes of the current <see cref="TreeNode{T}" />.
/// </summary>
/// <returns>
/// An enumerator that iterates through the child nodes of the current <see cref="TreeNode{T}" />.
/// </returns>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Removes the specified node from <see cref="Children" />.
/// </summary>
/// <param name="childNode">
/// The child node to remove.
/// </param>
/// <returns>
/// <see langword="true" /> if the specified node was present in <see cref="Children" />, otherwise
/// <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="childNode" /> is <see langword="null" />.
/// </exception>
public Boolean RemoveChild(TreeNode<T> childNode)
{
childNode.RejectIf().IsNull(nameof(childNode));
lock (SyncRoot)
{
childNode.ParentReference = null;
return ChildrenReference.Remove(childNode);
}
}
/// <summary>
/// Converts the value of the current <see cref="TreeNode{T}" /> to its equivalent string representation.
/// </summary>
/// <returns>
/// A string representation of the current <see cref="TreeNode{T}" />.
/// </returns>
public override String ToString() => $"{{ \"{nameof(Depth)}\": {Depth}, \"{nameof(Height)}\": {Height}, \"{nameof(Value)}\": \"{Value}\" }}";
/// <summary>
/// Adds the specified node to <see cref="Children" /> and sets its <see cref="Parent" /> to the current node.
/// </summary>
/// <param name="childNode">
/// The child node to add.
/// </param>
/// <param name="enforceDuplicateProhibition">
/// A value indicating whether or not to reject <paramref name="childNode" /> if the current node already contains a
/// reference to it.
/// </param>
/// <returns>
/// <see langword="true" /> if the specified node was added to <see cref="Children" />, otherwise <see langword="false" />.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="childNode" /> is <see langword="null" />.
/// </exception>
protected Boolean AddChild(TreeNode<T> childNode, Boolean enforceDuplicateProhibition)
{
lock (SyncRoot)
{
if (Capacity > UnlimitedCapacityValue && ChildrenReference.Count() >= Capacity)
{
return false;
}
else if (enforceDuplicateProhibition && ChildrenReference.Contains(childNode.RejectIf().IsNull(nameof(childNode))))
{
return false;
}
childNode.ParentReference = this;
ChildrenReference.Add(childNode);
}
return true;
}
/// <summary>
/// Gets the child nodes of the current <see cref="TreeNode{T}" />, or an empty collection if the current
/// <see cref="TreeNode{T}" /> is a terminal node.
/// </summary>
public IEnumerable<ITreeNode<T>> Children
{
get
{
lock (SyncRoot)
{
foreach (var child in ChildrenReference)
{
yield return child;
}
}
}
}
/// <summary>
/// Gets the number of connections from the tree's root node to the current <see cref="TreeNode{T}" />.
/// </summary>
public Int32 Depth => this.TraverseUp(DepthFunction);
/// <summary>
/// Gets the number of connections on the longest path between the current <see cref="TreeNode{T}" /> and a leaf node.
/// </summary>
public Int32 Height => this.TraverseDown(HeightFunction);
/// <summary>
/// Gets a value indicating whether or not the current <see cref="TreeNode{T}" /> is a leaf node (a node without children).
/// </summary>
/// <remarks>
/// When true, <see cref="Children" /> is an empty collection.
/// </remarks>
public Boolean IsLeaf => (ChildrenReference.Any() == false);
/// <summary>
/// Gets a value indicating whether or not the current <see cref="TreeNode{T}" /> is a root node (a node without a parent).
/// </summary>
/// <remarks>
/// When true, <see cref="Parent" /> is a null reference.
/// </remarks>
public Boolean IsRoot => (ParentReference is null);
/// <summary>
/// Gets the parent node of the current <see cref="TreeNode{T}" />, or <see langword="null" /> if the current
/// <see cref="TreeNode{T}" /> is a root node.
/// </summary>
public ITreeNode<T> Parent => ParentReference;
/// <summary>
/// Gets or sets the value of the current <see cref="TreeNode{T}" />.
/// </summary>
public T Value
{
get;
set;
}
/// <summary>
/// Represents a value of <see cref="Capacity" /> which indicates that no limit is imposed.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal const Int32 UnlimitedCapacityValue = -1;
/// <summary>
/// Represents the maximum count of <see cref="Children" /> permitted for the current <see cref="TreeNode{T}" />, or -1 if
/// no limit is imposed.
/// </summary>
protected readonly Int32 Capacity;
/// <summary>
/// Represents the child nodes of the current <see cref="TreeNode{T}" />, or an empty collection if the current
/// <see cref="TreeNode{T}" /> is a terminal node.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly ICollection<TreeNode<T>> ChildrenReference;
/// <summary>
/// Represents a function used to calculate the node's depth in the tree.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Func<ITreeNode<T>, Int32, Int32> DepthFunction = (node, parentResult) =>
{
return node.IsRoot ? 0 : (parentResult + 1);
};
/// <summary>
/// Represents a function used to calculate the node's height in the tree.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Func<ITreeNode<T>, IEnumerable<Int32>, Int32> HeightFunction = (node, childResults) =>
{
return node.IsLeaf ? 0 : (childResults.Max() + 1);
};
/// <summary>
/// Represents an object that is used to synchronize access to the associated resource(s).
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly Object SyncRoot = new Object();
/// <summary>
/// Represents the parent node of the current <see cref="TreeNode{T}" />, or <see langword="null" /> if the current
/// <see cref="TreeNode{T}" /> is a root node.
/// </summary>
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private ITreeNode<T> ParentReference = null;
}
}