-
Notifications
You must be signed in to change notification settings - Fork 0
/
Binary_Search_Tree_Tests.cs
284 lines (234 loc) · 9.13 KB
/
Binary_Search_Tree_Tests.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
278
279
280
281
282
283
284
using System;
using System.Collections.Generic;
using System.Linq;
using DataStructures.BinarySearchTree;
using NUnit.Framework;
namespace DataStructures.Tests;
public static class BinarySearchTreeTests
{
[Test]
public static void Constructor_UseCustomComparer_FormsCorrectTree()
{
var cmpFunc = Comparer<string>.Create((x, y) => x.Length - y.Length);
var tree = new BinarySearchTree<string>(cmpFunc);
var elems = new[] { "z", "yy", "vvv", "bbbb", "fffff", "pppppp" };
tree.AddRange(elems);
Assert.That(tree.Search("vvv"), Is.Not.Null);
Assert.That(tree.Search("vvv")!.Right, Is.Not.Null);
Assert.That(tree.Search("vvv")!.Right!.Key, Is.EqualTo("bbbb"));
}
[Test]
public static void Add_MultipleKeys_FormsCorrectBST()
{
var tree = new BinarySearchTree<int>();
tree.Add(5);
Assert.That(tree.Count, Is.EqualTo(1));
tree.Add(3);
Assert.That(tree.Count, Is.EqualTo(2));
tree.Add(4);
Assert.That(tree.Count, Is.EqualTo(3));
tree.Add(2);
Assert.That(tree.Count, Is.EqualTo(4));
var rootNode = tree.Search(5);
Assert.That(rootNode!.Key, Is.EqualTo(5));
Assert.That(rootNode!.Left!.Key, Is.EqualTo(3));
Assert.That(rootNode!.Right, Is.Null);
var threeNode = tree.Search(3);
Assert.That(threeNode!.Key, Is.EqualTo(3));
Assert.That(threeNode!.Left!.Key, Is.EqualTo(2));
Assert.That(threeNode!.Right!.Key, Is.EqualTo(4));
var twoNode = tree.Search(2);
Assert.That(twoNode!.Left, Is.Null);
Assert.That(twoNode!.Right, Is.Null);
var fourNode = tree.Search(4);
Assert.That(fourNode!.Left, Is.Null);
Assert.That(fourNode!.Right, Is.Null);
}
[Test]
public static void Add_KeyAlreadyInTree_ThrowsCorrectException()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2 });
_ = Assert.Throws<ArgumentException>(() => tree.Add(5));
}
[Test]
public static void AddRange_MultipleKeys_FormsCorrectBST()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2 });
var rootNode = tree.Search(5);
Assert.That(rootNode!.Key, Is.EqualTo(5));
Assert.That(rootNode!.Left!.Key, Is.EqualTo(3));
Assert.That(rootNode!.Right, Is.Null);
var threeNode = tree.Search(3);
Assert.That(threeNode!.Key, Is.EqualTo(3));
Assert.That(threeNode!.Left!.Key, Is.EqualTo(2));
Assert.That(threeNode!.Right!.Key, Is.EqualTo(4));
var twoNode = tree.Search(2);
Assert.That(twoNode!.Left, Is.Null);
Assert.That(twoNode!.Right, Is.Null);
var fourNode = tree.Search(4);
Assert.That(fourNode!.Left, Is.Null);
Assert.That(fourNode!.Right, Is.Null);
}
[Test]
public static void Search_MultipleKeys_FindsAllKeys()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2, 7, 6, 8 });
Assert.That(tree.Search(2)!.Key, Is.EqualTo(2));
Assert.That(tree.Search(3)!.Key, Is.EqualTo(3));
Assert.That(tree.Search(4)!.Key, Is.EqualTo(4));
Assert.That(tree.Search(5)!.Key, Is.EqualTo(5));
Assert.That(tree.Search(6)!.Key, Is.EqualTo(6));
Assert.That(tree.Search(7)!.Key, Is.EqualTo(7));
Assert.That(tree.Search(8)!.Key, Is.EqualTo(8));
}
[Test]
public static void Contains_MultipleKeys_FindsAllKeys()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2, 7, 6, 8 });
Assert.That(tree.Contains(2), Is.True);
Assert.That(tree.Contains(3), Is.True);
Assert.That(tree.Contains(4), Is.True);
Assert.That(tree.Contains(5), Is.True);
Assert.That(tree.Contains(6), Is.True);
Assert.That(tree.Contains(7), Is.True);
Assert.That(tree.Contains(8), Is.True);
}
[Test]
public static void Remove_LeafNodes_CorrectlyRemovesNodes()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2, 7, 6, 8 });
var twoRemoveResult = tree.Remove(2);
Assert.That(twoRemoveResult, Is.True);
Assert.That(tree.Search(2), Is.Null);
Assert.That(tree.Search(3)!.Left, Is.Null);
Assert.That(tree.Search(3)!.Right, Is.Not.Null);
Assert.That(tree.Count, Is.EqualTo(6));
var fourRemoveResult = tree.Remove(4);
Assert.That(fourRemoveResult, Is.True);
Assert.That(tree.Search(4), Is.Null);
Assert.That(tree.Search(3)!.Left, Is.Null);
Assert.That(tree.Search(3)!.Right, Is.Null);
Assert.That(tree.Count, Is.EqualTo(5));
}
[Test]
public static void Remove_NodesWithOneChild_CorrectlyRemovesNodes()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2, 7, 6, 8 });
tree.Remove(4);
var threeRemoveResult = tree.Remove(3);
Assert.That(threeRemoveResult, Is.True);
Assert.That(tree.Search(3), Is.Null);
Assert.That(tree.Search(2)!.Left, Is.Null);
Assert.That(tree.Search(2)!.Right, Is.Null);
Assert.That(tree.Count, Is.EqualTo(5));
tree.Remove(6);
var sevenRemoveResult = tree.Remove(7);
Assert.That(sevenRemoveResult, Is.True);
Assert.That(tree.Search(7), Is.Null);
Assert.That(tree.Search(8)!.Left, Is.Null);
Assert.That(tree.Search(8)!.Right, Is.Null);
Assert.That(tree.Count, Is.EqualTo(3));
}
[Test]
public static void Remove_NodesWithTwoChildren_CorrectlyRemovesNodes()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2, 7, 6, 8 });
var sevenRemoveResult = tree.Remove(7);
Assert.That(sevenRemoveResult, Is.True);
Assert.That(tree.Search(7), Is.Null);
Assert.That(tree.Search(6)!.Left, Is.Null);
Assert.That(tree.Search(6)!.Right, Is.Not.Null);
Assert.That(tree.Count, Is.EqualTo(6));
}
[Test]
public static void Remove_NonExistentElement_ReturnsFalse()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2, 7, 6, 8 });
Assert.That(tree.Remove(999), Is.False);
Assert.That(tree.Count, Is.EqualTo(7));
}
[Test]
public static void Remove_EmptyTree_ReturnsFalse()
{
var tree = new BinarySearchTree<int>();
Assert.That(tree.Remove(8), Is.False);
Assert.That(tree.Count, Is.EqualTo(0));
}
[Test]
public static void Remove_RemoveRoot_CorrectlyRemovesRoot()
{
var tree = new BinarySearchTree<int>();
tree.Add(5);
tree.Remove(5);
Assert.That(tree.Count, Is.EqualTo(0));
Assert.That(tree.Search(5), Is.Null);
tree.AddRange(new List<int> { 5, 4, 6 });
tree.Remove(5);
Assert.That(tree.Count, Is.EqualTo(2));
Assert.That(tree.Search(5), Is.Null);
Assert.That(tree.Search(4), Is.Not.Null);
Assert.That(tree.Search(6), Is.Not.Null);
Assert.That(tree.Search(4)!.Right!.Key, Is.EqualTo(6));
}
[Test]
public static void GetMax_NonEmptyTree_ReturnsCorrectValue()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2, 7, 6, 8 });
Assert.That(tree.GetMax()!.Key, Is.EqualTo(8));
}
[Test]
public static void GetMax_EmptyTree_ReturnsDefaultValue()
{
var tree = new BinarySearchTree<int>();
Assert.That(tree.GetMax(), Is.Null);
}
[Test]
public static void GetMin_NonEmptyTree_ReturnsCorrectValue()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2, 7, 6, 8 });
Assert.That(tree.GetMin()!.Key, Is.EqualTo(2));
}
[Test]
public static void GetMin_EmptyTree_ReturnsDefaultValue()
{
var tree = new BinarySearchTree<int>();
Assert.That(tree.GetMin(), Is.Null);
}
[Test]
public static void GetKeysInOrder_MultipleKeys_ReturnsAllKeysInCorrectOrder()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2, 7, 6, 8 });
var keys = tree.GetKeysInOrder();
var expected = new List<int> { 2, 3, 4, 5, 6, 7, 8 };
Assert.That(keys.SequenceEqual(expected), Is.True);
}
[Test]
public static void GetKeysPreOrder_MultipleKeys_ReturnsAllKeysInCorrectOrder()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2, 7, 6, 8 });
var keys = tree.GetKeysPreOrder();
var expected = new List<int> { 5, 3, 2, 4, 7, 6, 8 };
Assert.That(keys.SequenceEqual(expected), Is.True);
}
[Test]
public static void GetKeysPostOrder_MultipleKeys_ReturnsAllKeysInCorrectOrder()
{
var tree = new BinarySearchTree<int>();
tree.AddRange(new List<int> { 5, 3, 4, 2, 7, 6, 8 });
var keys = tree.GetKeysPostOrder();
var expected = new List<int> { 2, 4, 3, 6, 8, 7, 5 };
Assert.That(keys.SequenceEqual(expected), Is.True);
}
}