jeffmt1234 0 Newbie Poster

I can't seem to wrap my head around what I'm doing wrong. I'm trying to create an N-ARY tree. I'm pretty sure I'm close. But the nodes are filling wrong. I have used cout to output the nodes as they fill and sometimes the nodes are filling with the same number 3 diff times and others not. Not quite sure what's going on. The alpha beta search works fine.. Just struggling with the creation of the N-ary tree.

Please help point out what I'm doing wrong. THANKS!

#include <iostream>
#include <vector>

using namespace std;

typedef struct tree TREE_DATA;

struct tree
{
    int num;
	vector<TREE_DATA*> element; 
};

void create_tree(int level, TREE_DATA *&node, int tree_depth, int branch_fact);
int alpha_beta(int level, TREE_DATA *&node, int tree_depth, int branch_fact, 
			   int alpha, int beta, int array2[]);
int max(int parent_value, int nextVal);
int min(int parent_value, int nextVal);



int main()
{
   TREE_DATA *root;
   TREE_DATA *new_element;

   int level=0;
   int tree_depth=3;
   int branch_fact=3;
   int array2[1200];
   int alpha = -999999; 
   int beta  =  999999;
   int returnVal=0;
    
   new_element = new TREE_DATA();
   new_element->num=0;
 
   root = new_element;
   create_tree(level, root, tree_depth, branch_fact);

   returnVal=alpha_beta(level, root, tree_depth, branch_fact, alpha, beta, array2);
   cout<<endl;
   cout<<"Move chosen at top of tree: "<<returnVal<<endl;
   cout<<endl;
   cout<<"Values of each moves chosen:"<<endl;
      
      for(int i=0; i < 1200; i++)
      {
        if(array2[i]>-1)
	cout<<array2[i]<<" ";
      }
  
  return 0;
}



void create_tree(int level, TREE_DATA *&node, int tree_depth, int branch_fact)
{
   TREE_DATA  *new_element;
   static int counter = 0;
	
   
	
   if(level == tree_depth)
   {
	   /*
       new_element = new TREE_DATA();
	   new_element->num=counter;
	   node->element.push_back(new_element);
       counter++;
	   	   cout<<"inside if"<<endl;
	   cout<<node->num<<endl;
	   */
    }
   else
   {
     for(int i=0; i<branch_fact; i++)
     {
       
       new_element = new TREE_DATA();
	   
	   new_element->num=counter;
       
	   node->element.push_back(new_element);
	   cout<<"inside for"<<endl;
	   cout<<node->num<<endl;
	   cout<<"counter"<<endl;
	   cout<<counter<<endl;
       counter++;
	   	  
       create_tree(level+1, node->element[i], tree_depth, branch_fact);
       
      
     }
   
   }
}

int heuristicEval(TREE_DATA *&node)
{
    int num=0;
    num = node->num;
    return (num);
}


int min(int parent_value, int nextVal)
{
    if(parent_value < nextVal)
      return (parent_value);
    else 
      return (nextVal);
}

int max(int parent_value, int nextVal)
{
    if(parent_value > nextVal)
      return (parent_value);
    else
      return (nextVal);
}


int alpha_beta(int level, TREE_DATA *&node, int tree_depth, int branch_fact, 
			   int alpha, int beta, int array2[])
{
   
   static int count=0;  //counter for array
	
   int returnVal, nextVal;
	
   if(level == tree_depth)
   {
     returnVal = heuristicEval(node);
   }
   else
   {
     for(int i=0; i<branch_fact; i++)
     {
       if(alpha > beta)
       { 
         cout<<"Cutoff occured at node: "<<node->num<<" level: "<<level<<endl;
         cout<<"Alpha = "<<alpha<<" and Beta = "<<beta<<endl;
         break;
       }
       nextVal=alpha_beta(level+1, node->element[i], tree_depth, branch_fact, alpha, beta, array2);
       
       if(level%2 != 0)
         beta = min(beta, nextVal);
       else
         alpha = max(alpha, nextVal);
     }
     if(level%2 != 0)
       returnVal = beta;
     else
       returnVal = alpha;
   }
	
   array2[count]=returnVal;
   count++;
   return returnVal;
}