I am currently working on creating a tree (this is my learning process of creating a minimax tree and am starting towards minimax tree from here) with multiple nodes recursively.
Can anyone please determine if the algorithm is correct? From my perspective and couple of test (using print, count and gdb), I think I am creating the tree that I want, which looks like this:
The code for this tree:
#include <iostream>
#include <vector>
#define SIZE 3 // this is number of child nodes a node will have
using namespace std;
struct Node{
std::vector<Node*> v_node;
std::vector<int> my_vec;
int val;
};
class Tree{
Node *root;
int size;
int count;
void create_tree(Node *n, int size){
if(size == 0) return;
count++;
for(int i = 0; i < SIZE; i++){
Node * node = new Node;
node->v_node.resize(SIZE, nullptr);
node->my_vec.resize((size-1), n->my_vec[0]-1); // decrease val of vector by 1 in each level
node->val = i;
n->v_node[i] = node;
create_tree(node, size-1); // recursively call
}
}
public:
Tree(int s){
size = s;
root = new Node;
root->v_node.resize(SIZE, nullptr);
root->my_vec.resize(s, 36);
root->val = 0;
count = 0;
}
void ct(){
create_tree(root, size);
}
int get_node_count(){
return count;
}
};
int main(){
Tree t(4); //
t.ct();
std::cout << "Node count = " << t.get_node_count() << std::endl;
}
My core questions are:
Do you think using for loop inside recursive function a good idea?
I know that recursion have limits. When I do
ulimit -s
it gives me around 8000. This means that if I am using minimax for complex games like chess this might cause stack overflow. This gives me hint that this algorithm that I wrote is not good. Can you comment on this?I know I need to do the delete for releasing the resource, and for that I am planning to use
unique_pointer
later.