public static List<Pair<TraversalParent, List<Step<?,?>>>> collectStepsOfAssignableClassRecursivelyFromDepthGroupedByParent(
            final Traversal.Admin<?, ?> traversal) {

        final List<Pair<TraversalParent, List<Step<?,?>>>> traversalParentCollectionList = new ArrayList<>();
        final Stack<Step<?,?>> processingStack = new Stack<>();

        final List<Step<?,?>> noParentlist = new ArrayList<>();
        traversal.getSteps().forEach(childStep -> {
            handleChildStepCollection(processingStack, noParentlist, childStep);
        });
        if (!noParentlist.isEmpty()) {
            // we reverse the list here to keep it the same way as we had it before combining
            // the recursive collection and grouping into this method
            Collections.reverse(noParentlist);
            // steps of the main/root traversal do not have any parent, so we just add them under the EmptyStep
            traversalParentCollectionList.add(new Pair<>((TraversalParent)EmptyStep.instance(), noParentlist));
        }

        while (!processingStack.isEmpty()) {
            final List<Step<?,?>> childStepCollectionlist = new ArrayList<>();
            final Step<?,?> current = processingStack.pop();

            if (current instanceof TraversalParent) {
                ((TraversalParent) current).getLocalChildren().forEach(localChild -> localChild.getSteps().forEach(childStep -> {
                    handleChildStepCollection(processingStack, childStepCollectionlist, childStep);
                }));
                ((TraversalParent) current).getGlobalChildren().forEach(globalChild -> globalChild.getSteps().forEach(childStep -> {
                    handleChildStepCollection(processingStack, childStepCollectionlist, childStep);
                }));

                if (!childStepCollectionlist.isEmpty()) {
                    // we reverse the childStepCollectionlist here to keep it the same way as we had it before combining
                    // the recursive collection and grouping into this method
                    Collections.reverse(childStepCollectionlist);
                    // desired children are added together/grouped on the traversal parent
                    traversalParentCollectionList.add(new Pair<>((TraversalParent)current, childStepCollectionlist));
                }
            }
        }
        // reverse the list such that we get the deepest steps first
        Collections.reverse(traversalParentCollectionList);
        return traversalParentCollectionList;
    }
Get steps of the specified class throughout the traversal and grouping them based on the traversal parent
collecting them in a fashion that orders them from the deepest steps first
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/strategy/optimization/FilterRankingStrategy.java