Depth First & Breadth First Search Program

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

 ``1234567891011121314151617`` ``````#include #include using namespace std; bool recursive_depth(int); bool is_goal(int); void input_tree(); void print_tree(); int goal; int root_node; int const NUM_SUCC = 4; int tree[NUM_SUCC][4]; stack mystack; ``````

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970`` ``````#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) { return true; }else{ return false; } } bool recursive_depth(int state) { cout<=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); } return false; } 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 }``````
bump
Not sure why you need a separate function just to compare 2 numbers, when simply doing `if (a == b) { ... }` will suffice.

 ``123456789101112131415161718`` ``````//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
the goals of my program are

implement depth first search using recursion

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
Last edited on
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.
Topic archived. No new replies allowed.