### Question:
Tree { public void grow(int buildDirection, double timeStep) { SubTree otherHalfTree = buildTree( buildDirection == -1 ? backward : forward, buildDirection, treeHeight, timeStep ); if (buildDirection == -1) { backward = otherHalfTree.backward; } else { forward = otherHalfTree.forward; } sumMetropolisAcceptanceProbability += otherHalfTree.sumMetropolisAcceptanceProbability; treeSize += otherHalfTree.treeSize; if (otherHalfTree.shouldContinue()) { if (Tree.acceptOtherProposalWithProbability(otherHalfTree.getLogSumWeight() - logSumWeight, random)) { proposal = otherHalfTree.proposal; } logSumWeight = logSumExp(logSumWeight, otherHalfTree.logSumWeight); sumMomentum = add(sumMomentum, otherHalfTree.sumMomentum); } diverged = otherHalfTree.diverged; if (!diverged) { uTurned = otherHalfTree.uTurned || Tree.isUTurning( forward.getVelocity(), backward.getVelocity(), sumMomentum ); } treeHeight++; } Tree(LeapfrogState startState,
                Proposal proposal,
                double maxEnergyChange,
                ProbabilisticModelWithGradient logProbGradientCalculator,
                LeapfrogIntegrator leapfrogIntegrator,
                List<? extends Variable> sampleFromVariables,
                KeanuRandom random); void grow(int buildDirection,
                     double timeStep); static double logSumExp(double a, double b); boolean shouldContinue(); void save(Statistics statistics);  }### Answer:
@Test public void treeSizeAndHeightGrows() { Tree tree = new Tree( start, null, 1000, gradientCalculator, leapfrogIntegrator, singletonList(vertex), KeanuRandom.getDefaultRandom() ); tree.grow(1, 1e-6); tree.grow(-1, 1e-6); tree.grow(-1, 1e-6); assertEquals(3, tree.getTreeHeight()); assertEquals((int) Math.pow(2, tree.getTreeHeight()) - 1, tree.getTreeSize()); }
@Test public void treeGrowthEvaluatesOneLogProbPerStep() { ProbabilisticModelWithGradient mockModel = mock(ProbabilisticModelWithGradient.class); Map<VariableReference, DoubleTensor> mockGradient = ImmutableMap.of(vertex.getId(), DoubleTensor.scalar(0.0)); when(mockModel.logProbGradients(anyMap())) .thenReturn(mockGradient); Tree tree = new Tree( start, null, 1000, mockModel, leapfrogIntegrator, singletonList(vertex), KeanuRandom.getDefaultRandom() ); tree.grow(1, 1e-6); verify(mockModel, times(tree.getTreeSize())).logProbGradients(anyMap()); tree.grow(-1, 1e-6); verify(mockModel, times(tree.getTreeSize())).logProbGradients(anyMap()); tree.grow(-1, 1e-6); verify(mockModel, times(tree.getTreeSize())).logProbGradients(anyMap()); tree.grow(1, 1e-6); verify(mockModel, times(tree.getTreeSize())).logProbGradients(anyMap()); }