public void Handle_TreeHasOnlyRoot_ShouldReturnNull()
        {
            // Arrange
            var handler = HandlerFabric.CreateRemoveHandler();
            var root    = NodeFabric.CreateNode(3);

            // Act
            var actual = handler.Handle(root, 3);

            // Assert
            Assert.IsNull(actual);
        }
        public void Handle_NodeToDeleteIsLeaf_ShouldRemoveOnlyLeaf()
        {
            // Arrange
            var handler = HandlerFabric.CreateRemoveHandler();
            var root    = NodeFabric.CreateNode(10);

            root.LeftNode = NodeFabric.CreateNode(3);

            // Act
            var actual = handler.Handle(root, 3);

            // Assert
            Assert.IsNull(actual.LeftNode);
        }
        public void Handle_InsertedValueMoreThenRoot_ShoudInsertIntoRightNode()
        {
            // Arrange
            var handler = HandlerFabric.CreateInsertHandler();

            var root = NodeFabric.CreateNode(10);

            // Act
            var actual = handler.Handle(root, 15, new object());

            // Assert
            Assert.IsNotNull(actual.RightNode);
            Assert.AreEqual(15, actual.RightNode.Key);
        }
        public void Handle_NodeToDeleteHasOneChild_ShouldReplaceNodeToDeleteByChild()
        {
            // Arrange
            var handler = HandlerFabric.CreateRemoveHandler();
            var root    = NodeFabric.CreateNode(10);

            root.LeftNode           = NodeFabric.CreateNode(3);
            root.LeftNode.RightNode = NodeFabric.CreateNode(5);

            // Act
            var actual = handler.Handle(root, 3);

            // Assert
            Assert.AreEqual(5, actual.LeftNode.Key);
            Assert.IsNull(actual.LeftNode.RightNode);
        }
        public void Handle_SearchKeyDoesNotExist_ShouldReturnNull()
        {
            // Arrange
            var handler = HandlerFabric.CreateSearchHandler();

            var root = NodeFabric.CreateNode(10);

            root.LeftNode           = NodeFabric.CreateNode(5);
            root.RightNode          = NodeFabric.CreateNode(15);
            root.RightNode.LeftNode = NodeFabric.CreateNode(12);

            // Act
            var actual = handler.Handle(root, 3);

            // Assert
            Assert.IsNull(actual);
        }
        public void Handle_SearchKeyExists_ShouldReturnNodeWithTheSameKey()
        {
            // Arrange
            var handler = HandlerFabric.CreateSearchHandler();

            var root = NodeFabric.CreateNode(10);

            root.LeftNode           = NodeFabric.CreateNode(5);
            root.RightNode          = NodeFabric.CreateNode(15);
            root.RightNode.LeftNode = NodeFabric.CreateNode(12);

            // Act
            var actual = handler.Handle(root, 12);

            // Assert
            Assert.AreEqual(root.RightNode.LeftNode, actual);
        }
        public void Handle_NodeToDeleteHasTwoLeaves_ShouldReplaceNodeToDeleteByRightNode()
        {
            // Arrange
            var handler = HandlerFabric.CreateRemoveHandler();
            var root    = NodeFabric.CreateNode(5);

            root.LeftNode  = NodeFabric.CreateNode(4);
            root.RightNode = NodeFabric.CreateNode(6);

            // Act
            var actual = handler.Handle(root, 5);

            // Assert
            Assert.AreEqual(6, actual.Key);
            Assert.AreEqual(4, actual.LeftNode.Key);
            Assert.IsNull(actual.RightNode);
        }
        public void Handle_TreeHasSomeNodes_ShouldInsertIntoCorrectNode()
        {
            // Arrange
            var handler = HandlerFabric.CreateInsertHandler();

            var root = NodeFabric.CreateNode(10);

            root.LeftNode           = NodeFabric.CreateNode(5);
            root.LeftNode.RightNode = NodeFabric.CreateNode(7);

            // Act
            var actual = handler.Handle(root, 6, new object());

            // Assert
            var inserted = actual.LeftNode.RightNode.LeftNode;

            Assert.IsNotNull(inserted);
            Assert.AreEqual(6, inserted.Key);
        }
        public void Handle_NodeToDeleteHasTwoChildren_ShouldReplaceNodeToDeleteBySmallestNodeInRightSubtree()
        {
            // Arrange
            var handler = HandlerFabric.CreateRemoveHandler();
            var root    = NodeFabric.CreateNode(5);

            root.LeftNode            = NodeFabric.CreateNode(4);
            root.RightNode           = NodeFabric.CreateNode(7);
            root.RightNode.LeftNode  = NodeFabric.CreateNode(6);
            root.RightNode.RightNode = NodeFabric.CreateNode(8);

            // Act
            var actual = handler.Handle(root, 5);

            // Assert
            Assert.AreEqual(6, actual.Key);
            Assert.AreEqual(7, actual.RightNode.Key);
            Assert.AreEqual(4, actual.LeftNode.Key);
            Assert.IsNull(actual.RightNode.LeftNode);
        }