net.amygdalum.util.graph.ReversePostOrderTraversal Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of compilerutils Show documentation
Show all versions of compilerutils Show documentation
Utility classes needed for search and compiler applications
The newest version!
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 ReversePostOrderTraversal> implements Traversal {
@Override
public void traverse(GraphNode start) {
Set> visited = new HashSet<>();
Deque> ordered = new LinkedList<>();
List> reversePostOrdered = new LinkedList<>();
ordered.push(start);
while (!ordered.isEmpty()) {
GraphNode node = ordered.peek();
if (visited.contains(node)) {
ordered.pop();
reversePostOrdered.add(0, 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 : reversePostOrdered) {
visitGraphNode(node);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy