Merge pull request #4 from LinlyBoi/Test

Finally
This commit is contained in:
Linly
2022-06-02 11:26:30 +02:00
committed by GitHub
4 changed files with 216 additions and 36 deletions

View File

@@ -7,5 +7,21 @@
</content>
<orderEntry type="inheritedJdk" />
<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>
</module>

View File

@@ -30,11 +30,15 @@ public class RecipeNode {
public void setParent(RecipeNode parent) {
this.parent = parent;
}
public RecipeNode getParent() {
return parent;
}
//Children
public void addChild(RecipeNode child) {
if (children == null) {
children = new ArrayList<RecipeNode>();
}
child.setParent(this);
children.add(child);
}
public ArrayList<RecipeNode> getChildren() {

View File

@@ -20,43 +20,108 @@ public class RecipeTree
this.root = root;
}
public void DFS(String value)
public RecipeNode DFS(String value)
{
DFS(root, value);
}
public void DFS(RecipeNode node, String value)
{
//Base case where we stop recursion
if(node == null)
{
System.out.println("Node not found, going back up");
return;
}
//Case where its actually found !!!!
if(node.getIngredient().equals(value))
{
System.out.println(node.getIngredient() + " " + node.getPortion());
}
//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
for(RecipeNode child : node.getChildren())
{
DFS(child, value);
}
return DFS(root, value);
}
public RecipeNode DFS(RecipeNode node, String value)
{
if (node == null)
{
return null;
}
if (node.getIngredient().equals(value))
{
return node;
}
if (node.getChildren() != null)
{
for (RecipeNode child : node.getChildren())
{
RecipeNode result = DFS(child, value);
if (result != null)
{
return result;
}
}
}
return null;
}
public void addNode(RecipeNode parent, RecipeNode child)
{
if (parent == null)
{
root = child;
}
else
} else
{
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()
{
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
View 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();
}
}