I'm trying to implement a N-ary tree in C. I know how to implement if I decide how many childs would have earlier. But my problem is, i want to make it dynamic (i want to resize the children and child size)
struct node
{
char * type;
struct node **child;
int children;
int id;
};
this is my struct and i want to do that with a pointer of pointer. How can i implement the insertion function?
struct node **toKeep = (*root)->child;
(*root)->children+=1;
(*root)->child =malloc(sizeof(struct node*)*
(*root)->child = toKeep;
size_t i;
for (i= 0; i < (*root)->children; i++)
{
if ((*root)->child[i] == NULL)
{
(*root)->child[i] = newNode(type, id);
printf("%d \n", (*root)->child[i]->id);
break;
}
}
i tried this to keep existing nodes but im unable to add more than 2 nodes ? What can cause the problem or is there a better way to write it?