0

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.

Edited by Nick Evan: Fixed formatting

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by jeffmt1234
0

oh, the little cout's here and there were just me debugging... just to see where things were hanging up at..

0

oh, the little cout's here and there were just me debugging... just to see where things were hanging up at..

Why not build a tree class something like:

#include <vector>

class tree
{
public:
tree();
tree (*parent);

add_child(tree * new_child);
set_parent(tree * parent);
tree * get_parent();
std::vector<tree *> get_children();
clear();

private:
tree * my_parent;
std::vector<tree *> my_children;

};

tree::tree()
: my_parent(NULL):
{
}

add_child(tree * new_child)
{
new_child->set_parent(this);
tree.push_back(new_child);
}

This is an incomplete example
as there is no depth

If this is just a small tree I would recommend
bool has_parent();
int calculate_depth(); //requires iteration or recursion

the alternative would require updating all of the childs' child nodes
when adding

0

Hmm I'll give it a try lol.. but still seems confusing. Thanks for the quick response.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.