Recursive Topological sort implemented (Presumably).

This commit is contained in:
2022-06-01 19:36:40 +02:00
parent 7d436ad9a2
commit 6d2cb16343

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;
}
}