Example #1
0
        public void CustomData_ShouldReturnCorrectTree()
        {
            //Arrange
            IBinarySearchTree <int> tree = new AVLTree <int>(DataComparers.NewByType <int>());

            tree = tree.AddNode(40);
            tree = tree.AddNode(20);
            tree = tree.AddNode(10);
            tree = tree.AddNode(25);
            tree = tree.AddNode(30);
            tree = tree.AddNode(22);
            tree = tree.AddNode(50);

            //Act
            Assert.Equal(25, tree.Value);
            Assert.Equal(3, tree.Height);

            Assert.Equal(20, tree.Left.Value);
            Assert.Equal(2, tree.Left.Height);

            Assert.Equal(10, tree.Left.Left.Value);
            Assert.Equal(1, tree.Left.Left.Height);

            Assert.Equal(22, tree.Left.Right.Value);
            Assert.Equal(1, tree.Left.Right.Height);

            Assert.Equal(40, tree.Right.Value);
            Assert.Equal(2, tree.Right.Height);

            Assert.Equal(30, tree.Right.Left.Value);
            Assert.Equal(1, tree.Right.Left.Height);

            Assert.Equal(50, tree.Right.Right.Value);
            Assert.Equal(1, tree.Right.Right.Height);
        }
Example #2
0
        public void LLWithSubtrees_ShouldRightRotate()
        {
            //Arrange
            IBinarySearchTree <int> tree = new AVLTree <int>(DataComparers.NewByType <int>());

            tree = tree.AddNode(30);
            tree = tree.AddNode(35);
            tree = tree.AddNode(20);
            tree = tree.AddNode(25);
            tree = tree.AddNode(10);
            tree = tree.AddNode(5);


            //Act
            Assert.Equal(20, tree.Value);
            Assert.Equal(3, tree.Height);

            Assert.Equal(10, tree.Left.Value);
            Assert.Equal(2, tree.Left.Height);

            Assert.Equal(5, tree.Left.Left.Value);
            Assert.Equal(1, tree.Left.Left.Height);

            Assert.Equal(30, tree.Right.Value);
            Assert.Equal(2, tree.Right.Height);

            Assert.Equal(25, tree.Right.Left.Value);
            Assert.Equal(1, tree.Right.Left.Height);

            Assert.Equal(35, tree.Right.Right.Value);
            Assert.Equal(1, tree.Right.Right.Height);
        }
        public void InOrder()
        {
            //Arrange
            var tree = new BinarySearchTree <int>(DataComparers.NewByType <int>());

            tree.AddNode(50);
            tree.AddNode(20);
            tree.AddNode(60);
            tree.AddNode(30);
            tree.AddNode(40);
            tree.AddNode(55);


            //Act
            var inorder = new List <int>();

            tree.InOrder((v) => inorder.Add(v.Value));
            Assert.Equal(6, inorder.Count);
            Assert.Equal(20, inorder[0]);
            Assert.Equal(30, inorder[1]);
            Assert.Equal(40, inorder[2]);
            Assert.Equal(50, inorder[3]);
            Assert.Equal(55, inorder[4]);
            Assert.Equal(60, inorder[5]);
        }
        public void SearchExistingElement()
        {
            //Arrange
            var tree = new BinarySearchTree <int>(DataComparers.NewByType <int>());
            var item = tree.AddNode(50);

            item = tree.AddNode(20);
            item = tree.AddNode(60);
            item = tree.AddNode(30);
            item = tree.AddNode(40);
            item = tree.AddNode(55);


            //Act
            var res = tree.Search(50);

            Assert.True(res != null);
            res = tree.Search(20);
            Assert.True(res != null);
            res = tree.Search(60);
            Assert.True(res != null);
            res = tree.Search(30);
            Assert.True(res != null);
            res = tree.Search(40);
            Assert.True(res != null);
            res = tree.Search(55);
            Assert.True(res != null);
        }
        public void OneElement_SearchExistingElement()
        {
            //Arrange
            var tree = new BinarySearchTree <int>(DataComparers.NewByType <int>());

            tree.AddNode(50);

            //Act
            Assert.True(tree.Search(50) != null);
        }
        static void Main(string[] args)
        {
            var tree = new BinarySearchTree <int>(DataComparers.NewByType <int>());

            tree.AddNode(50);
            tree.AddNode(30);
            tree.AddNode(60);
            tree.AddNode(20);
            tree.AddNode(40);
            tree.AddNode(55);

            var newTree = tree.Clone();

            System.Console.ReadLine();
        }
        public void SearchNotExistingElement()
        {
            //Arrange
            var tree = new BinarySearchTree <int>(DataComparers.NewByType <int>());

            tree.AddNode(50);
            tree.AddNode(20);
            tree.AddNode(60);
            tree.AddNode(30);
            tree.AddNode(40);
            tree.AddNode(55);


            //Act
            Assert.True(tree.Search(255) == null);
        }