Пример #1
0
        public void Should_Find()
        {
            //arrange
            var tree = new MyAVLTree <int>();

            tree.Insert(5);
            tree.Insert(2);
            tree.Insert(7);
            tree.Insert(1);
            tree.Insert(4);
            tree.Insert(6);
            tree.Insert(15);
            tree.Insert(3);
            tree.Insert(9);
            tree.Insert(16);

            //act
            var result = tree.Find(15);

            //assert
            tree.Count.ShouldBeEquivalentTo(10);

            result.Data.ShouldBeEquivalentTo(15);
            result.TreeHeight.ShouldBeEquivalentTo(2);

            result.Left.Data.ShouldBeEquivalentTo(9);
            result.Left.TreeHeight.ShouldBeEquivalentTo(1);
            result.Right.Data.ShouldBeEquivalentTo(16);
            result.Right.TreeHeight.ShouldBeEquivalentTo(1);
        }
Пример #2
0
        public void Should_Insert_Right_Left_Example()
        {
            //arrange
            var tree = new MyAVLTree <int>();

            //act
            tree.Insert(5);
            tree.Insert(10);
            tree.Insert(20);
            tree.Insert(40);
            tree.Insert(30);

            //assert
            tree.Count.ShouldBeEquivalentTo(5);

            tree.Root.Data.ShouldBeEquivalentTo(10);
            tree.Root.TreeHeight.ShouldBeEquivalentTo(3);
            tree.Root.Left.Data.ShouldBeEquivalentTo(5);
            tree.Root.Left.TreeHeight.ShouldBeEquivalentTo(1);
            tree.Root.Right.Data.ShouldBeEquivalentTo(30);
            tree.Root.Right.TreeHeight.ShouldBeEquivalentTo(2);
            tree.Root.Right.Right.Data.ShouldBeEquivalentTo(40);
            tree.Root.Right.Right.TreeHeight.ShouldBeEquivalentTo(1);
            tree.Root.Right.Left.Data.ShouldBeEquivalentTo(20);
            tree.Root.Right.Left.TreeHeight.ShouldBeEquivalentTo(1);
        }
Пример #3
0
        public void Should_Insert_Check_Example()
        {
            //arrange
            var tree = new MyAVLTree <int>();

            //act
            tree.Insert(1);
            tree.Insert(2);
            tree.Insert(3);
            tree.Insert(6);
            tree.Insert(5);
            tree.Insert(-2);
            tree.Insert(-5);
            tree.Insert(-8);

            //assert
            tree.Count.ShouldBeEquivalentTo(8);

            tree.Root.Data.ShouldBeEquivalentTo(2);
            tree.Root.Left.Data.ShouldBeEquivalentTo(-2);
            tree.Root.Right.Data.ShouldBeEquivalentTo(5);
            tree.Root.Left.Left.Data.ShouldBeEquivalentTo(-5);
            tree.Root.Left.Right.Data.ShouldBeEquivalentTo(1);
            tree.Root.Right.Left.Data.ShouldBeEquivalentTo(3);
            tree.Root.Right.Right.Data.ShouldBeEquivalentTo(6);
            tree.Root.Left.Left.Left.Data.ShouldBeEquivalentTo(-8);
        }
Пример #4
0
        public void GivenFourValues_WhenTreeIsEmpty_ShouldAddThisValuesOnTree()
        {
            // Arrange
            var tree = new MyAVLTree();

            // Act
            tree.Insert(20);
            tree.Insert(10);
            tree.Insert(30);
            tree.Insert(21);

            // Assert
            tree.Contains(20).Should().BeTrue();
            tree.Contains(10).Should().BeTrue();
            tree.Contains(30).Should().BeTrue();
            tree.Contains(21).Should().BeTrue();
        }
Пример #5
0
        public void Should_Remove_With_Right_Right_Rebalance()
        {
            //arrange
            var tree = new MyAVLTree <int>();

            tree.Insert(5);
            tree.Insert(2);
            tree.Insert(7);
            tree.Insert(1);
            tree.Insert(4);
            tree.Insert(6);
            tree.Insert(15);
            tree.Insert(3);
            tree.Insert(9);
            tree.Insert(16);

            //act
            tree.Remove(9);
            tree.Remove(6);

            //assert
            tree.Count.ShouldBeEquivalentTo(8);

            tree.Root.Data.ShouldBeEquivalentTo(5);
            tree.Root.TreeHeight.ShouldBeEquivalentTo(4);

            tree.Root.Left.Data.ShouldBeEquivalentTo(2);
            tree.Root.Left.TreeHeight.ShouldBeEquivalentTo(3);
            tree.Root.Right.Data.ShouldBeEquivalentTo(15);
            tree.Root.Right.TreeHeight.ShouldBeEquivalentTo(2);

            tree.Root.Left.Left.Data.ShouldBeEquivalentTo(1);
            tree.Root.Left.Left.TreeHeight.ShouldBeEquivalentTo(1);
            tree.Root.Left.Right.Data.ShouldBeEquivalentTo(4);
            tree.Root.Left.Right.TreeHeight.ShouldBeEquivalentTo(2);
            tree.Root.Right.Left.Data.ShouldBeEquivalentTo(7);
            tree.Root.Right.Left.TreeHeight.ShouldBeEquivalentTo(1);
            tree.Root.Right.Right.Data.ShouldBeEquivalentTo(16);
            tree.Root.Right.Right.TreeHeight.ShouldBeEquivalentTo(1);

            tree.Root.Left.Right.Left.Data.ShouldBeEquivalentTo(3);
            tree.Root.Left.Right.Left.TreeHeight.ShouldBeEquivalentTo(1);
        }
