-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBinarySearchTreeNode.cs
135 lines (117 loc) · 3.8 KB
/
BinarySearchTreeNode.cs
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
namespace DataStructures.Trees
{
/// <summary>
/// The binary search tree node.
/// </summary>
public class BSTNode<T> : System.IComparable<BSTNode<T>> where T : System.IComparable<T>
{
private T _value;
private BSTNode<T> _parent;
private BSTNode<T> _left;
private BSTNode<T> _right;
public BSTNode() : this(default(T), 0, null, null, null) { }
public BSTNode(T value) : this(value, 0, null, null, null) { }
public BSTNode(T value, int subTreeSize, BSTNode<T> parent, BSTNode<T> left, BSTNode<T> right)
{
Value = value;
Parent = parent;
LeftChild = left;
RightChild = right;
}
public virtual T Value
{
get { return this._value; }
set { this._value = value; }
}
public virtual BSTNode<T> Parent
{
get { return this._parent; }
set { this._parent = value; }
}
public virtual BSTNode<T> LeftChild
{
get { return this._left; }
set { this._left = value; }
}
public virtual BSTNode<T> RightChild
{
get { return this._right; }
set { this._right = value; }
}
/// <summary>
/// Checks whether this node has any children.
/// </summary>
public virtual bool HasChildren
{
get { return (this.ChildrenCount > 0); }
}
/// <summary>
/// Checks whether this node has left child.
/// </summary>
public virtual bool HasLeftChild
{
get { return (this.LeftChild != null); }
}
/// <summary>
/// Check if this node has only one child and whether it is the right child.
/// </summary>
public virtual bool HasOnlyRightChild => !this.HasLeftChild && this.HasRightChild;
/// <summary>
/// Checks whether this node has right child.
/// </summary>
public virtual bool HasRightChild
{
get { return (this.RightChild != null); }
}
/// <summary>
/// Check if this node has only one child and whether it is the left child.
/// </summary>
public virtual bool HasOnlyLeftChild => !this.HasRightChild && this.HasLeftChild;
/// <summary>
/// Checks whether this node is the left child of it's parent.
/// </summary>
public virtual bool IsLeftChild
{
get { return (this.Parent != null && this.Parent.LeftChild == this); }
}
/// <summary>
/// Checks whether this node is the left child of it's parent.
/// </summary>
public virtual bool IsRightChild
{
get { return (this.Parent != null && this.Parent.RightChild == this); }
}
/// <summary>
/// Checks whether this node is a leaf node.
/// </summary>
public virtual bool IsLeafNode
{
get { return (this.ChildrenCount == 0); }
}
/// <summary>
/// Returns number of direct descendents: 0, 1, 2 (none, left or right, or both).
/// </summary>
/// <returns>Number (0,1,2)</returns>
public virtual int ChildrenCount
{
get
{
int count = 0;
if (this.HasLeftChild)
count++;
if (this.HasRightChild)
count++;
return count;
}
}
/// <summary>
/// Compares to.
/// </summary>
public virtual int CompareTo(BSTNode<T> other)
{
if (other == null)
return -1;
return this.Value.CompareTo(other.Value);
}
}//end-of-bstnode
}