Question: Create breadth First Search similar to this code. (Ask user for start node and end node to find result) Here is the Depth First Seach

Create breadth First Search similar to this code. (Ask user for start node and end node to find result)

Here is the Depth First Seach code. (Please create depth First Search from this) apply similar code.

DepthFirstSearch.java

import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; import java.util.Stack; /**  * Depth first search in java  */ public class DepthFirstSearch { public static void main(String args[]) { Node a = new Node("A"); Node b = new Node("B"); Node c = new Node("C"); Node d = new Node("D"); Node e = new Node("E"); a.setLeftChild(c);a.setRightChild(e); b.setLeftChild(e);b.setRightChild(d); c.setLeftChild(a);c.setRightChild(d); d.setLeftChild(c);d.setRightChild(b); e.setLeftChild(a);e.setRightChild(b); if(computeDFS(a, d)) System.out.print("Path Found!"); } public static boolean compute(Node startNode, Node goalNode) { if(startNode == goalNode){ System.out.println("Goal Node Found!"); System.out.println(startNode); } Queue queue = new LinkedList<>(); ArrayList explored = new ArrayList<>(); queue.add(startNode); explored.add(startNode); while(!queue.isEmpty()){ Node current = queue.remove(); current.setExplored(); System.out.println(current); if(current.equals(goalNode)) { return true; } else  { ArrayList children = current.getChildren(); if(children.isEmpty()) return false; else  { for(int i = 0; i < children.size(); i++) { if(!children.get(i).isExplored()) queue.add(children.get(i)); } } } explored.add(current); } return false; } public static boolean computeDFS(Node startNode, Node goalNode) { if(startNode == goalNode){ System.out.println("Goal Node Found!"); System.out.println(startNode); } Stack s = new Stack(); ArrayList explored = new ArrayList<>(); s.push(startNode); explored.add(startNode); while(!s.isEmpty()){ Node current = s.pop(); current.setExplored(); System.out.println(current); if(current.equals(goalNode)) { return true; } else  { ArrayList children = current.getChildren(); if(children.isEmpty()) return false; else  { for(int i = 0; i < children.size(); i++) { if(!children.get(i).isExplored()) s.add(children.get(i)); } } } explored.add(current); } return false; } }

-------------------------------------------------------------------------------------------------------------------------------------

Node.Java

import java.util.ArrayList; /**  * The Node class represents a station  * a string representing the station's name.  * As well as an ArrayList of Nodes that will store  * any instantiated Nodes children.  */ public class Node { // A Unique Identifier for our Node  public String value; // An arraylist containing a list of Nodes that  // This Node is directly connected to - It's child Nodes.  Node leftChild; Node rightChild; boolean explored; public Node(String stationName){ this.value = stationName; } public void setLeftChild(Node left) { leftChild = left; } public void setRightChild(Node right) { rightChild = right; } public ArrayList getChildren(){ ArrayList childNodes = new ArrayList<>(); if(this.leftChild != null) { childNodes.add(leftChild); } if(this.rightChild != null) { childNodes.add(rightChild); } return childNodes; } // An auxiliary function which allows  // us to remove any child Nodes from  // our list of child Nodes.  public boolean removeChild(Node n){ return false; } public String toString() { return value; } public void setExplored() { this.explored = true; } public boolean isExplored() { return explored; } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!