0

I am stuck with a leetcode problem on N-ary tree since last few hours. Could anyone please help me?

struct Node {
  int val;
  int numChildren;
  struct Node **children;
};

typedef struct Node node;

node *new_node(int numC, int data) {
    node *new_node = (node *)malloc(sizeof(node));

    if (new_node) {
        new_node->children = &new_node;
        new_node->numChildren = numC;
        new_node->val = data;
    }

    return new_node;
}

int main(void) {
    node *root = new_node(3, 1); 

    node *p1 = new_node(2, 3);
    node *p2 = new_node(0, 2);
    node *p3 = new_node(0, 4);
  
    root->children[0] = p1; //join p1 to root's 1st child
    root->children[1] = p2;
    root->children[2] = p3;

    node *p4 = new_node(0, 5);
    node *p5 = new_node(0, 6);
    
    root->children[0]->children[0] = p4; 
    root->children[0]->children[1] = p5; 

    printf("%d, \t %d, \t %d \n", root->children[0]->val, root->children[1]->val, root->children[2]->val);
    printf("%d, \t %d ", root->children[0]->children[0]->val, root->children[0]->children[1]->val);

    //preOrder (root);
}

I am expecting:

1, 3, 2, 4, 5, 6

The output is:

5, 6, 4
5, 6

my p4 and p5 pointer are overwriting p1 and p2. Why?

Chris
  • 26,361
  • 5
  • 21
  • 42
Amar
  • 15
  • 3

2 Answers2

0

One of the problems is here:

new_node->children = &new_node;

Here you save a pointer to the local variable new_node. Once the function returns, the life-time of that variable will end and the pointer you have saved will become invalid.

The next problem is here:

root->children[1] = p2;
root->children[2] = p3;

Arrays in C are not dynamic, you can't add elements to an array. If the pointer was valid, it would point only to a single Node structure, which means that the only valid index is 0.

Both of these issues, on their own, lead to undefined behavior.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Mistake 1:

 root->children[0] = p1; //join p1 to root's 1st child
    root->children[1] = p2;
    root->children[2] = p3;

code is assigning the pointer to the unallocated memory. assigned memory first using : (node *) calloc(root->numChildren, sizeof(node*)); then create a new node and connect the memory location via pointer: root->children = &temp_node1[0];

   int main (void)
    {
        node * root = (node *) malloc(sizeof(node));          
          root->children = NULL;
          root->numChildren = 3;
          root->val = 1;
        node * temp_node1 = (node *) calloc(root->numChildren, sizeof(node*));
        root->children = &temp_node1[0];
        
          node * temp_node =(node *) malloc(sizeof(node));
          temp_node->children = NULL;
          temp_node->numChildren = 2;
          temp_node->val = 3;
        root->children[0] = temp_node;
    
    
          node * temp_node2 = (node *) malloc(sizeof(node));
            temp_node2->children = NULL;
            temp_node2->numChildren = 0;
            temp_node2->val = 2;
        
         root->children[1] = temp_node2;
    
       node * temp_node3 = (node *) malloc(sizeof(node));
            temp_node3->children = NULL;
            temp_node3->numChildren = 0;
            temp_node3->val = 4;
        
         root->children[2] = temp_node3;
        
    
        //node * root = temp_node;
        node * temp_node4 = (node *) calloc(root->children[0]->numChildren, sizeof(node*));
        root->children[0]->children = &temp_node4[0];
    
       node * temp_node5 = (node *) malloc(sizeof(node));
            temp_node5->children = NULL;
            temp_node5->numChildren = 0;
            temp_node5->val = 5;
        
         root->children[0]->children[0] = temp_node5;
        
       node * temp_node6 = (node *) malloc(sizeof(node));
             temp_node6->children = NULL;
            temp_node6->numChildren = 0;
            temp_node6->val = 6;
    
          root->children[0]->children[1] = temp_node6;
          
        //preOrder (root);
    }
Amar
  • 15
  • 3