/
TreeNode.cs
73 lines (62 loc) · 1.75 KB
/
TreeNode.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace anagrammes
{
public class TreeNode
{
private Anagramme _value;
private Dictionary<char, TreeNode> _children = new Dictionary<char, TreeNode>();
public TreeNode(Anagramme value)
{
_value = value;
}
public TreeNode this[char c]
{
get
{
if(_children.ContainsKey(c))
{
return _children[c];
}
else
{
return null;
}
}
}
public TreeNode Parent { get; private set; }
public Anagramme Value { get { return _value; } }
public Dictionary<char, TreeNode> Children
{
get { return _children; }
}
public TreeNode AddChild(Anagramme value)
{
var node = new TreeNode(value) { Parent = this };
_children.Add(value.letter, node);
return node;
}
public TreeNode[] AddChildren(params Anagramme[] values)
{
return values.Select(AddChild).ToArray();
}
public bool RemoveChild(char c)
{
return _children.Remove(c);
}
public void Traverse(Action<Anagramme> action)
{
action(Value);
foreach (var child in _children)
child.Value.Traverse(action);
}
public IEnumerable<Anagramme> Flatten()
{
return new[] { Value }.Union(_children.Values.SelectMany(x => x.Flatten()));
}
}
}