Merge pull request #3 from LinlyBoi/Sort-Topologique

Recursive Topological sort implemented (Presumably).
This commit is contained in:
Linly
2022-06-01 19:38:54 +02:00
committed by GitHub

View File

@@ -1,3 +1,5 @@
import java.util.Stack;
public class RecipeTree
{
@@ -62,4 +64,19 @@ public class RecipeTree
root.printNode();
}
}
public Stack<RecipeNode> sortTopologically(RecipeNode currentNode) {
Stack<RecipeNode> recipeStack = new Stack<>();
if(currentNode.getChildren() == null) {
recipeStack.push(currentNode);
return recipeStack;
} else {
for (RecipeNode childNode : currentNode.children) {
recipeStack.push(currentNode);
sortTopologically(childNode);
}
}
return recipeStack;
}
}