diff --git a/Icing-On-Graph.iml b/Icing-On-Graph.iml index c90834f..1bb3d1a 100644 --- a/Icing-On-Graph.iml +++ b/Icing-On-Graph.iml @@ -7,5 +7,21 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/RecipeNode.java b/src/RecipeNode.java index 4f43cbf..36624d5 100644 --- a/src/RecipeNode.java +++ b/src/RecipeNode.java @@ -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(); } + child.setParent(this); children.add(child); } public ArrayList getChildren() { diff --git a/src/RecipeTree.java b/src/RecipeTree.java index 4ea8ec4..e961aa9 100644 --- a/src/RecipeTree.java +++ b/src/RecipeTree.java @@ -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 stack) + { + if (root.getChildren() != null) + { + for (RecipeNode child : root.getChildren()) + { + stack.push(child); + } + + + for (RecipeNode child : root.getChildren()) + { + sortTopology(child, stack); + } + } + + } + + public Stack sortTopology() + { + Stack stack = new Stack(); + stack.push(root); + sortTopology(root, stack); + return stack; + } + + public void printTree() { if (root != null) @@ -65,18 +130,5 @@ public class RecipeTree } } - public Stack sortTopologically(RecipeNode currentNode) { - Stack 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; - } } diff --git a/src/RecipeTreeTest.java b/src/RecipeTreeTest.java new file mode 100644 index 0000000..9c2d43f --- /dev/null +++ b/src/RecipeTreeTest.java @@ -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 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(); + + } +} \ No newline at end of file