GraphUtil { public static <V, E> void copyTree(Tree<V, E> source, Tree<V, E> target) { target.addVertex(source.getRoot()); LinkedList<E> toAdd = new LinkedList<E>(source.getChildEdges(source.getRoot())); while (!toAdd.isEmpty()) { E e = toAdd.removeFirst(); target.addEdge(e, source.getSource(e), source.getDest(e)); toAdd.addAll(source.getChildEdges(source.getDest(e))); } }  static void addVertices(Graph<V, E> graph, Collection<V> vertices); static void copyGraph(Graph<V, E> source, Graph<V, E> target); static void copyTree(Tree<V, E> source, Tree<V, E> target); static boolean isDescendant(Tree<V, E> t, V v, V w); static boolean isWeaklyConnected(Hypergraph<V, E> graph); static boolean areVerticesWeaklyConnected(Hypergraph<V, E> studyGraph, V first, V second); static boolean isSimpleCycle(UndirectedGraph<V, E> graph); static V findCommonAncestor(Tree<V, E> tree, V u, V v); static List<V> findPath(Tree<V, E> tree, V u, V v);  }
@Test public void testCopyTree() { DelegateTree<String, Integer> tree = new DelegateTree<String, Integer>(); tree.setRoot("A"); tree.addChild(1, "A", "D"); tree.addChild(2, "D", "B"); tree.addChild(3, "D", "C"); tree.addChild(4, "D", "E"); tree.addChild(5, "A", "F"); tree.addChild(6, "C", "G"); Tree<String, Integer> copy = new DelegateTree<String, Integer>(); GraphUtil.copyTree(tree, copy); assertEquals(tree.getVertexCount(), copy.getVertexCount()); assertEquals(tree.getEdgeCount(), copy.getEdgeCount()); }