@Test public final void testDepthFirstTraversal() { String[] output = new String[] {"Root", "Leaf1", "Child1", "Child1Leaf", "Child2", "Child2Leaf1", "Child2Leaf2", "Leaf2"}; int i = 0; for (Tree<String> t : TreeTraversal.depthFirstTraversal(tree)) { assertEquals(output[i++], t.getLabel()); } }
public static <T> List<Tree<T>> depthFirstTraversal(Tree<T> tree) { Stack<Tree<T>> treeStack = new Stack<>(); treeStack.push(tree); ArrayList<Tree<T>> dfs = new ArrayList<>(); while (!treeStack.isEmpty()) { Tree<T> t = treeStack.pop(); dfs.add(t); Stack<Tree<T>> tmpStack = new Stack<>(); for (Tree<T> child : t.childrenIterator()) { tmpStack.add(child); } while (!tmpStack.isEmpty()) { treeStack.add(tmpStack.pop()); } } return dfs; }
TreeTraversal { public static <T> List<Tree<T>> depthFirstTraversal(Tree<T> tree) { Stack<Tree<T>> treeStack = new Stack<>(); treeStack.push(tree); ArrayList<Tree<T>> dfs = new ArrayList<>(); while (!treeStack.isEmpty()) { Tree<T> t = treeStack.pop(); dfs.add(t); Stack<Tree<T>> tmpStack = new Stack<>(); for (Tree<T> child : t.childrenIterator()) { tmpStack.add(child); } while (!tmpStack.isEmpty()) { treeStack.add(tmpStack.pop()); } } return dfs; } }
TreeTraversal { public static <T> List<Tree<T>> depthFirstTraversal(Tree<T> tree) { Stack<Tree<T>> treeStack = new Stack<>(); treeStack.push(tree); ArrayList<Tree<T>> dfs = new ArrayList<>(); while (!treeStack.isEmpty()) { Tree<T> t = treeStack.pop(); dfs.add(t); Stack<Tree<T>> tmpStack = new Stack<>(); for (Tree<T> child : t.childrenIterator()) { tmpStack.add(child); } while (!tmpStack.isEmpty()) { treeStack.add(tmpStack.pop()); } } return dfs; }  }
TreeTraversal { public static <T> List<Tree<T>> depthFirstTraversal(Tree<T> tree) { Stack<Tree<T>> treeStack = new Stack<>(); treeStack.push(tree); ArrayList<Tree<T>> dfs = new ArrayList<>(); while (!treeStack.isEmpty()) { Tree<T> t = treeStack.pop(); dfs.add(t); Stack<Tree<T>> tmpStack = new Stack<>(); for (Tree<T> child : t.childrenIterator()) { tmpStack.add(child); } while (!tmpStack.isEmpty()) { treeStack.add(tmpStack.pop()); } } return dfs; }  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); }
TreeTraversal { public static <T> List<Tree<T>> depthFirstTraversal(Tree<T> tree) { Stack<Tree<T>> treeStack = new Stack<>(); treeStack.push(tree); ArrayList<Tree<T>> dfs = new ArrayList<>(); while (!treeStack.isEmpty()) { Tree<T> t = treeStack.pop(); dfs.add(t); Stack<Tree<T>> tmpStack = new Stack<>(); for (Tree<T> child : t.childrenIterator()) { tmpStack.add(child); } while (!tmpStack.isEmpty()) { treeStack.add(tmpStack.pop()); } } return dfs; }  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);  }