### Question:
UndirectedTree { public void addRelationship(int node1, int node2) { if (node1 < node2) { addChild(node1, node2); } else if (node1 > node2) { addChild(node2, node1); } } UndirectedTree(int capacity); void addRelationship(int node1, int node2); void removeRelationship(int node1, int node2); int size(); boolean isEmpty(); boolean nonEmpty(); void forEachBFS(int root, RelationshipConsumer consumer); void forEachDFS(int root, RelationshipConsumer consumer); void verifyTreeIntegrity(); String toString(int root);  }### Answer:
@Test void shouldNotResizeDynamically() { try { new UndirectedTree(1) .addRelationship(0, 1); } catch (IllegalArgumentException e) { assertEquals( "Cannot operate on node (1) with capacity 1", e.getMessage()); } }
@Test void testLinearTree() { UndirectedTree tree = new UndirectedTree(4); tree.addRelationship(0, 1); tree.addRelationship(2, 3); tree.addRelationship(2, 1); testDfs(tree, "0|1", "1|2", "2|3"); testBfs(tree, "0|1", "1|2", "2|3"); }
@Test void testSimpleBranchTree() { UndirectedTree tree = new UndirectedTree(4); tree.addRelationship(0, 2); tree.addRelationship(1, 0); tree.addRelationship(1, 3); testDfs(tree, "0|1", "1|3", "0|2"); testBfs(tree, "0|1", "0|2", "1|3"); }
@Test void testLargerTree() { UndirectedTree tree = new UndirectedTree(12); tree.addRelationship(0, 1); tree.addRelationship(0, 4); tree.addRelationship(0, 8); tree.addRelationship(1, 2); tree.addRelationship(4, 5); tree.addRelationship(4, 7); tree.addRelationship(8, 9); tree.addRelationship(2, 3); tree.addRelationship(5, 6); tree.addRelationship(9, 10); tree.addRelationship(9, 11); testDfs( tree, "0|8", "8|9", "9|11", "9|10", "0|4", "4|7", "4|5", "5|6", "0|1", "1|2", "2|3"); testBfs( tree, "0|8", "0|4", "0|1", "8|9", "4|7", "4|5", "1|2", "9|11", "9|10", "5|6", "2|3"); }
@Test void shouldReportInvalidTrees() { UndirectedTree tree = new UndirectedTree(6); tree.addRelationship(0, 1); tree.addRelationship(0, 2); tree.addRelationship(1, 3); tree.addRelationship(2, 4); tree.addRelationship(4, 5); try { tree.addRelationship(1, 2); fail(); } catch (IllegalStateException e) { assertEquals("node (2) already has a parent", e.getMessage()); } }