diff --git a/src/RecipeNode.java b/src/RecipeNode.java index 9e3b9cf..4f43cbf 100644 --- a/src/RecipeNode.java +++ b/src/RecipeNode.java @@ -1,7 +1,10 @@ +import java.util.ArrayList; + public class RecipeNode { private String ingredient; // Ingredient's name private double portion; // Portion size of ingredient - RecipeNode pointer; // To point at parent node + RecipeNode parent; // To point at parent node + ArrayList children; // To point at children nodes // Accessors and Mutators for fields (Except pointer's) RecipeNode(String ingredient, double portion) { @@ -24,4 +27,29 @@ public class RecipeNode { public double getPortion() { return portion; } + public void setParent(RecipeNode parent) { + this.parent = parent; + } + //Children + public void addChild(RecipeNode child) { + if (children == null) { + children = new ArrayList(); + } + children.add(child); + } + public ArrayList getChildren() { + return children; + } + public void printNode() { + System.out.println(ingredient + " " + portion); + if (children != null) { + for (RecipeNode child : children) { + child.printNode(); + } + } + } + @Override + public String toString() { + return "RecipeNode [ingredient=" + ingredient + ", portion=" + portion + "]"; + } } diff --git a/src/RecipeTree.java b/src/RecipeTree.java index d6e0fe9..f08bf1a 100644 --- a/src/RecipeTree.java +++ b/src/RecipeTree.java @@ -1,3 +1,62 @@ public class RecipeTree + { + private RecipeNode root; + + public RecipeTree() + { + root = null; + } + + public RecipeNode getRoot() + { + return root; + } + + public void setRoot(RecipeNode root) + { + this.root = root; + } + + public void DFS(String value) + { + DFS(root, value); + } + public void DFS(RecipeNode node, String value) + { + if(node == null) + { + System.out.println("Node not found"); + return; + } + if(node.getIngredient().equals(value)) + { + System.out.println(node.getIngredient() + " " + node.getPortion()); + } + for(RecipeNode child : node.getChildren()) + { + DFS(child, value); + } + } + + + public void addNode(RecipeNode parent, RecipeNode child) + { + if (parent == null) + { + root = child; + } + else + { + parent.addChild(child); + } + } + + public void printTree() + { + if (root != null) + { + root.printNode(); + } + } }