-
Notifications
You must be signed in to change notification settings - Fork 0
/
TreeHom3r.cs
309 lines (255 loc) · 6.56 KB
/
TreeHom3r.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
// GENERIC IMPLEMENTATION OF NON-BINARY TREES
//**************************************************
using System.Collections.Generic;
public delegate bool TreeNodeCompare<T>(T nodeData1, T nodeData2);
public delegate bool TreeNodeCondition<T>(T nodeData);
public delegate void TreeNodeAction<T>(T nodeData);
public delegate void TreeNodeDo<T> (T source, T target);
public delegate void TreeAction<T>(TreeHom3r<T> subtree);
public class TreeHom3r<T>
{
private T data;
//private LinkedList<Tree<T>> children; // Use linkedlist if you want faster insert. we rather prefer faster access by index
private List<TreeHom3r<T>> children;
private TreeHom3r<T> parent;
//*****************************
public TreeHom3r(T ndata)
{
data = ndata;
children = new List<TreeHom3r<T>>();
parent = null;
}
//*****************************
public void AddChild(T data)
{
//children.AddFirst(new Tree<T>(data)); // With linked list
TreeHom3r<T> child = new TreeHom3r<T>(data);
child.parent = this;
children.Add(child);
}
public bool isEmpty()
{
return (children == null);
}
//*****************************
public TreeHom3r<T> GetChild(int i)
{
// Linked list:
//foreach (Tree<T> n in children)
//if (--i == 0)
// return n;
//return null;
return children [i];
}
//*****************************
public int GetChildCount()
{
return children.Count;
}
//*****************************
public T GetData()
{
return data;
}
//*****************************
public void SetData(T newData)
{
data = newData;
}
//*****************************
public TreeHom3r<T> GetParent()
{
return parent;
}
//*****************************
public void DoIf(TreeHom3r<T> node, TreeNodeCompare<T> comparer, T otherData, TreeNodeAction<T> action)
{
bool comparison = comparer (node.data, otherData);
if (comparison)
{
action(node.data);
foreach (TreeHom3r<T> kid in node.children)
//DoIf (node, comparer, kid.GetData(), action);
DoIf (kid, comparer, otherData, action);
}
else
{
foreach (TreeHom3r<T> kid in node.children)
DoIf (kid, comparer, otherData, action);
}
}
//*****************************
public void DoIf(TreeHom3r<T> node, TreeNodeCompare<T> comparer, T otherData, TreeNodeDo<T> action)
{
bool comparison = comparer (node.data, otherData);
if (comparison)
{
action(node.data, otherData);
foreach (TreeHom3r<T> kid in node.children)
//DoIf (node, comparer, kid.GetData(), action);
DoIf (kid, comparer, otherData, action);
}
else
{
foreach (TreeHom3r<T> kid in node.children)
DoIf (kid, comparer, otherData, action);
}
}
//*****************************
public void DoIf(TreeHom3r<T> node, TreeNodeCondition<T> condition, TreeNodeAction<T> action)
{
bool meetsCondition = condition (node.data);
if (meetsCondition)
{
action(node.data);
foreach (TreeHom3r<T> kid in node.children)
DoIf (kid, condition, action);
}
else
{
foreach (TreeHom3r<T> kid in node.children)
DoIf (kid, condition, action);
}
}
//*****************************
public void DoIf(TreeHom3r<T> node, TreeNodeCompare<T> comparer, T otherData, TreeAction<T> action)
{
bool comparison = comparer (node.data, otherData);
if (comparison)
{
action(node);
foreach (TreeHom3r<T> kid in node.children)
//DoIf (node, comparer, kid.GetData(), action);
DoIf (kid, comparer, otherData, action);
}
else
{
foreach (TreeHom3r<T> kid in node.children)
DoIf (kid, comparer, otherData, action);
}
}
//*****************************
// Do something only to direct children (not recursive), if a condition is met between the parent and each child
public void DoIfParentToChildren(TreeHom3r<T> parent, TreeNodeCompare<T> comparer, TreeAction<T> action)
{
foreach (TreeHom3r<T> kid in parent.children)
{
if (comparer (parent.data, kid.data))
action(kid);
}
}
//*****************************
// Do something only to direct children (not recursive), if a condition is met for each child
public void DoIfToChildren(TreeHom3r<T> parent, TreeNodeCondition<T> condition, TreeAction<T> action)
{
foreach (TreeHom3r<T> kid in parent.children)
{
if (condition (kid.data))
action(kid);
}
}
//*****************************
// Do something only to direct children (not recursive)
public void DoToChildren(TreeHom3r<T> parent, TreeAction<T> action)
{
foreach (TreeHom3r<T> kid in parent.children)
{
action(kid);
}
}
//*****************************
public void DoIf(TreeHom3r<T> node, TreeNodeCondition<T> condition, TreeAction<T> action)
{
bool meetsCondition = condition (node.data);
if (meetsCondition)
action(node);
foreach (TreeHom3r<T> kid in node.children)
DoIf (kid, condition, action);
}
//*****************************
public void Do(TreeHom3r<T> node, TreeNodeAction<T> action)
{
action (node.data);
foreach (TreeHom3r<T> kid in node.children)
Do (kid, action);
}
//*****************************
// public T GetIf(Tree<T> node, TreeNodeCompare<T> comparer, T otherData)
// {
// bool comparison = comparer (node.data, otherData);
// if (comparison)
// {
// return node.data;
// }
// else
// {
// foreach (Tree<T> kid in node.children)
// {
// T kidResult = GetIf (kid, comparer, otherData);
// if (kidResult != null)
// return kidResult;
// }
// }
//
// return default(T);
// }
//
//*****************************
public TreeHom3r<T> GetIf(TreeHom3r<T> node, TreeNodeCompare<T> comparer, T otherData)
{
bool comparison = comparer (node.data, otherData);
if (comparison)
{
return node;
}
else
{
foreach (TreeHom3r<T> kid in node.children)
{
TreeHom3r<T> kidResult = GetIf (kid, comparer, otherData);
if (kidResult != null)
return kidResult;
}
}
return null;
}
//*****************************
public bool MeetsCondition(TreeHom3r<T> node, TreeNodeCondition<T> visitor)
{
bool condition = visitor (node.data);
if (condition)
{
return true;
}
else
{
foreach (TreeHom3r<T> kid in node.children)
{
bool kidResult = MeetsCondition (kid, visitor);
if (kidResult)
return true;
}
}
return false;
}
//*****************************
public int GetChildPosition(TreeNodeCompare<T> visitor, T otherData)
{
int position = -1;
for (int i=0; i < children.Count; i++)
{
bool compare = visitor (children[i].data, otherData);
if (compare)
{
position = i;
break;
}
}
return position;
}
public void Clear()
{
children.Clear();
parent = null;
}
}