Question: I need java code to implement this problem search for the DAN by BFS // Recursive Java program for level order traversal of Binary Tree
I need java code to implement this problem
search for the DAN by BFS
// Recursive Java program for level order traversal of Binary Tree
/* Class containing left and right child of current
node and key value*/
class Node
{
String data;
Node left, right;
Node medile;
public Node(String item)
{
data = item;
left = right = null;
}
Node() {
}
}
class BFS
{
// Root of the Binary Tree
Node root;
private Object left;
public BFS()
{
root = null;
}
/* function to print level order traversal of tree*/
void printLevelOrder()
{
int h = height(root);
int i;
for (i=1; i
printGivenLevel(root, i);
}
/* Compute the "height" of a tree -- the number of
nodes along the longest path from the root node
down to the farthest leaf node.*/
int height(Node root)
{
if (root == null)
return 0;
else
{
/* compute height of each subtree */
int lheight = height(root.left);
int mheight = height(root.medile);
int rheight = height(root.right);
/* use the larger one */
if (lheight > rheight)
return(lheight+1);
else return(rheight+1);
}
}
/* Print nodes at the given level */
void printGivenLevel (Node root ,int level)
{
if (root == null)
return;
if (level == 1)
System.out.print(root.data + " ");
else if (level > 1)
{
printGivenLevel(root.left, level-1);
printGivenLevel(root.medile, level-1);
printGivenLevel(root.right, level-1);
}
}
/* Driver program to test above functions */
public static void main(String args[])
{
BFS tree = new BFS();
tree.root= new Node(" ");
tree.root.left= new Node("D");
tree.root.medile=new Node("N");
tree.root.right=new Node("A");
tree.root.left.left=new Node("DD");
tree.root.left.medile=new Node("DN");
tree.root.left.right=new Node("DA");
tree.root.medile.left=new Node("ND");
tree.root.medile.medile=new Node("NN");
tree.root.medile.right=new Node("NA");
tree.root.right.left= new Node("AD");
tree.root.right.medile=new Node("AN");
tree.root.right.right=new Node("AA");
tree.root.left.left.left=new Node("DDD");
tree.root.left.left.medile=new Node("DDN");
tree.root.left.left.right=new Node("DDA");
tree.root.left.medile.left=new Node("DND");
tree.root.left.medile.medile=new Node("DNN");
tree.root.left.medile.right=new Node("DNA");
tree.root.left.right.left=new Node("DAD");
tree.root.left.right.right=new Node("DAN");
System.out.println("Level order traversal of binary tree is ");
tree.printLevelOrder();
System.out.println(" ");
Node n=new Node();
}
}
The example we will use is the case of a genetics professor searching for a name for her newborn baby boy - of course, it must only contain the letters D, N and A. The states in this search are strings of letters (but only Ds, Ns and As), and the initial state is an empty string. The goal state is "DAN" string. Also, the actions available are: i) add a 'D' to an existing string (ii) add an N' to an existing string (iii) add an 'A' to an existing string. Think of the search graphically: by making each state a node in a tree and each action a branch, we can think of the search progressing as movement from node to node along branches in the tree and we say that a node in a search space has been expanded if the state that node represents has been visited and searched from. add N add D add N add A NDNNNA AD(AN (AA DDA Implement the solution of the given problem using uninformed search strategies: o Breadth-first search I have this class
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
