This is what i have so far, i have the areas commented where I need help
im not sure how to have to user input numbers into a tree and to have it print out the steps it takes to reach the goal
Header File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream>
#include <stack>
usingnamespace std;
bool recursive_depth(int);
bool is_goal(int);
void input_tree();
void print_tree();
int goal;
int root_node;
intconst NUM_SUCC = 4;
int tree[NUM_SUCC][4];
stack<int> mystack;
#include "search.h"
void main()
{
int state;
//input your tree and goal state, start state
//call recursive
input_tree();
recursive_depth(state);
print_tree();
}
bool is_goal(int state)
{
state = root_node;
if (state == goal) {
returntrue;
}else{
returnfalse;
}
}
bool recursive_depth(int state)
{
cout<<state<<" " << endl;
if (is_goal(state)) {
cout<<"goal found\n" << endl;
returntrue;
}
else{
if(!mystack.empty()){
int temp = mystack.top();
mystack.pop();
}
for(int i = (NUM_SUCC-1); i>=0; i--)
{
if (tree[state][i]!=0)
{
mystack.push(i);
}
}
}
while (!mystack.empty())
{
//i dont know what to put in this function the below code isnt right
mystack.pop();
state++;
recursive_depth(state);
}
returnfalse;
}
void print_tree() {
//im not sure how to print the tree
}
void input_tree() {
{
cout << "Input the root_node (start as 0)" << endl;
cin >> root_node;
cout << "Input the goal state " << endl;
cin >> goal;
//i ask for goal and root node but i have no idea how to have the user input the tree
}
Not sure why you need a separate function just to compare 2 numbers, when simply doing if (a == b) { ... } will suffice.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
//i ask for goal and root node but i have no idea how to have the user input the tree
for (int row = 0; row < NUM_SUCC; row++) {
for (int col = 0; col < NUM_SUCC; col++) {
cin >> tree[row][col];
}
}
//im not sure how to print the tree
for (int row = 0; row < NUM_SUCC; row++) {
for (int col = 0; col < NUM_SUCC; col++) {
cout << tree[row][col] << ' ';
}
cout << endl;
}
As for the rest of your code, I'm not sure what you are trying to accomplish. Is this a game tree you are building? If so, what algorithm are you trying to implement? I am guessing something like A*? But I'm not sure
1)write a function called bool is_goal() which takes a state and determines whether the state is a goal or not. The function should return true if the state is a goal, and false otherwise
2) represent the binary tree using a two dimensional array
3) write a function for inputting the array representation and another for printing
4) use a stack to implement the recursive depth-first function
5) have the program output the states in order they are visited until the goal state is reached
Again, what is the purpose of this? All these steps you have mentioned can be written without a goal in mind. What is your program supposed to do?
Assuming this is a game tree you are trying to build, the last step can be implemented by placing a parent pointer in each node. When the goal node is found, you can trace back to the parent by visiting the current node's parent and so forth.