Skip to content

Commit 5df7cfe

Browse files
authored
Merge pull request #46 from Valdas3/narytree
#36: implement n-ary tree
2 parents 849f255 + faab414 commit 5df7cfe

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using System;
2+
using Algorithms.NTree;
3+
using Xunit;
4+
5+
namespace Algorithms.Tests.NTreeTests
6+
{
7+
public class NTreeTests
8+
{
9+
NTree<int> BuildTree()
10+
{
11+
var tree = new NTree<int>(0);
12+
tree.Add(1).Add(2).Add(3);
13+
tree.Children[0].Add(4).Add(5);
14+
tree.Children[0].Children[0].Add(6);
15+
tree.Children[1].Add(7);
16+
tree.Children[2].Add(8);
17+
tree.Children[2].Children[0].Add(9);
18+
return tree;
19+
}
20+
21+
[Fact]
22+
public void ElementsAreInsertedInCorrectOrder()
23+
{
24+
var tree = BuildTree();
25+
26+
var result = tree.ToString();
27+
28+
Assert.Equal("0146527389", result);
29+
}
30+
31+
[Fact]
32+
public void ParentReturnsCorrectNode()
33+
{
34+
var tree = BuildTree();
35+
var child = tree.Children[1].Children[0];
36+
var parent = tree.Children[1];
37+
38+
var result = child.Parent;
39+
40+
Assert.Equal(parent.Value, result.Value);
41+
}
42+
43+
[Fact]
44+
public void ParentOfRootIsNull()
45+
{
46+
var tree = BuildTree();
47+
48+
Assert.Null(tree.Parent);
49+
}
50+
51+
[Fact]
52+
public void FindReturnsCorrectNode()
53+
{
54+
var tree = BuildTree();
55+
var expected = tree.Children[0].Children[0];
56+
57+
var result = tree.Find(4);
58+
59+
Assert.Same(expected, result);
60+
}
61+
62+
[Fact]
63+
public void FindReturnsNullWhenValueDoesNotExist()
64+
{
65+
var tree = BuildTree();
66+
67+
var result = tree.Find(-14);
68+
69+
Assert.Null(result);
70+
}
71+
72+
[Theory]
73+
[InlineData(1, "027389")]
74+
[InlineData(2, "01465389")]
75+
[InlineData(9, "014652738")]
76+
public void RemoveWorks(int node, string expected)
77+
{
78+
var tree = BuildTree();
79+
var nodeToRemove = tree.Find(node);
80+
81+
nodeToRemove.Remove();
82+
83+
Assert.Equal(expected, tree.ToString());
84+
}
85+
86+
[Fact]
87+
public void ThrowsExceptionWhenRemovingRoot()
88+
{
89+
var tree = BuildTree();
90+
Action removeRoot = () => tree.Remove();
91+
92+
Assert.Throws<ArgumentException>(removeRoot);
93+
}
94+
}
95+
}

Algorithms/Algorithms/NTree/NTree.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace Algorithms.NTree
6+
{
7+
public class NTree<T> where T : IEquatable<T>
8+
{
9+
public T Value { get; set; }
10+
public NTree<T> Parent { get; set; }
11+
public List<NTree<T>> Children { get; set; } = new List<NTree<T>>();
12+
13+
public NTree(T value)
14+
{
15+
Value = value;
16+
}
17+
18+
public NTree<T> Add(T value)
19+
{
20+
var child = new NTree<T>(value)
21+
{
22+
Parent = this
23+
};
24+
Children.Add(child);
25+
return this;
26+
}
27+
28+
public NTree<T> Find(T value)
29+
{
30+
if (Value.Equals(value))
31+
{
32+
return this;
33+
}
34+
35+
return Children.Select(x => x.Find(value)).FirstOrDefault(x => x != null);
36+
}
37+
38+
public void Remove()
39+
{
40+
if (Parent == null)
41+
{
42+
throw new ArgumentException("Can't remove root");
43+
}
44+
45+
var index = Parent.Children.FindIndex(x => x.Value.Equals(Value));
46+
Parent.Children.RemoveAt(index);
47+
}
48+
49+
// returns concatenated values in prefix order
50+
public override string ToString()
51+
{
52+
return Value + string.Concat(Children.Select(x => x.ToString()));
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)