public static <T> Iterable<T> depthFirst(TreeDef<T> treeDef, T node) {
		return () -> new Iterator<T>() {
			Deque<T> queue = new ArrayDeque<>(Arrays.asList(node));

			@Override
			public boolean hasNext() {
				return !queue.isEmpty();
			}

			@Override
			public T next() {
				if (queue.isEmpty()) {
					throw new NoSuchElementException();
				}
				T next = queue.removeLast();
				List<T> children = treeDef.childrenOf(next);
				ListIterator<T> iterator = children.listIterator(children.size());
				while (iterator.hasPrevious()) {
					queue.addLast(iterator.previous());
				}
				return next;
			}
		};
	}
public static < T > Iterable < T > depthFirst ( TreeDef < T > treeDef , T node ) { return ( ) -> new Iterator < T > ( ) { Deque < T > queue = new ArrayDeque < > ( Arrays . asList ( node ) ) ; @ Override public boolean hasNext ( ) { return ! queue . isEmpty ( ) ; } @ Override public T next ( ) { if ( queue . isEmpty ( ) ) { throw new NoSuchElementException ( ) ; } T next = queue . removeLast ( ) ; List < T > children = treeDef . childrenOf ( next ) ; ListIterator < T > iterator = children . listIterator ( children . size ( ) ) ; while ( iterator . hasPrevious ( ) ) { queue . addLast ( iterator . previous ( ) ) ; } return next ; } } ; }
Creates an {@code Iterable} that starts at {@code node} and iterates deeper into the tree in a depth-first order.