-
Notifications
You must be signed in to change notification settings - Fork 299
/
Copy pathTree.cs
252 lines (199 loc) · 5.68 KB
/
Tree.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Advanced.Algorithms.DataStructures;
/// <summary>
/// A tree implementation.
/// </summary>
public class Tree<T> : IEnumerable<T> where T : IComparable
{
private TreeNode<T> Root { get; set; }
public int Count { get; private set; }
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<T> GetEnumerator()
{
return new TreeEnumerator<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>
/// Time complexity: O(n)
/// </summary>
public void Insert(T parent, T child)
{
if (Root == null)
{
Root = new TreeNode<T>(null, child);
Count++;
return;
}
var parentNode = Find(parent);
if (parentNode == null) throw new ArgumentNullException();
var exists = Find(Root, child) != null;
if (exists) throw new ArgumentException("value already exists");
parentNode.Children.InsertFirst(new TreeNode<T>(parentNode, child));
Count++;
}
/// <summary>
/// Time complexity: O(n)
/// </summary>
public void Delete(T value)
{
Delete(Root.Value, value);
}
/// <summary>
/// Time complexity: O(n)
/// </summary>
public IEnumerable<T> Children(T value)
{
return Find(value)?.Children.Select(x => x.Value);
}
private TreeNode<T> Find(T value)
{
if (Root == null) return null;
return Find(Root, value);
}
private int GetHeight(TreeNode<T> node)
{
if (node == null) return -1;
var currentHeight = -1;
foreach (var child in node.Children)
{
var childHeight = GetHeight(child);
if (currentHeight < childHeight) currentHeight = childHeight;
}
currentHeight++;
return currentHeight;
}
private void Delete(T parentValue, T value)
{
var parent = Find(parentValue);
if (parent == null) throw new Exception("Cannot find parent");
var itemToRemove = Find(parent, value);
if (itemToRemove == null) throw new Exception("Cannot find item");
//if item is root
if (itemToRemove.Parent == null)
{
if (itemToRemove.Children.Count() == 0)
{
Root = null;
}
else
{
if (itemToRemove.Children.Count() == 1)
{
Root = itemToRemove.Children.DeleteFirst();
Root.Parent = null;
}
else
{
throw new Exception("Node have multiple children. Cannot delete node unambiguosly");
}
}
}
else
{
if (itemToRemove.Children.Count() == 0)
{
itemToRemove.Parent.Children.Delete(itemToRemove);
}
else
{
if (itemToRemove.Children.Count() == 1)
{
var orphan = itemToRemove.Children.DeleteFirst();
orphan.Parent = itemToRemove.Parent;
itemToRemove.Parent.Children.InsertFirst(orphan);
itemToRemove.Parent.Children.Delete(itemToRemove);
}
else
{
throw new Exception("Node have multiple children. Cannot delete node unambiguosly");
}
}
}
Count--;
}
private TreeNode<T> Find(TreeNode<T> parent, T value)
{
if (parent.Value.CompareTo(value) == 0) return parent;
foreach (var child in parent.Children)
{
var result = Find(child, value);
if (result != null) return result;
}
return null;
}
}
internal class TreeNode<T> : IComparable where T : IComparable
{
internal TreeNode(TreeNode<T> parent, T value)
{
Parent = parent;
Value = value;
Children = new SinglyLinkedList<TreeNode<T>>();
}
internal T Value { get; set; }
internal TreeNode<T> Parent { get; set; }
internal SinglyLinkedList<TreeNode<T>> Children { get; set; }
internal bool IsLeaf => Children.Count() == 0;
public int CompareTo(object obj)
{
return Value.CompareTo(obj as TreeNode<T>);
}
}
internal class TreeEnumerator<T> : IEnumerator<T> where T : IComparable
{
private readonly TreeNode<T> root;
private Stack<TreeNode<T>> progress;
internal TreeEnumerator(TreeNode<T> root)
{
this.root = root;
}
public bool MoveNext()
{
if (root == null) return false;
if (progress == null)
{
progress = new Stack<TreeNode<T>>(root.Children);
Current = root.Value;
return true;
}
if (progress.Count > 0)
{
var next = progress.Pop();
Current = next.Value;
foreach (var child in next.Children) progress.Push(child);
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;
}
}