code: def build_tree(
      theta,
      v,
      direction,
      j,
      epsilon,
      transform,
      loglike,
      gradient,
      Lmin,
  ):
      """The main recursion."""
      if j == 0:
          # Base case: Take a single leapfrog step in the direction v.
          (
              thetaprime,
              vprime,
              pprime,
              logpprime,
              reflected,
          ) = step_or_reflect(
              theta=theta,
              v=v * direction,
              epsilon=epsilon,
              transform=transform,
              loglike=loglike,
              gradient=gradient,
              Lmin=Lmin,
          )
  
          # if not sprime:
          #    print("stopped trajectory:", direction, logpprime, Lmin, (theta, thetaprime, epsilon))
          # Set the return values---minus=plus for all things here, since the
          # "tree" is of depth 0.
          thetaminus = thetaprime
          thetaplus = thetaprime
  
          if reflected and np.dot(v, vprime) <= 0:
              # if reversing locally, store that in can_continue, not s
              sprime = True
              # print("  reversed locally")
              can_continue = False
              vminus = v * direction
              vplus = v * direction
              v = v * direction
          else:
              # Is the point acceptable?
              sprime = logpprime > Lmin
              # if sprime:
              #    print("  -->")
              # else:
              #    print("  stuck")
              can_continue = True
              vminus = vprime * direction
              vplus = vprime * direction
              v = vprime * direction
  
          pminus = pprime
          pplus = pprime
          # print(direction, (theta, thetaprime), (v, vprime))
  
          # probability is zero if it is an invalid state
          alphaprime = 1.0 * (sprime and can_continue)
          nalphaprime = 1
          nreflectprime = reflected * 1
      else:
          # Recursion: Implicitly build the height j-1 left and right subtrees.
          (
              thetaminus,
              vminus,
              pminus,
              thetaplus,
              vplus,
              pplus,
              thetaprime,
              vprime,
              pprime,
              logpprime,
              sprime,
              can_continue,
              alphaprime,
              nalphaprime,
              nreflectprime,
          ) = build_tree(
              theta,
              v,
              direction,
              j - 1,
              epsilon,
              transform,
              loglike,
              gradient,
              Lmin,
          )
          # No need to keep going if the stopping criteria were met in the first subtree.
          if can_continue and sprime:
              if direction == -1:
                  (
                      thetaminus,
                      vminus,
                      pminus,
                      _,
                      _,
                      _,
                      thetaprime2,
                      vprime2,
                      pprime2,
                      logpprime2,
                      sprime2,
                      can_continue2,
                      alphaprime2,
                      nalphaprime2,
                      nreflectprime2,
                  ) = build_tree(
                      thetaminus,
                      vminus,
                      direction,
                      j - 1,
                      epsilon,
                      transform,
                      loglike,
                      gradient,
                      Lmin,
                  )
              else:
                  (
                      _,
                      _,
                      _,
                      thetaplus,
                      vplus,
                      pplus,
                      thetaprime2,
                      vprime2,
                      pprime2,
                      logpprime2,
                      sprime2,
                      can_continue2,
                      alphaprime2,
                      nalphaprime2,
                      nreflectprime2,
                  ) = build_tree(
                      thetaplus,
                      vplus,
                      direction,
                      j - 1,
                      epsilon,
                      transform,
                      loglike,
                      gradient,
                      Lmin,
                  )
  
              # Choose which subtree to propagate a sample up from.
              if np.random.uniform() < alphaprime2 / (
                  alphaprime + alphaprime2
              ):
                  thetaprime = thetaprime2[:]
                  vprime = vprime2[:]
                  pprime = pprime2[:]
                  logpprime = logpprime2
  
              # Update the stopping criterion.
              sturn = stop_criterion(
                  thetaminus, thetaplus, vminus, vplus
              )
              # print(sprime, sprime2, sturn)
              # if not (sprime and sprime2 and sturn):
              #    print("sprime stop:", sprime, sprime2, sturn)
              sprime = sprime and sprime2 and sturn
              can_continue = can_continue and can_continue2
              # Update the acceptance probability statistics.
              alphaprime += alphaprime2
              nalphaprime += nalphaprime2
              nreflectprime += nreflectprime2
  
      return (
          thetaminus,
          vminus,
          pminus,
          thetaplus,
          vplus,
          pplus,
          thetaprime,
          vprime,
          pprime,
          logpprime,
          sprime,
          can_continue,
          alphaprime,
          nalphaprime,
          nreflectprime,
      )
  
file_path: ultranest/ultranest/dychmc.py
imports: from __future__ import print_function, division
  import numpy
  import matplotlib.pyplot
label_desc: Use image node_id 3 for calling a global function with example usage: build_tree(theta, v, direction, j, epsilon, transform, loglike, gradient, Lmin) and returns: thetaminus,  vminus,  pminus,  thetaplus,  vplus,  pplus,  thetaprime,  vprime,  pprime,  logpprime,  sprime,  can_continue,  alphaprime,  nalphaprime,  nreflectprime