Iterator<T> (@Nullable T start, boolean fwd) { JBTreeTraverser<T> traverser = JBTreeTraverser.from(n -> { int count = n.getChildCount(); List<T> children = new ArrayList<>(count); for (int i = 0; i < count; ++i) { T c = ObjectUtils.tryCast(n.getChildAt(fwd ? i : count - i - 1), getNodeClass()); if (c != null) children.add(c); } return children; }); if (start == null) { traverser = traverser.withRoot(getRoot()); } else { List<T> roots = new ArrayList<>(); for (TreeNode node = null, parent = start; parent != null; node = parent, parent = node.getParent()) { int idx = node == null ? -1 : parent.getIndex(node); for (int i = fwd ? idx + 1 : 0, c = fwd ? parent.getChildCount() : idx; i < c; ++i) { T child = ObjectUtils.tryCast(parent.getChildAt(fwd ? i : idx - i - 1), getNodeClass()); if (child != null) roots.add(child); } } traverser = traverser.withRoots(roots); } return traverser.preOrderDfsTraversal().iterator(); }