/
BinarySearchTree.cs
277 lines (246 loc) · 9.14 KB
/
BinarySearchTree.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
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
using System;
using System.Collections.Generic;
using System.Collections;
namespace DSA.DataStructures.Trees
{
/// <summary>
/// Represents a binary search tree.
/// </summary>
public class BinarySearchTree<T> : IEnumerable<T>
{
/// <summary>
/// The comparer of the elements in the <see cref="BinarySearchTree{T}"/>.
/// </summary>
internal IComparer<T> comparer;
/// <summary>
/// Gets the tree root of the <see cref="BinarySearchTree{T}"/>.
/// </summary>
public BinarySearchTreeNode<T> Root { get; internal set; }
/// <summary>
/// Gets the number of elements in the <see cref="BinarySearchTree{T}"/>.
/// </summary>
public virtual int Count { get; internal set; }
/// <summary>
/// Creates a new instance of the <see cref="BinarySearchTree{T}"/> class and uses the default <see cref="IComparer{T}"/> implementation to compare elements.
/// </summary>
public BinarySearchTree() : this(null) { }
/// <summary>
/// Creates a new instance of the <see cref="BinarySearchTree{T}"/> class and uses the specified <see cref="IComparer{T}"/> implementation to compare elements.
/// </summary>
/// <param name="comparer">The <see cref="IComparer{T}"/> implementation to use when comparing elements, or null to use the default comparer <see cref="Comparer{T}.Default"/>.</param>
public BinarySearchTree(IComparer<T> comparer)
{
this.comparer = comparer ?? Comparer<T>.Default;
Root = null;
Count = 0;
}
/// <summary>
/// Adds an element to the <see cref="BinarySearchTree{T}"/>.
/// </summary>
/// <param name="value">The value to add.</param>
public virtual void Add(T value)
{
if (Root == null)
{
Root = new BinarySearchTreeNode<T>(value);
Count++;
return;
}
var curNode = Root;
var lastNode = Root;
bool addedToLeftSide = true;
while (curNode != null)
{
int cmp = comparer.Compare(value, curNode.Value);
if (cmp < 0)
{
lastNode = curNode;
curNode = curNode.Left;
addedToLeftSide = true;
}
else if (cmp > 0)
{
lastNode = curNode;
curNode = curNode.Right;
addedToLeftSide = false;
}
else throw new ArgumentException("Tried to insert duplicate value!");
}
if (addedToLeftSide)
{
lastNode.Left = new BinarySearchTreeNode<T>(value);
Count++;
}
else
{
lastNode.Right = new BinarySearchTreeNode<T>(value);
Count++;
}
}
/// <summary>
/// Removes an element from the <see cref="BinarySearchTree{T}"/>.
/// </summary>
/// <param name="value">The value to remove.</param>
/// <returns>true if the item is successfully removed; otherwise false. Also returns false if item is not found.</returns>
public virtual bool Remove(T value)
{
if (Root == null) return false;
var curNode = Root;
var lastNode = Root;
bool lastWasLeftSide = true;
while (curNode != null)
{
int cmp = comparer.Compare(value, curNode.Value);
if (cmp < 0)
{
lastNode = curNode;
curNode = curNode.Left;
lastWasLeftSide = true;
}
else if (cmp > 0)
{
lastNode = curNode;
curNode = curNode.Right;
lastWasLeftSide = false;
}
else
{
if (curNode.Right == null)
{
if (lastWasLeftSide)
{
if (curNode == Root) Root = curNode.Left;
else lastNode.Left = curNode.Left;
}
else
{
lastNode.Right = curNode.Left;
}
}
else
{
BinarySearchTreeNode<T> min = null;
var rightNode = FindAndRemoveMin(curNode.Right, ref min);
min.Right = rightNode;
min.Left = curNode.Left;
if (lastWasLeftSide)
{
if (curNode == Root) Root = min;
else lastNode.Left = min;
}
else
{
lastNode.Right = min;
}
}
curNode.Invalidate();
Count--;
return true;
}
}
return false;
}
/// <summary>
/// Finds the min node in the subtree and returns the root of the new subtree
/// </summary>
private BinarySearchTreeNode<T> FindAndRemoveMin(BinarySearchTreeNode<T> subtreeRoot, ref BinarySearchTreeNode<T> min)
{
if (subtreeRoot.Left == null)
{
if (subtreeRoot.Right == null)
{
min = subtreeRoot;
return null;
}
else
{
min = subtreeRoot;
return subtreeRoot.Right;
}
}
var curNode = subtreeRoot;
var lastNode = subtreeRoot;
while (curNode.Left != null)
{
lastNode = curNode;
curNode = curNode.Left;
}
lastNode.Left = curNode.Right;
min = curNode;
return subtreeRoot;
}
/// <summary>
/// Determines whether a value is in the <see cref="BinarySearchTree{T}"/>.
/// </summary>
/// <param name="value">The value to check.</param>
/// <returns>true if item is found; otherwise false.</returns>
public virtual bool Contains(T value)
{
if (Root == null) return false;
var curNode = Root;
while (curNode != null)
{
int cmp = comparer.Compare(value, curNode.Value);
if (cmp == 0) return true;
if (cmp < 0) curNode = curNode.Left;
if (cmp > 0) curNode = curNode.Right;
}
return false;
}
/// <summary>
/// Removes all elements from the <see cref="BinarySearchTree{T}"/>.
/// </summary>
public virtual void Clear()
{
if (Root != null)
{
HashSet<BinarySearchTreeNode<T>> cleared = new HashSet<BinarySearchTreeNode<T>>();
Stack<BinarySearchTreeNode<T>> stack = new Stack<BinarySearchTreeNode<T>>();
stack.Push(Root);
while (stack.Count > 0)
{
BinarySearchTreeNode<T> curNode = stack.Peek();
if (curNode.Left == null || cleared.Contains(curNode.Left))
{
cleared.Add(curNode);
stack.Pop();
if (curNode.Right != null) stack.Push(curNode.Right);
curNode.Invalidate();
}
else stack.Push(curNode.Left);
}
}
Root = null;
Count = 0;
}
/// <summary>
/// Returns an enumerator that iterates through the <see cref="BinarySearchTree{T}"/>.
/// </summary>
/// <returns>Returns the elements in ascending order.</returns>
public virtual IEnumerator<T> GetEnumerator()
{
if (Root != null)
{
HashSet<BinarySearchTreeNode<T>> returned = new HashSet<BinarySearchTreeNode<T>>();
Stack<BinarySearchTreeNode<T>> stack = new Stack<BinarySearchTreeNode<T>>();
stack.Push(Root);
while (stack.Count > 0)
{
BinarySearchTreeNode<T> curNode = stack.Peek();
if (curNode.Left == null || returned.Contains(curNode.Left))
{
returned.Add(curNode);
stack.Pop();
yield return curNode.Value;
if (curNode.Right != null) stack.Push(curNode.Right);
}
else stack.Push(curNode.Left);
}
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
}