@Override
    public Tree projectTraverser(final Traverser.Admin<S> traverser) {
        final Tree topTree = new Tree();
        Tree depth = topTree;
        final Path path = traverser.path();
        for (int i = 0; i < path.size(); i++) {
            final TraversalProduct product = TraversalUtil.produce(path.<Object>get(i), this.traversalRing.next());
            if (product.isProductive()) {
                final Object object = product.get();
                if (!depth.containsKey(object))
                    depth.put(object, new Tree<>());
                depth = (Tree) depth.get(object);
            }
        }
        this.traversalRing.reset();
        return topTree;
    }
@author Marko A. Rodriguez (http://markorodriguez.com)
gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/process/traversal/step/map/TreeStep.java