@Test public void testPruningRootNodeAndPromotingLeftChildToRootDuringVisit() { Node root = new Node(); Node left = new Node(); root.setLeft(left); TreeVisitor visitor = mock(TreeVisitor.class); when(visitor.before(root, 0)).thenReturn(root); when(visitor.visit(root, 0)).thenReturn(left); when(visitor.after(left, 0)).thenReturn(left); when(visitor.before(left, 1)).thenReturn(left); when(visitor.visit(left, 1)).thenReturn(left); when(visitor.after(left, 1)).thenReturn(left); Node result = new TreeTraverser().traverse(root, visitor); verify(visitor).before(root, 0); verify(visitor).before(left, 1); verify(visitor).visit(left, 1); verify(visitor).after(left, 1); verify(visitor).visit(root, 0); verify(visitor).after(left, 0); verifyNoMoreInteractions(visitor); assertSame(left, result); assertEquals(0, result.childCount()); }
public Node traverse(Node node, TreeVisitor... visitors) { if (ArrayUtils.isEmpty(visitors)) { throw new IllegalArgumentException("You need to provide at least one visitor for this traverser"); } Node result = node; for (TreeVisitor visitor : visitors) { result = traverse(result, visitor, 0); } return result; }
TreeTraverser { public Node traverse(Node node, TreeVisitor... visitors) { if (ArrayUtils.isEmpty(visitors)) { throw new IllegalArgumentException("You need to provide at least one visitor for this traverser"); } Node result = node; for (TreeVisitor visitor : visitors) { result = traverse(result, visitor, 0); } return result; } }
TreeTraverser { public Node traverse(Node node, TreeVisitor... visitors) { if (ArrayUtils.isEmpty(visitors)) { throw new IllegalArgumentException("You need to provide at least one visitor for this traverser"); } Node result = node; for (TreeVisitor visitor : visitors) { result = traverse(result, visitor, 0); } return result; }  }
TreeTraverser { public Node traverse(Node node, TreeVisitor... visitors) { if (ArrayUtils.isEmpty(visitors)) { throw new IllegalArgumentException("You need to provide at least one visitor for this traverser"); } Node result = node; for (TreeVisitor visitor : visitors) { result = traverse(result, visitor, 0); } return result; }  Node traverse(Node node, TreeVisitor... visitors); }
TreeTraverser { public Node traverse(Node node, TreeVisitor... visitors) { if (ArrayUtils.isEmpty(visitors)) { throw new IllegalArgumentException("You need to provide at least one visitor for this traverser"); } Node result = node; for (TreeVisitor visitor : visitors) { result = traverse(result, visitor, 0); } return result; }  Node traverse(Node node, TreeVisitor... visitors);  }