public void Test_Delete_On_Single_Node_Tree() { // Arrange BinaryTree bt = new BinaryTree(); Data data = new Data(17, "Data17"); bt.Insert(17, data); // Act bool removed = bt.Remove(17); // Should find the single node (17 == 17)... // Assert Assert.IsTrue(removed); Assert.IsTrue(bt.TreeDepth() == 0); }
public void Test_Remove_On_Root_Node_With_Left_and_right_Subtrees() { // Arrange BinaryTree bt = new BinaryTree(); // The test tree looks like: // 11 // / \ // 5 17 // / \ // 2 7 // / \ // 1 4 // / // 3 Node[] nArray = new Node[8]; nArray[0] = new Node(11); // This is the node we will delete below (root node). nArray[1] = new Node(5); nArray[2] = new Node(17); nArray[3] = new Node(2); nArray[4] = new Node(7); nArray[5] = new Node(1); nArray[6] = new Node(4); nArray[7] = new Node(3); // This order of insertion should create a our "complex" tree. for (int i = 0; i < 8; i++) { bt.Insert(nArray[i]); } // Act bool removed = bt.Remove(11); // Assert Assert.IsTrue(removed); Queue<Node> queue = bt.TreeToQueue(); // Get the remaining elements to validate them... Assert.IsTrue(queue.Count == 7); Assert.IsTrue(1 == queue.Dequeue().iKey); Assert.IsTrue(2 == queue.Dequeue().iKey); Assert.IsTrue(3 == queue.Dequeue().iKey); Assert.IsTrue(4 == queue.Dequeue().iKey); Assert.IsTrue(5 == queue.Dequeue().iKey); Assert.IsTrue(7 == queue.Dequeue().iKey); Assert.IsTrue(17 == queue.Dequeue().iKey); }
public void Test_Remove_On_Null_Tree() { // Arrange BinaryTree bt = new BinaryTree(); // Act bool removed = bt.Remove(23); // Shouldn't find any node match. // Assert Assert.IsFalse(removed); }
public void Test_Remove_On_Node_With_Only_A_Right_Subtree() { // Arrange BinaryTree bt = new BinaryTree(); Node n1 = new Node(17); Node n2 = new Node(23); // This is the node we will delete. Node n3 = new Node(29); // This order of insertion should create a right-only subtree. bt.Insert(n1); bt.Insert(n2); bt.Insert(n3); // Act bool removed = bt.Remove(23); // Assert Assert.IsTrue(removed); Assert.IsTrue(bt.TreeDepth() == 2); }
public void Test_Remove_On_Node_With_Only_A_Left_Subtree() { // Arrange BinaryTree bt = new BinaryTree(); Node n1 = new Node(17); Node n2 = new Node(14); // This is the one we will remove. Node n3 = new Node(9); // This order of insertion should create a left-only subtree. bt.Insert(n1); bt.Insert(n2); bt.Insert(n3); // Act bool removed = bt.Remove(14); // Assert Assert.IsTrue(removed); Assert.IsTrue(bt.TreeDepth() == 2); Queue<Node> queue = bt.TreeToQueue(); Assert.IsTrue(queue.Dequeue() == n3); Assert.IsTrue(queue.Dequeue() == n1); }
public void Test_Remove_On_Node_With_Left_and_right_Leaves() { // Arrange BinaryTree bt = new BinaryTree(); Node n1 = new Node(17); Node n2 = new Node(23); // This is the node we will delete. Node n3 = new Node(21); Node n4 = new Node(29); bt.Insert(n1); bt.Insert(n2); // Node to delete. bt.Insert(n3); bt.Insert(n4); // Act bool removed = bt.Remove(23); // Assert Assert.IsTrue(removed); Queue<Node> queue = bt.TreeToQueue(); // To validate the remaining tree nodes. Assert.IsTrue(queue.Count == 3); Assert.IsTrue(17 == queue.Dequeue().iKey); Assert.IsTrue(21 == queue.Dequeue().iKey); Assert.IsTrue(29 == queue.Dequeue().iKey); }