-
Notifications
You must be signed in to change notification settings - Fork 293
/
BinaryTree.cs
255 lines (201 loc) · 6.28 KB
/
BinaryTree.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Advanced.Algorithms.DataStructures;
/// <summary>
/// A binary tree implementation using pointers.
/// </summary>
public class BinaryTree<T> : IEnumerable<T> where T : IComparable
{
private BinaryTreeNode<T> Root { get; set; }
public int Count { get; private set; }
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return new BinaryTreeEnumerator<T>(Root);
}
/// <summary>
/// Time complexity: O(n)
/// </summary>
public bool HasItem(T value)
{
if (Root == null) return false;
return Find(Root, value) != null;
}
/// <summary>
/// Time complexity: O(n)
/// </summary>
public int GetHeight()
{
return GetHeight(Root);
}
/// <summary>
/// Only inserts to unambiguous nodes (a node with two children cannot be inserted with a new child unambiguously).
/// Time complexity: O(n)
/// </summary>
/// <summary>
public void Insert(T parent, T child)
{
if (Root == null)
{
Root = new BinaryTreeNode<T>(null, child);
Count++;
return;
}
var parentNode = Find(parent);
if (parentNode == null) throw new Exception("Cannot find parent node");
var exists = Find(Root, child) != null;
if (exists) throw new ArgumentNullException("value already exists");
switch (parentNode.Left)
{
case null when parentNode.Right == null:
parentNode.Left = new BinaryTreeNode<T>(parentNode, child);
break;
case null:
parentNode.Left = new BinaryTreeNode<T>(parentNode, child);
break;
default:
if (parentNode.Right == null)
parentNode.Right = new BinaryTreeNode<T>(parentNode, child);
else
throw new Exception("Cannot insert to a parent with two child node unambiguosly");
break;
}
Count++;
}
/// <summary>
/// Only deletes unambiguous nodes (a node with two children cannot be deleted unambiguously).
/// Time complexity: O(n)
/// </summary>
public void Delete(T value)
{
var node = Find(value);
if (node == null) throw new Exception("Cannot find node");
switch (node.Left)
{
case null when node.Right == null:
if (node.Parent == null)
{
Root = null;
}
else
{
if (node.Parent.Left == node)
node.Parent.Left = null;
else
node.Parent.Right = null;
}
break;
case null when node.Right != null:
node.Right.Parent = node.Parent;
if (node.Parent.Left == node)
node.Parent.Left = node.Right;
else
node.Parent.Right = node.Right;
break;
default:
if (node.Right == null && node.Left != null)
{
node.Left.Parent = node.Parent;
if (node.Parent.Left == node)
node.Parent.Left = node.Left;
else
node.Parent.Right = node.Left;
}
else
{
throw new Exception("Cannot delete two child node unambiguosly");
}
break;
}
Count--;
}
/// <summary>
/// Time complexity: O(n)
/// </summary>
public IEnumerable<T> Children(T value)
{
var node = Find(value);
if (node != null) return new[] { node.Left, node.Right }.Where(x => x != null).Select(x => x.Value);
return null;
}
private int GetHeight(BinaryTreeNode<T> node)
{
if (node == null) return -1;
return Math.Max(GetHeight(node.Left), GetHeight(node.Right)) + 1;
}
private BinaryTreeNode<T> Find(T value)
{
return Root == null ? null : Find(Root, value);
}
private BinaryTreeNode<T> Find(BinaryTreeNode<T> parent, T value)
{
while (true)
{
if (parent == null) return null;
if (parent.Value.CompareTo(value) == 0) return parent;
var left = Find(parent.Left, value);
if (left != null) return left;
parent = parent.Right;
}
}
}
internal class BinaryTreeNode<T> : IComparable where T : IComparable
{
internal BinaryTreeNode(BinaryTreeNode<T> parent, T value)
{
Parent = parent;
Value = value;
}
internal T Value { get; set; }
internal BinaryTreeNode<T> Parent { get; set; }
internal BinaryTreeNode<T> Left { get; set; }
internal BinaryTreeNode<T> Right { get; set; }
internal bool IsLeaf => Left == null && Right == null;
public int CompareTo(object obj)
{
return Value.CompareTo(obj as BinaryTreeNode<T>);
}
}
internal class BinaryTreeEnumerator<T> : IEnumerator<T> where T : IComparable
{
private readonly BinaryTreeNode<T> root;
private Stack<BinaryTreeNode<T>> progress;
internal BinaryTreeEnumerator(BinaryTreeNode<T> root)
{
this.root = root;
}
public bool MoveNext()
{
if (root == null) return false;
if (progress == null)
{
progress = new Stack<BinaryTreeNode<T>>(new[] { root.Left, root.Right }.Where(x => x != null));
Current = root.Value;
return true;
}
if (progress.Count > 0)
{
var next = progress.Pop();
Current = next.Value;
foreach (var node in new[] { next.Left, next.Right }.Where(x => x != null)) progress.Push(node);
return true;
}
return false;
}
public void Reset()
{
progress = null;
Current = default;
}
public T Current { get; private set; }
object IEnumerator.Current => Current;
public void Dispose()
{
progress = null;
}
}