I have a alpha beta tree program i'm supposed to do. I can't figure out how to do a multi node tree! I've been trying all day at this..
Here is my code so far.
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
typedef struct tree TREE_DATA;
struct tree {
int num;
vector<TREE_DATA*> element;
};
void create_tree(TREE_DATA *&curr_pos, TREE_DATA *&node, TREE_DATA *&new_element, TREE_DATA *&parent_ptr, int branch_fact, int tree_depth);
int main()
{
TREE_DATA *root = NULL;
TREE_DATA *node, *new_element, *curr_pos, *parent_ptr;
int branch_fact;
int tree_depth;
int random_num;
cout<<"enter branch factor"<<endl;
cin>>branch_fact;
cout<<"enter depth"<<endl;
cin>>tree_depth;
new_element = new TREE_DATA();
new_element->num=0;
for(int i=0; i<branch_fact; i++)
{
cout<<"top"<<endl;
node = new TREE_DATA();
cout<<"1"<<endl;
new_element->element.push_back(node);
cout<<"2"<<endl;
random_num = rand();
cout<<"3"<<endl;
new_element->element[i]->num=random_num;
cout<<"bottom"<<endl;
}
if (!root){root = new_element;}
curr_pos = root;
random_num = rand();
parent_ptr = root;
create_tree(curr_pos, node, new_element, parent_ptr, branch_fact, tree_depth);
curr_pos = root;
cout<<curr_pos->num<<endl;
cout<<curr_pos->element[0]->num<<endl;
return 0;
}
void create_tree(TREE_DATA *&curr_pos, TREE_DATA *&node, TREE_DATA *&new_element, TREE_DATA *&parent_ptr, int branch_fact, int tree_depth)
{
for(int j=0; j<branch_fact; j++)
{
curr_pos=curr_pos->element[j];
if(curr_pos->element.empty())
{
cout<<"here111"<<endl;
new_element = new TREE_DATA();
int random_num = rand();
new_element->num=random_num;
for(int i = 0; i<branch_fact; i++)
{
random_num = rand();
cout<<"here"<<endl;
node=new TREE_DATA();
new_element->element.push_back(new_element);
new_element->element[i]->num=random_num;
}
curr_pos=new_element;
curr_pos = parent_ptr;
}
}
}
I can't figure out how to properly build it.. I obviously have it started but no where close to being complete. I have to be able to have a depth of 6 if needed, and different branching factor chosen by user.
PLease HELP! Thanks.