-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinarySearchTree.cs
366 lines (332 loc) · 11.3 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
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace AT3
{
class BinarySearchTree<T>
{
/// <summary>
/// List of staff
/// </summary>
private List<string> staff;
/// <summary>
/// Tree Node generic object called Root
/// </summary>
private TreeNode<T> root;
/// <summary>
/// Binary Search Tree Constructor
/// </summary>
public BinarySearchTree()
{
root = null;
staff = new List<string>();
}
public TreeNode<T> getRoot()
{
return root;
}
public void setRoot(TreeNode<T> _root)
{
root = _root;
}
/// <summary>
/// Returns the amount of staff in the staff list
/// </summary>
public int CountStaff()
{
return staff.Count();
}
/// <summary>
/// Returns the staff name at passed staff list index.
/// </summary>
public string ElementStaff(int i)
{
return staff.ElementAt(i);
}
/// <summary>
/// Adds to the staff list.
/// </summary>
public void AddStaff(string _name)
{
staff.Add(_name);
}
/// <summary>
/// Clears the staff list.
/// </summary>
public void ClearStaff()
{
staff.Clear();
}
/// <summary>
/// Inserts the data passed to this function into the tree
/// </summary>
public void Insert(T data)
{
root = Insert(data, root);
}
/// <summary>
/// Deletes the TreeNode<T> containing data passed to this function.
/// </summary>
public void Delete(T data)
{
root = Delete(root, data);
}
/// <summary>
/// Returns the element found at bottom left of the tree.
/// </summary>
public T FindMin()
{
return ElementAt(FindMin(root));
}
/// <summary>
/// Searches the tree for a branch containing passed data.
/// </summary>
public T Find(T data)
{
return ElementAt(Find(data, root));
}
/// <summary>
/// Returns the data element of passed TreeNode<T> object/
/// </summary>
private T ElementAt(TreeNode<T> branch)
{
if (branch == null)
{
return default(T);
}
else
{
return branch.getElement();
}
}
/// <summary>
/// Finds the tree node containing the data object passed, starting from the TreeNode<T> passed to this function
/// and returns the TreeNode<T> containing said data.
/// </summary>
private TreeNode<T> Find(T data, TreeNode<T> branch)
{
while (branch != null)
{
if ((data as IComparable).CompareTo(branch.getElement()) < 0)
{
branch = branch.getLeft();
}
else if ((data as IComparable).CompareTo(branch.getElement()) > 0)
{
branch = branch.getRight();
}
else
{
return branch;
}
}
return null;
}
/// <summary>
/// Finds bottom left node starting from the TreeNode<T> provided
/// </summary>
private TreeNode<T> FindMin(TreeNode<T> branch)
{
if (branch != null)
{
while (branch.getLeft() != null)
{
branch = branch.getLeft();
}
}
return branch;
}
/// <summary>
/// Adds each node from the tree to the staff list recursively, alphabetically arranged
/// </summary>
public void InOrder(TreeNode<T> node)
{
if (node == null)
{
return;
}
InOrder(node.getLeft());
staff.Add(node.getElement().ToString());
InOrder(node.getRight());
}
/// <summary>
/// Inserts the data into the tree, alphabetically arranged and balanced.
/// </summary>
public TreeNode<T> Insert(T data, TreeNode<T> branch)
{
if (branch == null)
{
branch = new TreeNode<T>(data);
staff.Add(branch.getElement().ToString());
}
else if ((data as IComparable).CompareTo(branch.getElement()) < 0)
{
branch.setLeft(Insert(data, branch.getLeft()));
branch.setHeight(UpdateHeight(branch));
branch = BalanceNode(branch, data);
}
else if ((data as IComparable).CompareTo(branch.getElement()) > 0)
{
branch.setRight(Insert(data, branch.getRight()));
branch.setHeight(UpdateHeight(branch));
branch = BalanceNode(branch, data);
}
else
{
throw new Exception("Duplicate item");
}
return branch;
}
/// <summary>
/// Updates the height of the TreeNode<T>
/// </summary>
public int UpdateHeight(TreeNode<T> node)
{
if (node.getLeft() != null && node.getRight() != null)
{
return 1 + Math.Max(node.getLeft().getHeight(), node.getRight().getHeight());
}
else if (node.getLeft() != null && node.getRight() == null)
{
return node.getLeft().getHeight() + 1;
}
else if (node.getLeft() == null && node.getRight() != null)
{
return node.getRight().getHeight() + 1;
}
else
{
return 1;
}
}
/// <summary>
/// Balances the TreeNode<T> if the tree height on the left of the tree is higher than the right and vice-versa, by rotating its nodes.
/// </summary>
public TreeNode<T> BalanceNode(TreeNode<T> node, T data)
{
int balance = GetBalance(node);
if (balance > 1)
{
if (GetBalance(node.getLeft()) >= 0)
{
node = RightRotate(node);
}
else
{
node.setLeft(LeftRotate(node.getLeft()));
node = RightRotate(node);
}
}
else if (balance < -1)
{
if (GetBalance(node.getRight()) <= 0)
{
node = LeftRotate(node);
}
else
{
node.setRight(RightRotate(node.getRight()));
node = LeftRotate(node);
}
}
return node;
}
/// <summary>
/// Checks whether the tree is balanced.
/// </summary>
private int GetBalance(TreeNode<T> branch)
{
int height = 0;
int left = 0;
int right = 0;
if (branch != null)
{
if (branch.getLeft() != null)
left = branch.getLeft().getHeight();
if (branch.getRight() != null)
right = branch.getRight().getHeight();
height = left - right;
}
return height;
}
/// <summary>
/// Rotates TreeNode<T> passed down(height) and to the left of finalNode.
/// </summary>
public TreeNode<T> LeftRotate(TreeNode<T> currentNode)
{
TreeNode<T> finalNode = currentNode.getRight();
TreeNode<T> subTree = finalNode.getLeft();
finalNode.setLeft(currentNode);
currentNode.setRight(subTree);
currentNode.setHeight(UpdateHeight(currentNode));
finalNode.setHeight(UpdateHeight(finalNode));
return finalNode;
}
/// <summary>
/// Rotates TreeNode<T> passed down(height) and to the right of finalNode.
/// </summary>
public TreeNode<T> RightRotate(TreeNode<T> currentNode)
{
TreeNode<T> finalNode = currentNode.getLeft();
TreeNode<T> subTree = finalNode.getRight();
finalNode.setRight(currentNode);
currentNode.setLeft(subTree);
currentNode.setHeight(UpdateHeight(currentNode));
finalNode.setHeight(UpdateHeight(finalNode));
return finalNode;
}
/// <summary>
/// Recursively moves through the tree to find the node to be deleted.
/// When it finds the node it tests to see if the node has zero, one or two children.
/// If the node has one or zero children it deletes the node.
/// If the node has two children it finds the minimum node of the right sub-tree which it sets as a temp node, then
/// sets the current node's data equal to the temp node's.
/// Recursively moves through the current node's sub-tree and deletes the minimum node.
/// </summary>
public TreeNode<T> Delete(TreeNode<T> branch, T data)
{
// if Current Node is null
if (branch == null) return branch;
// else iterate down (left or right)
else if ((data as IComparable).CompareTo(branch.getElement()) < 0)
{
branch.setLeft(Delete(branch.getLeft(), data));
}
else if ((data as IComparable).CompareTo(branch.getElement()) > 0)
{
branch.setRight(Delete(branch.getRight(), data));
}
// else Node has been found
else
{
// Case 1: No Children
if (branch.getLeft() == null && branch.getRight() == null)
branch = null;
// Case 2: One Child
else if (branch.getLeft() == null)
branch = branch.getRight();
else if (branch.getRight() == null)
branch = branch.getLeft();
// Case 3: Two Children
else
{
TreeNode<T> temp = FindMin(branch.getRight());
branch.setElement(temp.getElement());
branch.setRight(Delete(branch.getRight(), temp.getElement()));
}
}
// Set the new height & balance
if (branch != null)
{
branch.setHeight(UpdateHeight(branch));
branch = BalanceNode(branch, data);
}
return branch;
}
// Prints the Balanced BST as a string
public override string ToString()
{
return root.ToString();
}
}
}