Skip to main content
Fixed code block
Source Link
Stacklysm
  • 213
  • 1
  • 2
  • 8

This code is now maintained on GitHub

This code is now maintained on GitHub

Corrections
Source Link
Stacklysm
  • 213
  • 1
  • 2
  • 8

Also, I'm including some testtests I've made, all of them are passing as of now.

Also, I'm including some test I've made, all of them are passing as of now.

Also, I'm including some tests I've made, all of them are passing as of now.

Became Hot Network Question
Tweeted twitter.com/StackCodeReview/status/1153454813549670400
Added GitHub link
Source Link
Stacklysm
  • 213
  • 1
  • 2
  • 8

This code is now maintained on GitHub

[TestClass]
public class NodeTest
{
    [TestMethod]
    public void Node_DeepCopy_CopySuccessful()
    {
        // Arrange
        var root = new Node(null);

        var node1 = new Node(null);
        var node2 = new Node(null);

        var copyNode = new Node(null);

        // Act
        root.AddChild(node1);
        root.AddChild(node2);

        copyNode = root.DeepCopy();
        var actual = copyNode.HasChildren;

        // Assert
        Assert.AreEqual(true, actual);
    }

    [TestMethod]
    public void Node_DeepCopy_CopyIsIndependent()
    {
        // Arrange
        var root = new Node(null);

        var node1 = new Node(null);
        var node2 = new Node(null);

        var copyNode = new Node(null);

        // Act
        root.AddChild(node1);
        root.AddChild(node2);

        copyNode = root.DeepCopy();
        root.AddChild(new Node(null));

        var actual = root.Count != copyNode.Count;

        // Assert
        Assert.AreEqual(true, actual);
    }

    [TestMethod]
    public void Node_Search_ReturnsAllElements()
    {
        // Arrange
        const int EXPECTED_CHILDREN_COUNT = 3;

        var root = new Node(null);

        var root_child1 = new Node(null);
        var root_child2 = new Node(null);
        var root_child3 = new Node(null);

        // Act
        root.AddChild(root_child1);
        root.AddChild(root_child2);
        root.AddChild(root_child3);

        int actual = root.Count;

        // Assert
        Assert.AreEqual(EXPECTED_CHILDREN_COUNT, actual);
    }

    [TestMethod]
    public void Node_RecursiveSearch_ReturnsAllElements()
    {
        // Arrange
        const int EXPECTED_CHILDREN_COUNT = 9;

        var root = new Node("Root node");

        var rc1 = new Node("[Gen 1] 1st child of: root");
        var rc2 = new Node("[Gen 1] 2nd child of: root");
        var rc3 = new Node("[Gen 1] 3rd child of: root");

        var rc2_1 = new Node("[Gen 2] 1st child of: root's 2nd child");
        var rc2_2 = new Node("[Gen 2] 2nd child of: root's 2nd child");
        var rc3_1 = new Node("[Gen 2] 1st child of: root's 3rd child");

        var rc2_1_1 = new Node("[Gen 3] 1st child of: root's 2nd child's 1st child");
        var rc3_1_1 = new Node("[Gen 3] 1st child of: root's 3rd child's 1st child");

        var rc3_1_1_1 = new Node("[Gen 4] 1st child of: root's 3rd child's 1st child's 1st child");

        // Act
        rc2_1.AddChild(rc2_1_1);
        rc2.AddChild(rc2_1);
        rc2.AddChild(rc2_2);

        rc3_1_1.AddChild(rc3_1_1_1);
        rc3_1.AddChild(rc3_1_1);
        rc3.AddChild(rc3_1);

        root.AddChild(rc1);
        root.AddChild(rc2);
        root.AddChild(rc3);

        int actual = new List<Node>(root.GetChildrenRecursive()).Count;

        // Assert
        Assert.AreEqual(EXPECTED_CHILDREN_COUNT, actual);
    }
}
```
[TestClass]
public class NodeTest
{
    [TestMethod]
    public void Node_DeepCopy_CopySuccessful()
    {
        // Arrange
        var root = new Node(null);

        var node1 = new Node(null);
        var node2 = new Node(null);

        var copyNode = new Node(null);

        // Act
        root.AddChild(node1);
        root.AddChild(node2);

        copyNode = root.DeepCopy();
        var actual = copyNode.HasChildren;

        // Assert
        Assert.AreEqual(true, actual);
    }

    [TestMethod]
    public void Node_DeepCopy_CopyIsIndependent()
    {
        // Arrange
        var root = new Node(null);

        var node1 = new Node(null);
        var node2 = new Node(null);

        var copyNode = new Node(null);

        // Act
        root.AddChild(node1);
        root.AddChild(node2);

        copyNode = root.DeepCopy();
        root.AddChild(new Node(null));

        var actual = root.Count != copyNode.Count;

        // Assert
        Assert.AreEqual(true, actual);
    }

    [TestMethod]
    public void Node_Search_ReturnsAllElements()
    {
        // Arrange
        const int EXPECTED_CHILDREN_COUNT = 3;

        var root = new Node(null);

        var root_child1 = new Node(null);
        var root_child2 = new Node(null);
        var root_child3 = new Node(null);

        // Act
        root.AddChild(root_child1);
        root.AddChild(root_child2);
        root.AddChild(root_child3);

        int actual = root.Count;

        // Assert
        Assert.AreEqual(EXPECTED_CHILDREN_COUNT, actual);
    }

    [TestMethod]
    public void Node_RecursiveSearch_ReturnsAllElements()
    {
        // Arrange
        const int EXPECTED_CHILDREN_COUNT = 9;

        var root = new Node("Root node");

        var rc1 = new Node("[Gen 1] 1st child of: root");
        var rc2 = new Node("[Gen 1] 2nd child of: root");
        var rc3 = new Node("[Gen 1] 3rd child of: root");

        var rc2_1 = new Node("[Gen 2] 1st child of: root's 2nd child");
        var rc2_2 = new Node("[Gen 2] 2nd child of: root's 2nd child");
        var rc3_1 = new Node("[Gen 2] 1st child of: root's 3rd child");

        var rc2_1_1 = new Node("[Gen 3] 1st child of: root's 2nd child's 1st child");
        var rc3_1_1 = new Node("[Gen 3] 1st child of: root's 3rd child's 1st child");

        var rc3_1_1_1 = new Node("[Gen 4] 1st child of: root's 3rd child's 1st child's 1st child");

        // Act
        rc2_1.AddChild(rc2_1_1);
        rc2.AddChild(rc2_1);
        rc2.AddChild(rc2_2);

        rc3_1_1.AddChild(rc3_1_1_1);
        rc3_1.AddChild(rc3_1_1);
        rc3.AddChild(rc3_1);

        root.AddChild(rc1);
        root.AddChild(rc2);
        root.AddChild(rc3);

        int actual = new List<Node>(root.GetChildrenRecursive()).Count;

        // Assert
        Assert.AreEqual(EXPECTED_CHILDREN_COUNT, actual);
    }
}
```

