Downlaod File

Zahra Abu Al-Saud
200800290
College of Computer Engineering and Science
COSC 4362 Artificial Intelligence
Dr. Ghassen Ben Brahim
Section 201
Name: Zahra A. Abu Al-Saud
ID: 200800290
Major: Computer Science
Homework #2
1|Page
Zahra Abu Al-Saud
200800290
COSC 4362 – Fall 2012
Homework # 2
1. Question 1
The task for this assignment is to implement either of the following algorithms:
a)
b)
c)
Backtrack algorithm for Trees
Modified version of Depth-First Search algorithm for Trees
Modified version of Breadth-First Search algorithm for Trees.
You may use any language of your choice including C++, Java, or C#. Submit your code as well as a snapshot
of your program execution in the digital drop box in the blackboard.
2|Page
Zahra Abu Al-Saud
200800290

Modified version of Breadth-First Search algorithm for Trees.
1. Code:
#include <iostream>
#include <queue>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
class Node
{
public:
char value;
Node *left;
Node *right;
};
void print_all (Node *root)
{
cout << root->value << endl;
if ( root->left )
{
print_all(root->left);
}
if ( root->right )
{
print_all(root->right);
}
}
class bfsNode{
public:
Node *treeNode;
string path;
};
bool bfs(Node *root, char target) {
bfsNode node;
node.treeNode = root;
cout << "Root value: " << root->value << endl;
queue<bfsNode> Q;
Q.push(node);
while(Q.size() > 0) {
bfsNode tmp = Q.front();
Q.pop();
if(tmp.treeNode->value == target) {
cout << "The Target " << tmp.treeNode->value << " is found
!" << endl;
cout << "The path : " << tmp.path << " " << target << endl;
return true;
}
3|Page
Zahra Abu Al-Saud
200800290
if(tmp.treeNode && tmp.treeNode->left) {
bfsNode newNode;
newNode.treeNode = tmp.treeNode->left;
newNode.path = tmp.path;
newNode.path += " ";
newNode.path += tmp.treeNode->value;
Q.push(newNode);
}
if(tmp.treeNode && tmp.treeNode->right) {
bfsNode newNode;
newNode.treeNode = tmp.treeNode->right;
newNode.path = tmp.path;
newNode.path += " ";
newNode.path += tmp.treeNode->value;
Q.push(newNode);
}
}
cout << "Target is not found !" << endl;
return false;
}
void create_tree()
{
Node x;
x.value = 'A';
x.left = NULL;
x.right = NULL;
Node b;
b.value = 'B';
b.left = NULL;
b.right = NULL;
x.left = &b;
Node c;
c.value = 'C';
c.left = NULL;
c.right = NULL;
x.right = &c;
Node h;
h.value = 'H';
h.left = NULL;
h.right = NULL;
c.left = &h;
Node i;
i.value = 'I';
i.left = NULL;
i.right = NULL;
c.right = &i;
Node m;
m.value = 'M';
m.left = NULL;
4|Page
Zahra Abu Al-Saud
200800290
m.right = NULL;
i.left = &m;
Node n;
n.value = 'N';
n.left = NULL;
n.right = NULL;
i.right = &n;
Node e;
e.value = 'E';
e.left = NULL;
e.right = NULL;
b.left = &e;
Node f;
f.value = 'F';
f.left = NULL;
f.right = NULL;
b.right = &f;
Node g;
g.value = 'G';
g.left = NULL;
g.right = NULL;
f.left = &g;
Node *root = &x;
cout << "This is the tree (left and right nodes):"<< endl;
print_all(root);
bfs(root, 'G');
}
int main()
{
cout << endl;
cout << "\tThis program is a modified version of Breadth-First Search
for Trees\n" << endl;
create_tree();
system("pause");
return 0;
}
2. Screenshot:
This is a representation of corresponding tree in the code:
A
B
E
C
F
G
H
I
M
N
5|Page
Zahra Abu Al-Saud
200800290
Screenshot of the target 'G' is found with the solution path.
Screenshot of the target 'M' is found with the solution path.
Screenshot of the target 'Z' is not found.
6|Page