I have multi-way tree structure, which i have to traverse to determine status. I am looking into 'Post-Order' type traversal where leaf nodes are processed first. And search end condition depends on any of child node having status inactive. Also, from performance perspective, i would to use JDK7's fork-join mechanism.
Here is a (very) rough sketch of how you could do it.
final class TreeNode {
private final Iterable<TreeNode> children;
TreeNode(Iterable<TreeNode> aChildren) {
children = aChildren;
}
Iterable<TreeNode> getChildren() {
return children;
}
void postorder(TreeNodeIterator iterator) {
postorderTraverse(this, iterator);
}
private void postorderTraverse(TreeNode node, TreeNodeIterator iterator) {
for (TreeNode child : children) {
postorderTraverse(child, iterator);
}
iterator.visit(node);
}
void postorderParallel(TreeNodeIterator iterator) {
new ForkJoinPool().invoke(new VisitNodeAction(iterator, this));
}
interface TreeNodeIterator {
void visit(TreeNode child);
}
private class VisitNodeAction extends RecursiveAction {
private final TreeNodeIterator iterator;
private final TreeNode node;
private VisitNodeAction(TreeNodeIterator iterator, TreeNode node) {
this.iterator = iterator;
this.node = node;
}
@Override
protected void compute() {
List<RecursiveAction> tasks = new LinkedList<RecursiveAction>();
for (TreeNode child : children) {
tasks.add(new VisitNodeAction(iterator, child));
}
invokeAll(tasks);
iterator.visit(node);
}
}
}
Some things that would need to be modified:
RecursiveAction
that is checked before processing the node and updated when a node is inactive, although this is not a very clean or functional route. ForkJoinPool
on every invocation of postorderParallel
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.