-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBinaryTreeNode.cs
More file actions
156 lines (147 loc) · 6.12 KB
/
Copy pathBinaryTreeNode.cs
File metadata and controls
156 lines (147 loc) · 6.12 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
// =================================================================================================================================
// Copyright (c) RapidField LLC. Licensed under the MIT License. See LICENSE.txt in the project root for license information.
// =================================================================================================================================
using RapidField.SolidInstruments.Core.ArgumentValidation;
using System;
using System.Diagnostics;
using System.Linq;
namespace RapidField.SolidInstruments.Collections
{
/// <summary>
/// Represents a node in a binary tree structure.
/// </summary>
/// <remarks>
/// <see cref="BinaryTreeNode{T}" /> is the default implementation of <see cref="IBinaryTreeNode{T}" />.
/// </remarks>
/// <typeparam name="T">
/// The value type of the node.
/// </typeparam>
public class BinaryTreeNode<T> : TreeNode<T, BinaryTreeNode<T>>, IBinaryTreeNode<T>
{
/// <summary>
/// Initializes a new instance of the <see cref="BinaryTreeNode{T}" /> class.
/// </summary>
public BinaryTreeNode()
: this(default)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="BinaryTreeNode{T}" /> class.
/// </summary>
/// <param name="value">
/// The value of the node.
/// </param>
public BinaryTreeNode(T value)
: this(value, null, null, false, false)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="BinaryTreeNode{T}" /> class.
/// </summary>
/// <param name="value">
/// The value of the node.
/// </param>
/// <param name="leftChild">
/// The left child of the node.
/// </param>
/// <exception cref="ArgumentException">
/// <paramref name="leftChild" /> is invalid.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="leftChild" /> is <see langword="null" />.
/// </exception>
public BinaryTreeNode(T value, BinaryTreeNode<T> leftChild)
: this(value, leftChild, null, true, false)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="BinaryTreeNode{T}" /> class.
/// </summary>
/// <param name="value">
/// The value of the node.
/// </param>
/// <param name="leftChild">
/// The left child of the node.
/// </param>
/// <param name="rightChild">
/// The right child of the node.
/// </param>
/// <exception cref="ArgumentException">
/// One of the specified children is invalid -or- the children both reference the same object.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="leftChild" /> is null or <paramref name="rightChild" /> is <see langword="null" />.
/// </exception>
public BinaryTreeNode(T value, BinaryTreeNode<T> leftChild, BinaryTreeNode<T> rightChild)
: this(value, leftChild, rightChild, true, true)
{
return;
}
/// <summary>
/// Initializes a new instance of the <see cref="BinaryTreeNode{T}" /> class.
/// </summary>
/// <param name="value">
/// The value of the node.
/// </param>
/// <param name="leftChild">
/// The left child of the node.
/// </param>
/// <param name="rightChild">
/// The right child of the node.
/// </param>
/// <param name="rejectNullLeftChild">
/// A value indicating whether or not to raise an <see cref="ArgumentNullException" /> if <paramref name="leftChild" /> is
/// <see langword="null" />.
/// </param>
/// <param name="rejectNullRightChild">
/// A value indicating whether or not to raise an <see cref="ArgumentNullException" /> if <paramref name="rightChild" /> is
/// <see langword="null" />.
/// </param>
/// <exception cref="ArgumentException">
/// One of the specified children is invalid -or- the children both reference the same object.
/// </exception>
/// <exception cref="ArgumentNullException">
/// <paramref name="rejectNullLeftChild" /> is <see langword="true" /> and <paramref name="leftChild" /> is
/// <see langword="null" /> -or- <paramref name="rejectNullRightChild" /> is <see langword="true" /> and
/// <paramref name="rightChild" /> is <see langword="null" />.
/// </exception>
[DebuggerHidden]
private BinaryTreeNode(T value, BinaryTreeNode<T> leftChild, BinaryTreeNode<T> rightChild, Boolean rejectNullLeftChild, Boolean rejectNullRightChild)
: base(value, 2)
{
if (leftChild is null == false)
{
if (AddChild(leftChild) == false)
{
throw new ArgumentException("The specified left child is invalid.", nameof(leftChild));
}
}
else if (rejectNullLeftChild)
{
leftChild.RejectIf().IsNull(nameof(leftChild));
}
if (rightChild is null == false)
{
if (AddChild(rightChild) == false)
{
throw new ArgumentException("The specified right child is invalid.", nameof(rightChild));
}
}
else if (rejectNullRightChild)
{
rightChild.RejectIf().IsNull(nameof(rightChild));
}
}
/// <summary>
/// Gets the left child of the current <see cref="BinaryTreeNode{T}" />.
/// </summary>
public IBinaryTreeNode<T> LeftChild => Children.ElementAtOrDefault(0) as IBinaryTreeNode<T>;
/// <summary>
/// Gets the right child of the current <see cref="BinaryTreeNode{T}" />.
/// </summary>
public IBinaryTreeNode<T> RightChild => Children.ElementAtOrDefault(1) as IBinaryTreeNode<T>;
}
}