@@ -1,7 +1,10 @@
|
|||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
public class RecipeNode {
|
public class RecipeNode {
|
||||||
private String ingredient; // Ingredient's name
|
private String ingredient; // Ingredient's name
|
||||||
private double portion; // Portion size of ingredient
|
private double portion; // Portion size of ingredient
|
||||||
RecipeNode pointer; // To point at parent node
|
RecipeNode parent; // To point at parent node
|
||||||
|
ArrayList<RecipeNode> children; // To point at children nodes
|
||||||
|
|
||||||
// Accessors and Mutators for fields (Except pointer's)
|
// Accessors and Mutators for fields (Except pointer's)
|
||||||
RecipeNode(String ingredient, double portion) {
|
RecipeNode(String ingredient, double portion) {
|
||||||
@@ -24,4 +27,29 @@ public class RecipeNode {
|
|||||||
public double getPortion() {
|
public double getPortion() {
|
||||||
return portion;
|
return portion;
|
||||||
}
|
}
|
||||||
|
public void setParent(RecipeNode parent) {
|
||||||
|
this.parent = parent;
|
||||||
|
}
|
||||||
|
//Children
|
||||||
|
public void addChild(RecipeNode child) {
|
||||||
|
if (children == null) {
|
||||||
|
children = new ArrayList<RecipeNode>();
|
||||||
|
}
|
||||||
|
children.add(child);
|
||||||
|
}
|
||||||
|
public ArrayList<RecipeNode> 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 + "]";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,62 @@
|
|||||||
public class RecipeTree
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user