public void Can_add_one_node()
        {
            // Arrange
            BinarySearchTree <int> testTree = new BinarySearchTree <int>();
            int expected = 5;


            // Act
            testTree.Add(5);

            // Assert
            Assert.Equal(expected, testTree.Root.Value);
        }
Пример #2
0
        public static void Add_MultipleKeys_FormsCorrectBST()
        {
            var tree = new BinarySearchTree <int>();

            tree.Add(5);
            Assert.AreEqual(1, tree.Count);

            tree.Add(3);
            Assert.AreEqual(2, tree.Count);

            tree.Add(4);
            Assert.AreEqual(3, tree.Count);

            tree.Add(2);
            Assert.AreEqual(4, tree.Count);

            var rootNode = tree.Search(5);

            Assert.AreEqual(5, rootNode !.Key);
            Assert.AreEqual(3, rootNode !.Left !.Key);
            Assert.IsNull(rootNode !.Right);

            var threeNode = tree.Search(3);

            Assert.AreEqual(3, threeNode !.Key);
            Assert.AreEqual(2, threeNode !.Left !.Key);
            Assert.AreEqual(4, threeNode !.Right !.Key);

            var twoNode = tree.Search(2);

            Assert.IsNull(twoNode !.Left);
            Assert.IsNull(twoNode !.Right);

            var fourNode = tree.Search(4);

            Assert.IsNull(fourNode !.Left);
            Assert.IsNull(fourNode !.Right);
        }
Пример #3
0
        public void RedViolationTest_RedUncle_2()
        {
            ITree <int> tree = new BinarySearchTree <int>();

            tree.Add(5);
            tree.Add(3);
            tree.Add(6);

            IBinarySearchTreeNode <int> rootNode = (IBinarySearchTreeNode <int>)tree.Root;

            Assert.True(rootNode.IsBlack);
            Assert.False(rootNode.Left.IsBlack);
            Assert.False(rootNode.Right.IsBlack);

            tree.Add(4);

            Assert.Equal(4, tree.Count);

            Assert.Equal(5, rootNode.Value);
            Assert.Equal(3, rootNode.Left.Value);
            Assert.Equal(6, rootNode.Right.Value);
            Assert.Equal(4, rootNode.Left.Right.Value);

            Assert.Null(rootNode.Left.Left);
            Assert.Null(rootNode.Left.Right.Left);
            Assert.Null(rootNode.Left.Right.Right);
            Assert.Null(rootNode.Right.Left);
            Assert.Null(rootNode.Right.Right);

            Assert.Same(rootNode, rootNode.Left.Parent);
            Assert.Same(rootNode, rootNode.Right.Parent);
            Assert.Same(rootNode.Left, rootNode.Left.Right.Parent);

            Assert.True(rootNode.IsBlack);
            Assert.True(rootNode.Left.IsBlack);
            Assert.True(rootNode.Right.IsBlack);
            Assert.False(rootNode.Left.Right.IsBlack);
        }
Пример #4
0
        public void AddFirstNodeTest()
        {
            ITree <int> tree = new BinarySearchTree <int>();

            tree.Add(1);

            Assert.Equal(1, tree.Count);
            Assert.Equal(1, tree.Root.Value);

            IBinarySearchTreeNode <int> rootNode = (IBinarySearchTreeNode <int>)tree.Root;

            Assert.True(rootNode.IsBlack);

            Assert.Null(rootNode.Left);
            Assert.Null(rootNode.Right);
        }
Пример #5
0
        public static void Remove_RemoveRoot_CorrectlyRemovesRoot()
        {
            var tree = new BinarySearchTree <int>();

            tree.Add(5);
            tree.Remove(5);

            Assert.AreEqual(0, tree.Count);
            Assert.IsNull(tree.Search(5));

            tree.AddRange(new List <int> {
                5, 4, 6
            });
            tree.Remove(5);

            Assert.AreEqual(2, tree.Count);
            Assert.IsNull(tree.Search(5));
            Assert.IsNotNull(tree.Search(4));
            Assert.IsNotNull(tree.Search(6));
            Assert.AreEqual(6, tree.Search(4) !.Right !.Key);
        }
        public void Remove_ForNotEmptyTree_Works()
        {
            //arrange
            var bst = new BinarySearchTree <int>();

            bst.Add(7);
            bst.Add(2);
            bst.Add(12);
            bst.Add(11);
            bst.Add(1);
            bst.Add(6);
            //act
            bst.Remove(7);
            //assert
            bst.RootNode.Should().NotBeNull();
            bst.RootNode.Left.Should().NotBeNull();
            bst.RootNode.Right.Should().NotBeNull();
            bst.NodesCount.Should().Be(5);
        }
        public void Can_preorder_big_traverse()
        {
            // Arrange
            BinarySearchTree <int> testTree = new BinarySearchTree <int>();

            testTree.Add(5);
            testTree.Add(9);
            testTree.Add(1);
            testTree.Add(8);
            testTree.Add(12);
            testTree.Add(2);
            IEnumerable <int> expected = new int[] { 5, 1, 2, 9, 8, 12 };


            // Act
            IEnumerable <int> actual = testTree.PreOrder(testTree.ReturnRoot());

            // Assert
            Assert.Equal(expected, actual);
        }