com.link_intersystems.graph.tree.DepthFirstBottomUpTreeModelIterable Maven / Gradle / Ivy
Show all versions of lis-commons-graph Show documentation
package com.link_intersystems.graph.tree;
import java.util.*;
import java.util.stream.Stream;
/**
* Iterates through a tree model by depth first strategy, but returns the elements in reverse order of the branches.
*
* Assume the following tree structure. The number show the iteration order.
*
*
*
* +-(2)--(1)
* |
* (7)-+-(3)
* |
* | +-(4)
* | |
* +-(6)-+
* |
* +-(5)
*
*
* You can {@link #setLeavesOnly(boolean)} to true and it will only iterate the leaves: 1, 4, 5.
*
* @author René Link {@literal }
*/
public class DepthFirstBottomUpTreeModelIterable extends AbstractTreeModelIterable {
private boolean leavesOnly;
public DepthFirstBottomUpTreeModelIterable(TreeModel treeModel, T rootElement) {
super(treeModel, rootElement);
}
public void setLeavesOnly(boolean leavesOnly) {
this.leavesOnly = leavesOnly;
}
@Override
protected Iterator createIterator(TreeModel treeModel, T rootElement) {
Stack stack = new Stack<>();
stack.push(rootElement);
Set childrenResolved = Collections.newSetFromMap(new IdentityHashMap<>());
Map