@Test public void test_traverseByBreadth(){ final StringBuilder letters = new StringBuilder(); final StringBuilder nests = new StringBuilder(); Algorithms.traverseByBreadth(root, new NodeVisitor() { @Override public boolean visit(Node node, int nest) { letters.append(node.getLetters()); nests.append(nest); return true; } }); Assert.assertEquals("abcdefgh", letters.toString()); Assert.assertEquals("01112223", nests.toString()); }
public static void traverseByBreadth(Node root, NodeVisitor visitor){ Queue<Pair<Node, Integer>> nodeAndNests = new LinkedList<Pair<Node, Integer>>(); nodeAndNests.offer(Pair.create(root, 0)); Pair<Node, Integer> nodeAndNest = null; while((nodeAndNest = nodeAndNests.poll()) != null){ Node node = nodeAndNest.getFirst(); int nest = nodeAndNest.getSecond(); if(!visitor.visit(node, nest)) return; nest++; for(Node child : node.getChildren()){ nodeAndNests.offer(Pair.create(child, nest)); } } }
Algorithms { public static void traverseByBreadth(Node root, NodeVisitor visitor){ Queue<Pair<Node, Integer>> nodeAndNests = new LinkedList<Pair<Node, Integer>>(); nodeAndNests.offer(Pair.create(root, 0)); Pair<Node, Integer> nodeAndNest = null; while((nodeAndNest = nodeAndNests.poll()) != null){ Node node = nodeAndNest.getFirst(); int nest = nodeAndNest.getSecond(); if(!visitor.visit(node, nest)) return; nest++; for(Node child : node.getChildren()){ nodeAndNests.offer(Pair.create(child, nest)); } } } }
Algorithms { public static void traverseByBreadth(Node root, NodeVisitor visitor){ Queue<Pair<Node, Integer>> nodeAndNests = new LinkedList<Pair<Node, Integer>>(); nodeAndNests.offer(Pair.create(root, 0)); Pair<Node, Integer> nodeAndNest = null; while((nodeAndNest = nodeAndNests.poll()) != null){ Node node = nodeAndNest.getFirst(); int nest = nodeAndNest.getSecond(); if(!visitor.visit(node, nest)) return; nest++; for(Node child : node.getChildren()){ nodeAndNests.offer(Pair.create(child, nest)); } } }  }
Algorithms { public static void traverseByBreadth(Node root, NodeVisitor visitor){ Queue<Pair<Node, Integer>> nodeAndNests = new LinkedList<Pair<Node, Integer>>(); nodeAndNests.offer(Pair.create(root, 0)); Pair<Node, Integer> nodeAndNest = null; while((nodeAndNest = nodeAndNests.poll()) != null){ Node node = nodeAndNest.getFirst(); int nest = nodeAndNest.getSecond(); if(!visitor.visit(node, nest)) return; nest++; for(Node child : node.getChildren()){ nodeAndNests.offer(Pair.create(child, nest)); } } }  static void traverseByBreadth(Node root, NodeVisitor visitor); static void traverseByDepth(Node root, NodeVisitor visitor); static void dump(Node root, Writer writer); static boolean contains(Node root, String text); Iterable<String> commonPrefixSearch(Node root, String query); }
Algorithms { public static void traverseByBreadth(Node root, NodeVisitor visitor){ Queue<Pair<Node, Integer>> nodeAndNests = new LinkedList<Pair<Node, Integer>>(); nodeAndNests.offer(Pair.create(root, 0)); Pair<Node, Integer> nodeAndNest = null; while((nodeAndNest = nodeAndNests.poll()) != null){ Node node = nodeAndNest.getFirst(); int nest = nodeAndNest.getSecond(); if(!visitor.visit(node, nest)) return; nest++; for(Node child : node.getChildren()){ nodeAndNests.offer(Pair.create(child, nest)); } } }  static void traverseByBreadth(Node root, NodeVisitor visitor); static void traverseByDepth(Node root, NodeVisitor visitor); static void dump(Node root, Writer writer); static boolean contains(Node root, String text); Iterable<String> commonPrefixSearch(Node root, String query);  }