Beispiel #1
0
        public void Test_PreOrderTraversal_Root_Has_1_Child()
        {
            //Given
            var tree = new BinaryTreeCollection <int>();

            //When
            tree.Add(4);
            tree.Add(2);
            //Then
            Assert.Equal(new[] { 4, 2 }, tree.PreOrderTraversal());
        }
Beispiel #2
0
        public void Test_PostOrderTraversal_Root_Has_1_Right_Child()
        {
            //Given
            var tree = new BinaryTreeCollection <int>();

            //When
            tree.Add(4);
            tree.Add(6);
            //Then
            Assert.Equal(new[] { 6, 4 }, tree.PostOrderTraversal());
        }
Beispiel #3
0
        public void Test_InsertChild_Should_Add_2_Children_To_Root()
        {
            //Given
            var tree = new BinaryTreeCollection <int>();

            //When
            tree.Add(4);
            tree.Add(2);
            tree.Add(5);
            //Then
            Assert.Equal(new[] { 4, 2, 5 }, tree.PreOrderTraversal());
        }
Beispiel #4
0
        public void Test_InOrderTraversal_Root_Has_2_Children()
        {
            //Given
            var tree = new BinaryTreeCollection <int>()
            {
                4
            };

            //When
            tree.Add(2);
            tree.Add(5);
            //Then
            Assert.Equal(new[] { 2, 4, 5 }, tree.InOrderTraversal());
        }
Beispiel #5
0
        public void Test_PreOrderTraversal_Tree_Has_1_Node()
        {
            //Given
            var tree = new BinaryTreeCollection <int>();

            //When
            tree.Add(4);
            //Then
            Assert.Equal(new[] { 4 }, tree.PreOrderTraversal());
        }
Beispiel #6
0
        public void Test_AddMethod_Should_Throw_Exception_When_Value_Is_Already_In_Tree()
        {
            //Given
            var tree = new BinaryTreeCollection <int>(2)
            {
                2,
                0,
                3,
            };
            //When
            var exception = Assert.Throws <InvalidOperationException>(() => tree.Add(2));

            //Then
            Assert.Equal("Tree cannot contain duplicates! ", exception.Message);
        }
Beispiel #7
0
        public void Test_Remove__Element_Is_Leaf_Should_Use_Empty_Spaces_At_Next_Addition()
        {
            //Given
            var tree = new BinaryTreeCollection <int>(3)
            {
                3,
                5,
                6,
                7,
                8,
                9
            };

            //When
            tree.Remove(8);
            tree.Add(8);
            Node <int> foundNode = tree.FindNode(8);

            //Then
            Assert.Equal(new[] { 7, 8, 9 }, foundNode);
        }
Beispiel #8
0
        public void Test_InsertChild_Should_Correctly_Add_Elements_Repeatedely_As_Right_Children()
        {
            //Given
            var tree = new BinaryTreeCollection <int>();

            //When
            tree.Add(1);
            tree.Add(2);
            tree.Add(3);
            tree.Add(4);
            tree.Add(5);
            tree.Add(6);
            //Then
            Assert.Equal(new[] { 1, 2, 3, 4, 5, 6 }, tree.InOrderTraversal());
            Assert.Equal(6, tree.Count);
        }
Beispiel #9
0
        public void Test_InsertChild_Should_Correctly_Add_Elements_Repeatedely_As_Left_Children()
        {
            //Given
            var tree = new BinaryTreeCollection <int>();

            //When
            tree.Add(10);
            tree.Add(9);
            tree.Add(8);
            tree.Add(7);
            tree.Add(6);
            tree.Add(5);
            //Then
            Assert.Equal(new[] { 5, 6, 7, 8, 9, 10 }, tree.InOrderTraversal());
            Assert.Equal(6, tree.Count);
        }