This code is now maintained on GitHub

[TestClass]
public class NodeTest
{
    [TestMethod]
    public void Node_DeepCopy_CopySuccessful()
    {
        // Arrange
        var root = new Node(null);

        var node1 = new Node(null);
        var node2 = new Node(null);

        var copyNode = new Node(null);

        // Act
        root.AddChild(node1);
        root.AddChild(node2);

        copyNode = root.DeepCopy();
        var actual = copyNode.HasChildren;

        // Assert
        Assert.AreEqual(true, actual);
    }

    [TestMethod]
    public void Node_DeepCopy_CopyIsIndependent()
    {
        // Arrange
        var root = new Node(null);

        var node1 = new Node(null);
        var node2 = new Node(null);

        var copyNode = new Node(null);

        // Act
        root.AddChild(node1);
        root.AddChild(node2);

        copyNode = root.DeepCopy();
        root.AddChild(new Node(null));

        var actual = root.Count != copyNode.Count;

        // Assert
        Assert.AreEqual(true, actual);
    }

    [TestMethod]
    public void Node_Search_ReturnsAllElements()
    {
        // Arrange
        const int EXPECTED_CHILDREN_COUNT = 3;

        var root = new Node(null);

        var root_child1 = new Node(null);
        var root_child2 = new Node(null);
        var root_child3 = new Node(null);

        // Act
        root.AddChild(root_child1);
        root.AddChild(root_child2);
        root.AddChild(root_child3);

        int actual = root.Count;

        // Assert
        Assert.AreEqual(EXPECTED_CHILDREN_COUNT, actual);
    }

    [TestMethod]
    public void Node_RecursiveSearch_ReturnsAllElements()
    {
        // Arrange
        const int EXPECTED_CHILDREN_COUNT = 9;

        var root = new Node("Root node");

        var rc1 = new Node("[Gen 1] 1st child of: root");
        var rc2 = new Node("[Gen 1] 2nd child of: root");
        var rc3 = new Node("[Gen 1] 3rd child of: root");

        var rc2_1 = new Node("[Gen 2] 1st child of: root's 2nd child");
        var rc2_2 = new Node("[Gen 2] 2nd child of: root's 2nd child");
        var rc3_1 = new Node("[Gen 2] 1st child of: root's 3rd child");

        var rc2_1_1 = new Node("[Gen 3] 1st child of: root's 2nd child's 1st child");
        var rc3_1_1 = new Node("[Gen 3] 1st child of: root's 3rd child's 1st child");

        var rc3_1_1_1 = new Node("[Gen 4] 1st child of: root's 3rd child's 1st child's 1st child");

        // Act
        rc2_1.AddChild(rc2_1_1);
        rc2.AddChild(rc2_1);
        rc2.AddChild(rc2_2);

        rc3_1_1.AddChild(rc3_1_1_1);
        rc3_1.AddChild(rc3_1_1);
        rc3.AddChild(rc3_1);

        root.AddChild(rc1);
        root.AddChild(rc2);
        root.AddChild(rc3);

        int actual = new List<Node>(root.GetChildrenRecursive()).Count;

        // Assert
        Assert.AreEqual(EXPECTED_CHILDREN_COUNT, actual);
    }
}
removed language tag from the title
Link
t3chb0t
  • 44.3k
  • 9
  • 82
  • 189
Loading
tag
Link
dfhwze
  • 13.9k
  • 3
  • 38
  • 100
Loading
Removed the "Reinventing the wheel", since there are no generic implementations of this class
Link
Stacklysm
  • 213
  • 1
  • 2
  • 8
Loading
Added tests
Source Link
Stacklysm
  • 213
  • 1
  • 2
  • 8
Loading
added 75 characters in body
Source Link
Stacklysm
  • 213
  • 1
  • 2
  • 8
Loading
Source Link
Stacklysm
  • 213
  • 1
  • 2
  • 8
Loading