1

I am trying to build a multi-way tree in C. I've got stuck on allocation memory for childrens. I have a vector which contains the fathers of each node. Here is my code:

#define MAX_CHILDS 10

int t[10] = {1, 2, 4, 1, -1, 3, 2, 1, 0, 4};
NODE *root;
NODE *v[MAX_CHILDS];

//add children for specified node
void ADD_REF(int i) {
    v[i]->children[v[i]->child_count] = v[t[i]];
    v[i]->child_count++;
}

//creates the tree
NODE *T1(int n, int *t) {
    int root = 0;
    for (int i = 0; i < n; i++) {
        v[i] = (NODE *) malloc(sizeof(NODE));
        v[i]->info = i;
        v[i]->child_count = 0;
        v[i]->children = (NODE **) malloc(sizeof(NODE)); // I think the problem is here
    }

    for (int i = 0; i<n; i++) {
        if (t[i] == -1)
            root = i;
        else 
            ADD_REF(i);
    }

    return v[root];
}

void main() {
    root = T1(MAX_CHILDS, t);   
    print_tree(root, 0); // prints the tree
}

Here is the structure of the NODE:

typedef struct NODE {
    int info;                   
    int child_count;            
    struct NODE **children; 
} NODE;

I am not sure exactly if the problem is at the memory allocation. With my logic it should work.

Cœur
  • 37,241
  • 25
  • 195
  • 267
smotru
  • 433
  • 2
  • 8
  • 24
  • 1
    What's the actual question? – Rahul Shardha May 14 '14 at 17:26
  • I don't know exactly how to dynamically allocate memory to a vector of nodes since I don't know how much nodes will be. – smotru May 14 '14 at 17:29
  • Why not? `info` & `child_count` are ints and `children` is just a pointer. – Rahul Shardha May 14 '14 at 17:30
  • I'm new to C but 2 things I've noticed. You have one pointer in every node. Hence it's gonna behave like a linked list not a multi way tree. Second, why are you using two *'s. Shouldn't you be only using one? – Rahul Shardha May 14 '14 at 17:32
  • 2x* represents a vector of type NODE – smotru May 14 '14 at 17:35
  • Hmmm. Did not know that. I guess you need to decide how many way tree it is. It's gotta have SOME limit. Either user defined or predefined in your program. – Rahul Shardha May 14 '14 at 17:37
  • So each node knows his childs in the children vector. Only difference between this and a binary search tree should be the dynamically allocation of memory for childs (as far as I understand) – smotru May 14 '14 at 17:43
  • Why not use a fixed number (like I said above) either user defined or preset (2 for binary). Then when allocating memory, have a preset for the node** i.e. don't make it a vector. make it an array of size number. Your existing code should work. – Rahul Shardha May 14 '14 at 17:45
  • I can't use a predefined size on this. It is required for my task. – smotru May 14 '14 at 17:55
  • ok. Then prompt the user for input. Like `Enter a number to make a "number" way tree`. Then assign that number as the size of the array – Rahul Shardha May 14 '14 at 17:56

1 Answers1

0

I've found my error. The memory allocation was ok. The problem was at adding new children. I've added children for the current node instead of adding children to it's parent.

This was the solve:

void ADD_REF(int i) {
    v[t[i]]->children[v[t[i]]->child_count] = v[i];
    v[t[i]]->child_count++;
}
smotru
  • 433
  • 2
  • 8
  • 24