Пример #6
0
        public void Should_Insert()
        {
            //arrange
            var tree = new MyAVLTree <int>();

            //act
            tree.Insert(1);

            //assert
            tree.Count.ShouldBeEquivalentTo(1);
        }
Пример #7
0
        public void Should_Rotate_Right_Left()
        {
            //arrange
            var tree = new MyAVLTree <int>();

            //act
            tree.Insert(10);
            tree.Insert(5);
            tree.Insert(20);
            tree.Insert(40);
            tree.Insert(30);

            //assert
            tree.Count.ShouldBeEquivalentTo(5);

            tree.Root.Data.ShouldBeEquivalentTo(10);
            tree.Root.Left.Data.ShouldBeEquivalentTo(5);
            tree.Root.Right.Data.ShouldBeEquivalentTo(30);
            tree.Root.Right.Left.Data.ShouldBeEquivalentTo(20);
            tree.Root.Right.Right.Data.ShouldBeEquivalentTo(40);
        }
Пример #8
0
        public void Should_Find_False()
        {
            //arrange
            var tree = new MyAVLTree <int>();

            tree.Insert(5);
            tree.Insert(2);
            tree.Insert(7);
            tree.Insert(1);
            tree.Insert(4);
            tree.Insert(6);
            tree.Insert(15);
            tree.Insert(3);
            tree.Insert(9);
            tree.Insert(16);

            //act
            var result = tree.Find(0);

            //assert
            result.ShouldBeEquivalentTo(null);
        }
Пример #9
0
        public void GivenABalanceTree_WhenHaveFourItems_ShouldTheTopNodeBeThree()
        {
            // Arrange
            var tree = new MyAVLTree();

            tree.Insert(20, 30, 10, 11);

            // Act
            var height = tree.GetHeightOfValue(20);

            // Assert
            height.Should().Be(3);
        }
Пример #10
0
        public void GivenATree_WhenIsRightHeavy_ShouldRebalanceToLeft()
        {
            // Arrange
            var tree = new MyAVLTree();

            tree.Insert(10, 20, 30, 40);

            // Act
            var items = tree.GetAll();

            // Assert
            items.Should().ContainInOrder(20, 10, 30, 40);
        }
Пример #11
0
        public void GivenATree_WhenInsertThreeItemInAscendingOrder_ShouldSelfRebalance()
        {
            // Arrange
            var tree = new MyAVLTree();

            tree.Insert(10, 20, 30);

            // Act
            var balance = tree.GetBalance();

            // Assert
            balance.Should().Be(MyAVLTree.Balance.Balanced);
        }
Пример #12
0
        public void GivenATree_WhenInsertThreeItemInBalanceOrder_ShouldBeBalanced()
        {
            // Arrange
            var tree = new MyAVLTree();

            tree.Insert(20, 10, 30);

            // Act
            var balance = tree.GetBalance();

            // Assert
            balance.Should().Be(MyAVLTree.Balance.Balanced);
        }
Пример #13
0
        public void Should_Insert_Left_Left_Complex_Example()
        {
            //arrange
            var tree = new MyAVLTree <int>();

            tree.Insert(13);
            tree.Insert(10);
            tree.Insert(15);
            tree.Insert(5);
            tree.Insert(11);
            tree.Insert(16);
            tree.Insert(4);
            tree.Insert(8);

            //act
            tree.Insert(3);

            //assert
            tree.Count.ShouldBeEquivalentTo(9);
        }
Пример #14
0
        public void Should_Check_Remove_Example()
        {
            //arrange
            var tree = new MyAVLTree <int>();

            tree.Insert(44);
            tree.Insert(17);
            tree.Insert(62);
            tree.Insert(32);
            tree.Insert(50);
            tree.Insert(78);
            tree.Insert(48);
            tree.Insert(54);
            tree.Insert(88);

            //act
            tree.Remove(32);

            //assert
            tree.Count.ShouldBeEquivalentTo(8);

            tree.Root.Data.ShouldBeEquivalentTo(62);
            tree.Root.TreeHeight.ShouldBeEquivalentTo(4);

            tree.Root.Left.Data.ShouldBeEquivalentTo(44);
            tree.Root.Left.TreeHeight.ShouldBeEquivalentTo(3);
            tree.Root.Right.Data.ShouldBeEquivalentTo(78);
            tree.Root.Right.TreeHeight.ShouldBeEquivalentTo(2);

            tree.Root.Left.Left.Data.ShouldBeEquivalentTo(17);
            tree.Root.Left.Left.TreeHeight.ShouldBeEquivalentTo(1);
            tree.Root.Left.Right.Data.ShouldBeEquivalentTo(50);
            tree.Root.Left.Right.TreeHeight.ShouldBeEquivalentTo(2);
            tree.Root.Right.Right.Data.ShouldBeEquivalentTo(88);
            tree.Root.Right.Right.TreeHeight.ShouldBeEquivalentTo(1);

            tree.Root.Left.Right.Left.Data.ShouldBeEquivalentTo(48);
            tree.Root.Left.Right.Left.TreeHeight.ShouldBeEquivalentTo(1);
            tree.Root.Left.Right.Right.Data.ShouldBeEquivalentTo(54);
            tree.Root.Left.Right.Right.TreeHeight.ShouldBeEquivalentTo(1);
        }