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.

Recommended Answers

All 3 Replies

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

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.