code: public static NextNodeAndQuery getNextNodeAndQuery(IntermediateQuery query,
                                                         NodeCentricOptimizationResults<? extends QueryNode> results) {
  
          /**
           * If there is still a node at center: gets the next one of it
           */
          Optional<? extends QueryNode> optionalNewNode = results.getOptionalNewNode();
          if (optionalNewNode.isPresent()) {
              Optional<QueryNode> optionalNextNode = getDepthFirstNextNode(query, optionalNewNode.get());
  
              return new NextNodeAndQuery(optionalNextNode, query);
          }
  
          /**
           * Otherwise, if there is a replacing child: returns it
           */
          Optional<QueryNode> optionalReplacingChild = results.getOptionalReplacingChild();
          if (optionalReplacingChild.isPresent()) {
              return new NextNodeAndQuery(optionalReplacingChild, query);
          }
  
          /**
           * Otherwise, if there is a next sibling: returns it
           */
          Optional<QueryNode> optionalNextSibling = results.getOptionalNextSibling();
          if (optionalNextSibling.isPresent()) {
              return new NextNodeAndQuery(optionalNextSibling, query);
          }
  
          /**
           * Otherwise, looks for the next node of the closest ancestor (already visited)
           */
          Optional<QueryNode> optionalClosestAncestor = results.getOptionalClosestAncestor();
          if (optionalClosestAncestor.isPresent()) {
              Optional<QueryNode> optionalNextNode = getDepthFirstNextNode(query, optionalClosestAncestor.get(), true);
              return new NextNodeAndQuery(optionalNextNode, query);
          }
          /**
           * Nothing else to explore
           */
          else {
              return new NextNodeAndQuery(Optional.<QueryNode>empty(), query);
          }
      }
docs: Finds the next node to visit in a new intermediate query