TreeTraversal { public static <T> List<Tree<T>> breadthFirstTraversal(Tree<T> tree) { Queue<Tree<T>> nodeQueue = new LinkedList<>(); nodeQueue.offer(tree); ArrayList<Tree<T>> bfs = new ArrayList<>(); while (!nodeQueue.isEmpty()) { Tree<T> t = nodeQueue.remove(); bfs.add(t); for (Tree<T> child : t.childrenIterator()) { nodeQueue.offer(child); } } return bfs; }  static List<Tree<T>> depthFirstTraversal(Tree<T> tree); static List<Tree<T>> breadthFirstTraversal(Tree<T> tree); static List<Tree<T>> depthLimitedTraversal(Tree<T> tree, int depth); static List<Tree<T>> bottomUpBreadthFirstTraversal(Tree<T> tree);  }
@Test public final void testBreadthFirstTraversal() { String[] output = new String[] {"Root", "Leaf1", "Child1", "Child2", "Leaf2", "Child1Leaf", "Child2Leaf1", "Child2Leaf2"}; int i = 0; for (Tree<String> t : TreeTraversal.breadthFirstTraversal(tree)) { assertEquals(output[i++], t.getLabel()); } }