public void GivenZillowExampleWhenDeleteRootExpectMiddleReplacement() { // arrange // 5 // / | \ // 4 5 9 // / / // 2 7 // | // 2 TrinaryTree startTree = this.GetBasicTree(); // create expected tree // 5 // / \ // 4 9 // / / // 2 7 // | // 2 var expectedTree = new TrinaryTree(5); expectedTree.RootNode.Left = new TrinaryTree.TrinaryTreeNode(4); expectedTree.RootNode.Left.Left = new TrinaryTree.TrinaryTreeNode(2); expectedTree.RootNode.Left.Left.Middle = new TrinaryTree.TrinaryTreeNode(2); expectedTree.RootNode.Right = new TrinaryTree.TrinaryTreeNode(9); expectedTree.RootNode.Right.Left = new TrinaryTree.TrinaryTreeNode(7); // act expectedTree.Delete(5); // assert }
public void GivenRightOnlyChildrenWhenDeleteExpectMinFromRightSubtreeAsReplacement2() { // arrange // create start tree // 5 // \ // 7 <-- to remove // \ // 12 // / | \ // replacement --> 9 12 14 // \ // 10 var startTree = new TrinaryTree(5); startTree.RootNode.Right = new TrinaryTree.TrinaryTreeNode(7); startTree.RootNode.Right.Right = new TrinaryTree.TrinaryTreeNode(12); startTree.RootNode.Right.Right.Left = new TrinaryTree.TrinaryTreeNode(9); startTree.RootNode.Right.Right.Left.Right = new TrinaryTree.TrinaryTreeNode(10); startTree.RootNode.Right.Right.Middle = new TrinaryTree.TrinaryTreeNode(12); startTree.RootNode.Right.Right.Right = new TrinaryTree.TrinaryTreeNode(14); // act startTree.Delete(7); // expected tree // 5 // \ // 12 // / | \ // 9 12 14 // \ // 10 // assert Assert.Equal(5, startTree.RootNode.Data); startTree.RootNode.AssertLeftIsNull().AssertMiddleIsNull(); Assert.Equal(12, startTree.RootNode.Right.Data); Assert.Equal(14, startTree.RootNode.Right.Right.Data); AssertNoChildren(startTree.RootNode.Right.Right); Assert.Equal(12, startTree.RootNode.Right.Middle.Data); AssertNoChildren(startTree.RootNode.Right.Middle); Assert.Equal(9, startTree.RootNode.Right.Left.Data); startTree.RootNode.Right.Left.AssertLeftIsNull().AssertMiddleIsNull(); Assert.Equal(10, startTree.RootNode.Right.Left.Right.Data); AssertNoChildren(startTree.RootNode.Right.Left.Right); }
public void GivenEmptyTreeWhenDeleteExpectNoChange() { // arrange var fixture = new Fixture(); var nonExistantValue = fixture.Create <int>(); var tree = new TrinaryTree(); // act tree.Delete(nonExistantValue); // assert tree.RootNode.Should().BeNull(); }
public void GivenIdenticalNumbersWhenDeleteExpectLastOneRemoved() { // arrange var fixture = new Fixture(); var valueToAdd = fixture.Create <int>(); var tree = new TrinaryTree(valueToAdd); tree.RootNode.Middle = new TrinaryTree.TrinaryTreeNode(valueToAdd); tree.RootNode.Middle.Middle = new TrinaryTree.TrinaryTreeNode(valueToAdd); // act tree.Delete(valueToAdd); // assert tree.RootNode.Data.Should().Be(valueToAdd); tree.RootNode.AssertLeftIsNull().AssertRightIsNull(); tree.RootNode.Middle.Data.Should().Be(valueToAdd); AssertNoChildren(tree.RootNode.Middle); }
public void GivenLeftOnlyChildrenWhenDeleteExpectLeftReassignedToParent() { // arrange // 5 // / | \ // 4 5 9 // / / // 2 7 // | // 2 TrinaryTree startTree = this.GetBasicTree(); // create expected tree // 5 // / | \ // 2 5 9 // | / // 2 7 var expectedTree = new TrinaryTree(5); expectedTree.RootNode.Left = new TrinaryTree.TrinaryTreeNode(4); expectedTree.RootNode.Left.Left = new TrinaryTree.TrinaryTreeNode(2); expectedTree.RootNode.Left.Left.Middle = new TrinaryTree.TrinaryTreeNode(2); expectedTree.RootNode.Middle = new TrinaryTree.TrinaryTreeNode(5); expectedTree.RootNode.Right = new TrinaryTree.TrinaryTreeNode(9); expectedTree.RootNode.Right.Left = new TrinaryTree.TrinaryTreeNode(7); // act startTree.Delete(4); // assert Assert.Equal(5, startTree.RootNode.Data); Assert.Equal(2, startTree.RootNode.Left.Data); Assert.Equal(2, startTree.RootNode.Left.Middle.Data); Assert.Equal(5, startTree.RootNode.Middle.Data); Assert.Equal(9, startTree.RootNode.Right.Data); Assert.Equal(7, startTree.RootNode.Right.Left.Data); }