PreOrderTreeWalk implements TreeWalk<E, N> { @Override public <C extends TreeWalkCallback<E, N>> C perform(N root, C callback) { if (root == null) { return null; } callback.apply(root); for (TreeNode<E> child : root.getChildren()) { if (child != null) { perform((N) child, callback); } } return callback; }  @Override C perform(N root, C callback);  }
@Test @Override public void testWalkingOnALeafNode() throws Exception { final BinaryTreeNode<Integer> node = create(10, null, null); final PreOrderTreeWalk<Integer, BinaryTreeNode<Integer>> walk = new PreOrderTreeWalk<>(); final LoggingTreeWalkCallback<Integer, BinaryTreeNode<Integer>> callback = new LoggingTreeWalkCallback<>(); walk.perform(node, callback); assertThat(callback.getLog(), is(notNullValue())); assertThat(callback.getLog(), hasSize(1)); assertThat(callback.getLog().get(0), is(node)); }
@Test @Override public void testWalkingWithALeftBranch() throws Exception { final BinaryTreeNode<Integer> node = create(10, create(11, create(12, create(13, null, null), null), null), null); final PreOrderTreeWalk<Integer, BinaryTreeNode<Integer>> walk = new PreOrderTreeWalk<>(); final LoggingTreeWalkCallback<Integer, BinaryTreeNode<Integer>> callback = new LoggingTreeWalkCallback<>(); walk.perform(node, callback); assertThat(callback.getLog(), is(notNullValue())); assertThat(callback.getLog(), hasSize(4)); assertThat(callback.getLog().get(0).getValue(), is(10)); assertThat(callback.getLog().get(1).getValue(), is(11)); assertThat(callback.getLog().get(2).getValue(), is(12)); assertThat(callback.getLog().get(3).getValue(), is(13)); }
@Test @Override public void testWalkingWithARightBranch() throws Exception { final BinaryTreeNode<Integer> node = create(10, null, create(11, null, create(12, null, create(13, null, null)))); final PreOrderTreeWalk<Integer, BinaryTreeNode<Integer>> walk = new PreOrderTreeWalk<>(); final LoggingTreeWalkCallback<Integer, BinaryTreeNode<Integer>> callback = new LoggingTreeWalkCallback<>(); walk.perform(node, callback); assertThat(callback.getLog(), is(notNullValue())); assertThat(callback.getLog(), hasSize(4)); assertThat(callback.getLog().get(0).getValue(), is(10)); assertThat(callback.getLog().get(1).getValue(), is(11)); assertThat(callback.getLog().get(2).getValue(), is(12)); assertThat(callback.getLog().get(3).getValue(), is(13)); }
@Test @Override public void testWalkingWithAFullTree() throws Exception { final BinaryTreeNode<Integer> node = create(10, create(11, create(12, null, null), create(13, null, null)), create(14, create(15, null, null), create(16, null, null))); final PreOrderTreeWalk<Integer, BinaryTreeNode<Integer>> walk = new PreOrderTreeWalk<>(); final LoggingTreeWalkCallback<Integer, BinaryTreeNode<Integer>> callback = new LoggingTreeWalkCallback<>(); walk.perform(node, callback); assertThat(callback.getLog(), is(notNullValue())); assertThat(callback.getLog(), hasSize(7)); assertThat(callback.getLog().get(0).getValue(), is(10)); assertThat(callback.getLog().get(1).getValue(), is(11)); assertThat(callback.getLog().get(2).getValue(), is(12)); assertThat(callback.getLog().get(3).getValue(), is(13)); assertThat(callback.getLog().get(4).getValue(), is(14)); assertThat(callback.getLog().get(5).getValue(), is(15)); assertThat(callback.getLog().get(6).getValue(), is(16)); }