-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTreeNode.cs
More file actions
376 lines (355 loc) · 10.3 KB
/
TreeNode.cs
File metadata and controls
376 lines (355 loc) · 10.3 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
using BytecodeApi.Extensions;
using System.Collections;
using System.Diagnostics;
namespace BytecodeApi.Data;
/// <summary>
/// Represents an entry in a hierarchical tree structure.
/// </summary>
/// <typeparam name="T">The type of the value and its children.</typeparam>
[DebuggerDisplay($"{nameof(TreeNode<T>)}: Value = {{Value}}, Children: {{Children.Count}}")]
public class TreeNode<T> : IEnumerable<TreeNode<T>>, IEquatable<TreeNode<T>>
{
private readonly List<TreeNode<T>> Children;
/// <summary>
/// Gets or sets the value that is associated with this node.
/// </summary>
public T Value { get; set; }
/// <summary>
/// Gets the child node at the specified index.
/// </summary>
/// <param name="index">The index at which to retrieve the child node.</param>
public TreeNode<T> this[int index]
{
get
{
Check.IndexOutOfRange(index, Count);
return Children[index];
}
}
/// <summary>
/// Gets the number of child nodes.
/// </summary>
public int Count => Children.Count;
/// <summary>
/// Gets the parent node, or <see langword="null" />, if this node is the root node.
/// </summary>
public TreeNode<T>? Parent { get; private set; }
/// <summary>
/// Gets the root node. If this node is already the root node, <see langword="this" /> is returned.
/// </summary>
public TreeNode<T>? Root => Parent == null ? this : Parent.Root;
/// <summary>
/// Gets the sibling that is to the left of this node. If this node is already the root node, or if this node is the first child, <see langword="null" /> is returned.
/// </summary>
public TreeNode<T>? Left
{
get
{
if (Parent == null)
{
return null;
}
else
{
int index = Parent.Children.IndexOf(this);
return index <= 0 ? null : Parent.Children[index - 1];
}
}
}
/// <summary>
/// Gets the sibling that is to the right of this node. If this node is already the root node, or if this node is the last child, <see langword="null" /> is returned.
/// </summary>
public TreeNode<T>? Right
{
get
{
if (Parent == null)
{
return null;
}
else
{
int index = Parent.Children.IndexOf(this);
return index < 0 || index >= Parent.Children.Count - 1 ? null : Parent.Children[index + 1];
}
}
}
/// <summary>
/// Gets the depth of this node, where 0 represents the root node, and 1 represents a node within the root, and so on.
/// </summary>
public int Level => Parent == null ? 0 : Parent.Level + 1;
/// <summary>
/// Initializes a new instance of the <see cref="TreeNode{T}" /> class.
/// </summary>
/// <param name="value">The value that is associated with this node.</param>
public TreeNode(T value)
{
Value = value;
Children = [];
}
/// <summary>
/// Creates a new child node with the specified value.
/// </summary>
/// <param name="value">The value that is associated with the new child node.</param>
/// <returns>
/// The newly created child node.
/// </returns>
public TreeNode<T> Add(T value)
{
return Add(new TreeNode<T>(value));
}
/// <summary>
/// Adds the specified child node. If <paramref name="node" /> is already part of a tree, it will be removed from its current parent node.
/// </summary>
/// <param name="node">The child node to be added.</param>
/// <returns>
/// A reference to <paramref name="node" />.
/// </returns>
public TreeNode<T> Add(TreeNode<T> node)
{
Check.ArgumentNull(node);
node.Parent?.Remove(node);
Children.Add(node);
return node;
}
/// <summary>
/// Removes the child node from this <see cref="TreeNode{T}" />.
/// </summary>
/// <param name="node">The <see cref="TreeNode{T}" /> to remove.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="node" /> is successfully removed;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Remove(TreeNode<T> node)
{
if (Children.Remove(node))
{
node.Parent = null;
return true;
}
else
{
return false;
}
}
/// <summary>
/// Removes all child nodes that satisfy a specified condition.
/// </summary>
/// <param name="predicate">A function to test each child node for a condition.</param>
/// <returns>
/// The number of removed child nodes.
/// </returns>
public int RemoveAll(Func<TreeNode<T>, bool> predicate)
{
Check.ArgumentNull(predicate);
List<TreeNode<T>> removed = [];
foreach (TreeNode<T> node in Children)
{
if (predicate(node))
{
node.Parent = null;
removed.Add(node);
}
}
Children.RemoveRange(removed);
return removed.Count;
}
/// <summary>
/// Removes all child nodes where the value satisfies a specified condition.
/// </summary>
/// <param name="predicate">A function to test each child node's value for a condition.</param>
/// <returns>
/// The number of removed child nodes.
/// </returns>
public int RemoveAll(Func<T, bool> predicate)
{
Check.ArgumentNull(predicate);
List<TreeNode<T>> removed = [];
foreach (TreeNode<T> node in Children)
{
if (predicate(node.Value))
{
node.Parent = null;
removed.Add(node);
}
}
Children.RemoveRange(removed);
return removed.Count;
}
/// <summary>
/// Removes a range of child nodes.
/// </summary>
/// <param name="nodes">A collection of nodes to remove.</param>
public void RemoveRange(IEnumerable<TreeNode<T>> nodes)
{
Check.ArgumentNull(nodes);
foreach (TreeNode<T> node in nodes.ToArray())
{
Remove(node);
}
}
/// <summary>
/// Removes all child nodes.
/// </summary>
public void Clear()
{
foreach (TreeNode<T> node in Children)
{
node.Parent = null;
}
Children.Clear();
}
/// <summary>
/// Retrieves all ancestor nodes, excluding this node.
/// </summary>
/// <returns>
/// An <see cref="IEnumerable{T}" /> that iterates all ancestor nodes, excluding this node.
/// </returns>
public IEnumerable<TreeNode<T>> Ancestors()
{
TreeNode<T>? current = Parent;
while (current != null)
{
yield return current;
current = current.Parent;
}
}
/// <summary>
/// Retrieves all ancestor nodes, including this node.
/// </summary>
/// <returns>
/// An <see cref="IEnumerable{T}" /> that iterates all ancestor nodes, including this node.
/// </returns>
public IEnumerable<TreeNode<T>> AncestorsAndSelf()
{
yield return this;
foreach (TreeNode<T> node in Ancestors())
{
yield return node;
}
}
/// <summary>
/// Retrieves all descendant nodes, excluding this node.
/// </summary>
/// <returns>
/// An <see cref="IEnumerable{T}" /> that iterates all descendant nodes, excluding this node.
/// </returns>
public IEnumerable<TreeNode<T>> Descendants()
{
foreach (TreeNode<T> child in Children)
{
yield return child;
foreach (TreeNode<T> node in child.Descendants())
{
yield return node;
}
}
}
/// <summary>
/// Retrieves all descendant nodes, including this node.
/// </summary>
/// <returns>
/// An <see cref="IEnumerable{T}" /> that iterates all descendant nodes, including this node.
/// </returns>
public IEnumerable<TreeNode<T>> DescendantsAndSelf()
{
yield return this;
foreach (TreeNode<T> node in Descendants())
{
yield return node;
}
}
/// <summary>
/// Retrieves all sibling nodes from the same parent, excluding this node. If this node represents the root of the tree, an empty sequence is returned.
/// </summary>
/// <returns>
/// An <see cref="IEnumerable{T}" /> that iterates all sibling nodes from the same parent, excluding this node.
/// </returns>
public IEnumerable<TreeNode<T>> Siblings()
{
foreach (TreeNode<T> sibling in SiblingsAndSelf())
{
if (sibling != this)
{
yield return sibling;
}
}
}
/// <summary>
/// Retrieves all sibling nodes from the same parent, including this node. If this node represents the root of the tree, an empty sequence is returned.
/// </summary>
/// <returns>
/// An <see cref="IEnumerable{T}" /> that iterates all sibling nodes from the same parent, including this node.
/// </returns>
public IEnumerable<TreeNode<T>> SiblingsAndSelf()
{
if (Parent != null)
{
foreach (TreeNode<T> sibling in Parent)
{
yield return sibling;
}
}
}
/// <summary>
/// Returns the name of this <see cref="TreeNode{T}" />.
/// </summary>
/// <returns>
/// The name of this <see cref="TreeNode{T}" />.
/// </returns>
public override string ToString()
{
return Value?.ToString() ?? "";
}
/// <summary>
/// Determines whether the specified <see cref="object" /> is equal to this instance.
/// </summary>
/// <param name="obj">The <see cref="object" /> to compare with this instance.</param>
/// <returns>
/// <see langword="true" />, if the specified <see cref="object" /> is equal to this instance;
/// otherwise, <see langword="false" />.
/// </returns>
public override bool Equals([NotNullWhen(true)] object? obj)
{
return obj is TreeNode<T> node && Equals(node);
}
/// <summary>
/// Determines whether <see cref="Value" /> of this instance is equal to that of another <see cref="TreeNode{T}" />.
/// </summary>
/// <param name="other">The <see cref="TreeNode{T}" /> to compare to this instance.</param>
/// <returns>
/// <see langword="true" />, if <see cref="Value" /> of this instance is equal to that of the <paramref name="other" /> parameter;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Equals([NotNullWhen(true)] TreeNode<T>? other)
{
return
other != null &&
CSharp.TypeEquals(this, other) &&
Equals(Value, other.Value);
}
/// <summary>
/// Returns a hash code for this <see cref="TreeNode{T}" />.
/// </summary>
/// <returns>
/// The hash code for this <see cref="TreeNode{T}" /> instance.
/// </returns>
public override int GetHashCode()
{
return HashCode.Combine(Value);
}
/// <summary>
/// Returns an enumerator that iterates through the <see cref="TreeNode{T}" />.
/// </summary>
/// <returns>
/// An enumerator that can be used to iterate through the <see cref="TreeNode{T}" />.
/// </returns>
public IEnumerator<TreeNode<T>> GetEnumerator()
{
return Children.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Children.GetEnumerator();
}
}