I need to traverse a binary tree, skipping the children of any node for which a condition is met.
This implements a tree-guided clustering approach; the leaves of a subtree are considered a cluster when they collectively meet the condition.
It seems like the place to start would be pre-order traversal, but I’m not sure how to modify the algorithm to skip all the children of the current node.
Update
In addition to the two (correct) answers below, one can use the following Java libraries:
- MyArch TreeIter – Generic (with adapter class) tree traversal, with child skipping and dynamic maximum traversal depth
- Phylosoft Forester – Tree implementation with
getAllExternalDescendantsand a Newick-to-XML converter
If by skipping all the children you mean not just direct descendants but their entire subtrees, you can do the following: