### Question:
QuerierImpl implements Querier { @Override public <E, N extends OpenBinaryTreeNode<E, N>> boolean isBalanced(N node) { if (node == null) { throw new NullPointerException("A null node cannot be balanced"); } return partialReduce(node, IsBalancedTreeReducer.get()).isBalanced(); } QuerierImpl(); @Override SimpleCollection<List<N>> pathsToLeaves(N node); @Override boolean isBalanced(N node); @Override boolean isPerfectlyBalanced(N node); @Override R partialReduce(
            N node,
            BiFunction<N, Collection<R>, ReductionResult<R>> reducer);  }### Answer:
@Test(expected = NullPointerException.class) public void balancedNull() { final Querier querier = new QuerierImpl(); querier.isBalanced((BinaryTreeNode<String>) null); }
@Test(expected = IllegalArgumentException.class) public void emptyTreeBalanced() { final Querier querier = new QuerierImpl(); querier.isBalanced(new EmptyConstructor<>().build()); }
@Test public void leafBalanced() { final Querier querier = new QuerierImpl(); assertTrue(querier.isBalanced(new Constructor<String>().build("a", new MutableBinaryTreeImpl[] {}))); }
@Test public void offByOneBalanced() { final Constructor<String> constructor = new Constructor<>(); final Tree<String, MutableBinaryTreeNode<String>> tree = constructor.build("a", new MutableBinaryTreeImpl[] { constructor.build("b", new MutableBinaryTreeImpl[]{})}); final Querier querier = new QuerierImpl(); assertTrue(querier.isBalanced(tree)); }
@Test public void unbalanced() { final Constructor<String> constructor = new Constructor<>(); final Tree<String, MutableBinaryTreeNode<String>> tree = constructor.build("a", new MutableBinaryTreeImpl[] { constructor.build("b", new MutableBinaryTreeImpl[]{ constructor.build("c", new MutableBinaryTreeImpl[]{ constructor.build("d", new MutableBinaryTreeImpl[] {})})})}); final Querier querier = new QuerierImpl(); assertFalse(querier.isBalanced(tree)); }
@Test public void balanced() { final Constructor<String> constructor = new Constructor<>(); final Tree<String, MutableBinaryTreeNode<String>> tree = constructor.build("a", new MutableBinaryTreeImpl[] { constructor.build("b", new MutableBinaryTreeImpl[]{}), constructor.build("b", new MutableBinaryTreeImpl[]{})}); final Querier querier = new QuerierImpl(); assertTrue(querier.isBalanced(tree)); }
@Test public void largeBalanced() { final Constructor<String> constructor = new Constructor<>(); final Tree<String, MutableBinaryTreeNode<String>> tree = constructor.build("a", new MutableBinaryTreeImpl[] { constructor.build("b", new MutableBinaryTreeImpl[]{ constructor.build("c", new MutableBinaryTreeImpl[]{ constructor.build("d", new MutableBinaryTreeImpl[]{}), constructor.build("d", new MutableBinaryTreeImpl[]{}) }), constructor.build("c", new MutableBinaryTreeImpl[]{ constructor.build("d", new MutableBinaryTreeImpl[]{}), constructor.build("d", new MutableBinaryTreeImpl[]{}) }) }), constructor.build("b", new MutableBinaryTreeImpl[]{ constructor.build("c", new MutableBinaryTreeImpl[]{ constructor.build("d", new MutableBinaryTreeImpl[]{}), constructor.build("d", new MutableBinaryTreeImpl[]{}) }), constructor.build("c", new MutableBinaryTreeImpl[]{ constructor.build("d", new MutableBinaryTreeImpl[]{}), constructor.build("d", new MutableBinaryTreeImpl[]{}) }) }) }); final Querier querier = new QuerierImpl(); assertTrue(querier.isBalanced(tree)); }