Esempio n. 1
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);
        }
Esempio n. 2
0
        static void TreeTestSuite()
        {
            MyAVLTree <int> testtree = new MyAVLTree <int>();

            testtree.insert(10);
            testtree.insert(1010);
            testtree.insert(50);
            testtree.insert(5);
            testtree.insert(7);
            testtree.insert(0);
            testtree.insert(2);
            testtree.insert(3);
            testtree.insert(5);
            testtree.insert(6);
            Console.WriteLine("Is 6 in tree? {0}", (testtree.search(6) != null));

            Console.WriteLine("Is 5 in tree? {0}", (testtree.search(5) != null));

            Console.WriteLine("Is 17 in tree? {0}", (testtree.search(17) != null));

            Console.WriteLine("height of tree:  {0}", MyBST <int> .getHeight(testtree.head));
            Console.WriteLine("height of right subtree:  {0}", MyBST <int> .getHeight(testtree.head.right));
            Console.WriteLine("height of left subtree:  {0}", MyBST <int> .getHeight(testtree.head.left));

            Console.WriteLine("Performing rebalance.");
            testtree.reBalance();

            Console.WriteLine("height of tree:  {0}", MyBST <int> .getHeight(testtree.head));

            Console.WriteLine("height of right subtree:  {0}", MyBST <int> .getHeight(testtree.head.right));
            Console.WriteLine("height of left subtree:  {0}", MyBST <int> .getHeight(testtree.head.left));
        }
Esempio n. 3
0
        public void Should_Insert_Right_Right_Complex_Example()
        {
            //arrange
            var tree = new MyAVLTree <int>();

            //act
            tree.Insert(30);
            tree.Insert(5);
            tree.Insert(35);
            tree.Insert(32);
            tree.Insert(40);
            tree.Insert(45);

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

            tree.Root.Data.ShouldBeEquivalentTo(35);
            tree.Root.TreeHeight.ShouldBeEquivalentTo(3);

            tree.Root.Left.Data.ShouldBeEquivalentTo(30);
            tree.Root.Left.TreeHeight.ShouldBeEquivalentTo(2);
            tree.Root.Right.Data.ShouldBeEquivalentTo(40);
            tree.Root.Right.TreeHeight.ShouldBeEquivalentTo(2);

            tree.Root.Left.Left.Data.ShouldBeEquivalentTo(5);
            tree.Root.Left.Left.TreeHeight.ShouldBeEquivalentTo(1);
            tree.Root.Left.Right.Data.ShouldBeEquivalentTo(32);
            tree.Root.Left.Right.TreeHeight.ShouldBeEquivalentTo(1);
            tree.Root.Right.Right.Data.ShouldBeEquivalentTo(45);
            tree.Root.Right.Right.TreeHeight.ShouldBeEquivalentTo(1);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public void Should_Insert()
        {
            //arrange
            var tree = new MyAVLTree <int>();

            //act
            tree.Insert(1);

            //assert
            tree.Count.ShouldBeEquivalentTo(1);
        }
Esempio n. 6
0
        public void GivenAnEmptyTree_ShouldReturnNegativeOne()
        {
            // Arrange
            var tree = new MyAVLTree();

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

            // Assert
            height.Should().Be(-1);
        }
        public void GivenATree_WhenIsEmpty_ShouldBeBalanced()
        {
            // Arrange
            var tree = new MyAVLTree();

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

            // Assert
            balance.Should().Be(MyAVLTree.Balance.Balanced);
        }
Esempio n. 8
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);
        }
Esempio n. 9
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);
        }
Esempio n. 10
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);
        }
Esempio n. 11
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);
        }
Esempio n. 12
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();
        }
Esempio n. 13
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);
        }
Esempio n. 14
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);
        }
Esempio n. 15
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);
        }
Esempio n. 16
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);
        }
Esempio n. 17
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);
        }