@@ -7,5 +7,21 @@
|
|||||||
</content>
|
</content>
|
||||||
<orderEntry type="inheritedJdk" />
|
<orderEntry type="inheritedJdk" />
|
||||||
<orderEntry type="sourceFolder" forTests="false" />
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library name="JUnit5.8.1">
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
|
||||||
|
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
@@ -30,11 +30,15 @@ public class RecipeNode {
|
|||||||
public void setParent(RecipeNode parent) {
|
public void setParent(RecipeNode parent) {
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
}
|
}
|
||||||
|
public RecipeNode getParent() {
|
||||||
|
return parent;
|
||||||
|
}
|
||||||
//Children
|
//Children
|
||||||
public void addChild(RecipeNode child) {
|
public void addChild(RecipeNode child) {
|
||||||
if (children == null) {
|
if (children == null) {
|
||||||
children = new ArrayList<RecipeNode>();
|
children = new ArrayList<RecipeNode>();
|
||||||
}
|
}
|
||||||
|
child.setParent(this);
|
||||||
children.add(child);
|
children.add(child);
|
||||||
}
|
}
|
||||||
public ArrayList<RecipeNode> getChildren() {
|
public ArrayList<RecipeNode> getChildren() {
|
||||||
|
|||||||
@@ -20,43 +20,108 @@ public class RecipeTree
|
|||||||
this.root = root;
|
this.root = root;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DFS(String value)
|
public RecipeNode DFS(String value)
|
||||||
{
|
{
|
||||||
DFS(root, value);
|
return DFS(root, value);
|
||||||
}
|
}
|
||||||
public void DFS(RecipeNode node, String value)
|
|
||||||
|
public RecipeNode DFS(RecipeNode node, String value)
|
||||||
{
|
{
|
||||||
//Base case where we stop recursion
|
if (node == null)
|
||||||
if(node == null)
|
|
||||||
{
|
{
|
||||||
System.out.println("Node not found, going back up");
|
return null;
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
//Case where its actually found !!!!
|
if (node.getIngredient().equals(value))
|
||||||
if(node.getIngredient().equals(value))
|
|
||||||
{
|
{
|
||||||
System.out.println(node.getIngredient() + " " + node.getPortion());
|
return node;
|
||||||
}
|
}
|
||||||
//Depth first search we take first child then we take the first child in the first child then we take the first child in the first child in the first child
|
if (node.getChildren() != null)
|
||||||
for(RecipeNode child : node.getChildren())
|
|
||||||
{
|
{
|
||||||
DFS(child, value);
|
for (RecipeNode child : node.getChildren())
|
||||||
|
{
|
||||||
|
RecipeNode result = DFS(child, value);
|
||||||
|
if (result != null)
|
||||||
|
{
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public void addNode(RecipeNode parent, RecipeNode child)
|
public void addNode(RecipeNode parent, RecipeNode child)
|
||||||
{
|
{
|
||||||
if (parent == null)
|
if (parent == null)
|
||||||
{
|
{
|
||||||
root = child;
|
root = child;
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
{
|
{
|
||||||
parent.addChild(child);
|
parent.addChild(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void addNode(String parent, RecipeNode child)
|
||||||
|
{
|
||||||
|
RecipeNode parentNode = findNode(root, parent);
|
||||||
|
if (parentNode == null)
|
||||||
|
{
|
||||||
|
System.out.println("Parent node not found");
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
addNode(parentNode, child);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private RecipeNode findNode(RecipeNode root, String parent)
|
||||||
|
{
|
||||||
|
if (root == null)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
if (root.getIngredient().equals(parent))
|
||||||
|
{
|
||||||
|
return root;
|
||||||
|
}
|
||||||
|
for (RecipeNode child : root.getChildren())
|
||||||
|
{
|
||||||
|
RecipeNode found = findNode(child, parent);
|
||||||
|
if (found != null)
|
||||||
|
{
|
||||||
|
return found;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void sortTopology(RecipeNode root, Stack<RecipeNode> stack)
|
||||||
|
{
|
||||||
|
if (root.getChildren() != null)
|
||||||
|
{
|
||||||
|
for (RecipeNode child : root.getChildren())
|
||||||
|
{
|
||||||
|
stack.push(child);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
for (RecipeNode child : root.getChildren())
|
||||||
|
{
|
||||||
|
sortTopology(child, stack);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Stack<RecipeNode> sortTopology()
|
||||||
|
{
|
||||||
|
Stack<RecipeNode> stack = new Stack<RecipeNode>();
|
||||||
|
stack.push(root);
|
||||||
|
sortTopology(root, stack);
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void printTree()
|
public void printTree()
|
||||||
{
|
{
|
||||||
if (root != null)
|
if (root != null)
|
||||||
@@ -65,18 +130,5 @@ public class RecipeTree
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
108
src/RecipeTreeTest.java
Normal file
108
src/RecipeTreeTest.java
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Stack;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class RecipeTreeTest
|
||||||
|
{
|
||||||
|
|
||||||
|
@org.junit.jupiter.api.Test
|
||||||
|
void setRoot()
|
||||||
|
{
|
||||||
|
RecipeTree tree = new RecipeTree();
|
||||||
|
RecipeNode root = new RecipeNode("root", 1);
|
||||||
|
tree.setRoot(root);
|
||||||
|
assertEquals(root, tree.getRoot());
|
||||||
|
}
|
||||||
|
|
||||||
|
@org.junit.jupiter.api.Test
|
||||||
|
void DFS()
|
||||||
|
{
|
||||||
|
RecipeTree tree = new RecipeTree();
|
||||||
|
RecipeNode root = new RecipeNode("root", 1);
|
||||||
|
RecipeNode child1 = new RecipeNode("child1", 1);
|
||||||
|
RecipeNode child2 = new RecipeNode("child2", 1);
|
||||||
|
RecipeNode child3 = new RecipeNode("child3", 1);
|
||||||
|
RecipeNode child4 = new RecipeNode("child4", 1);
|
||||||
|
RecipeNode child5 = new RecipeNode("child5", 1);
|
||||||
|
RecipeNode child6 = new RecipeNode("child6", 1);
|
||||||
|
RecipeNode child7 = new RecipeNode("child7", 1);
|
||||||
|
RecipeNode child8 = new RecipeNode("child8", 1);
|
||||||
|
tree.setRoot(root);
|
||||||
|
tree.addNode(root, child1);
|
||||||
|
tree.addNode(root, child2);
|
||||||
|
tree.addNode(root, child3);
|
||||||
|
tree.addNode(child1, child4);
|
||||||
|
tree.addNode(child1, child5);
|
||||||
|
tree.addNode(child2, child6);
|
||||||
|
tree.addNode(child3, child7);
|
||||||
|
tree.addNode(child3, child8);
|
||||||
|
assertEquals(child1, tree.DFS("child1"));
|
||||||
|
assertEquals(child2, tree.DFS("child2"));
|
||||||
|
assertEquals(child3, tree.DFS("child3"));
|
||||||
|
assertEquals(child4, tree.DFS("child4"));
|
||||||
|
assertEquals(child5, tree.DFS("child5"));
|
||||||
|
assertEquals(child6, tree.DFS("child6"));
|
||||||
|
assertEquals(child7, tree.DFS("child7"));
|
||||||
|
assertEquals(child8, tree.DFS("child8"));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@org.junit.jupiter.api.Test
|
||||||
|
void sortTopologically()
|
||||||
|
{
|
||||||
|
RecipeTree tree = new RecipeTree();
|
||||||
|
RecipeNode root = new RecipeNode("root", 1);
|
||||||
|
RecipeNode child1 = new RecipeNode("child1", 1);
|
||||||
|
RecipeNode child2 = new RecipeNode("child2", 1);
|
||||||
|
RecipeNode child3 = new RecipeNode("child3", 1);
|
||||||
|
RecipeNode child4 = new RecipeNode("child4", 1);
|
||||||
|
RecipeNode child5 = new RecipeNode("child5", 1);
|
||||||
|
RecipeNode child6 = new RecipeNode("child6", 1);
|
||||||
|
RecipeNode child7 = new RecipeNode("child7", 1);
|
||||||
|
RecipeNode child8 = new RecipeNode("child8", 1);
|
||||||
|
tree.setRoot(root);
|
||||||
|
tree.addNode(root, child1);
|
||||||
|
tree.addNode(root, child2);
|
||||||
|
tree.addNode(root, child3);
|
||||||
|
tree.addNode(child1, child4);
|
||||||
|
tree.addNode(child1, child5);
|
||||||
|
tree.addNode(child2, child6);
|
||||||
|
tree.addNode(child3, child7);
|
||||||
|
tree.addNode(child3, child8);
|
||||||
|
Stack<RecipeNode> stack = tree.sortTopology();
|
||||||
|
while(!stack.isEmpty())
|
||||||
|
{
|
||||||
|
System.out.println(stack.pop().getIngredient());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@org.junit.jupiter.api.Test
|
||||||
|
void addNode()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
@org.junit.jupiter.api.Test
|
||||||
|
void printTree()
|
||||||
|
{
|
||||||
|
RecipeTree tree = new RecipeTree();
|
||||||
|
RecipeNode root = new RecipeNode("root", 1);
|
||||||
|
RecipeNode child1 = new RecipeNode("child1", 1);
|
||||||
|
RecipeNode child2 = new RecipeNode("child2", 1);
|
||||||
|
RecipeNode child3 = new RecipeNode("child3", 1);
|
||||||
|
RecipeNode child4 = new RecipeNode("child4", 1);
|
||||||
|
RecipeNode child5 = new RecipeNode("child5", 1);
|
||||||
|
RecipeNode child6 = new RecipeNode("child6", 1);
|
||||||
|
tree.setRoot(root);
|
||||||
|
tree.addNode(root, child1);
|
||||||
|
tree.addNode(root, child2);
|
||||||
|
tree.addNode(root, child3);
|
||||||
|
tree.addNode(child1, child4);
|
||||||
|
tree.addNode(child2, child5);
|
||||||
|
tree.addNode(child3, child6);
|
||||||
|
tree.printTree();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user