net.amygdalum.util.graph.PostOrderTraversal Maven / Gradle / Ivy
package net.amygdalum.util.graph;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public abstract class PostOrderTraversal> implements Traversal {
@Override
public void traverse(GraphNode start) {
Set> visited = new HashSet<>();
Deque> ordered = new LinkedList<>();
List> postOrdered = new LinkedList<>();
ordered.push(start);
while (!ordered.isEmpty()) {
GraphNode node = ordered.peek();
if (visited.contains(node)) {
ordered.pop();
postOrdered.add(node);
continue;
}
visited.add(node);
GraphNode[] successors = node.getSuccessors();
for (int i = successors.length-1; i >= 0 ; i--) {
if (visited.contains(successors[i])) {
continue;
}
ordered.push(successors[i]);
}
}
for (GraphNode node : postOrdered) {
visitGraphNode(node);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy