C - Nary Tree: error while appending a new child to the tree -
i'm developing nary tree in c using structure this:
typedef struct snarynode { int *data; // point node ’s data int n; // number of children struct snarynode **child; // child list } narynode; typedef narynode narytree;
i can't use list (as required project). i'm able create tree, when try append new child tree, "data" value of tree overwrited new value. example: create root data = 1
, want append new child data = 2
, @ end both root , new child have same "data" value, data = 2
. think problem not due mistake pointers management, since don't know i'm wrong, decided ask opinion. here full code:
lm_array2.h
#ifndef lm_array2_h #define lm_array2_h typedef struct snarynode { int *data; // point node ’s data int n; // number of children struct snarynode **child; // child list } narynode; typedef narynode narytree; typedef void (*datafreefunc) (const void *); void printarraytree (narytree*); narytree *createnode (int, int*); void freetree (narytree*, datafreefunc); int appendchild(narynode*, int*); void deletechild (narytree*, int, datafreefunc); #endif
lm_array2.c
#include <stdio.h> #include <stdlib.h> #include "lm_array2.h" void printarraytree (narytree *tree) { int d = *tree->data; printf("\n %d ", d); if (tree->n > 0) { int i; (i = 0; < tree->n; i++) { //printarraytree (tree->child[i]); int dd = *tree->child[i]->data; printf("\n %d", dd); } } else { printf("\n-----\n"); return; } } narytree *createnode ( int children , int *data) { // allocate space new narynode in memory narynode *node = (narynode*) calloc (1 , sizeof (narynode) ); // set contents of narynode appropriately node->data = data; node->n = children ; node->child = (narynode**) calloc ( children , sizeof (narynode*) ); // return node initialized return node; } void freetree (narytree *tree , datafreefunc dfree) { unsigned i; // don’ t try null pointer if ( tree == null) return; // free children recursively ( = 0; < tree->n; ++i ) freetree ( tree->child [ ] , dfree); // free child array free ( tree->child ); // free data if function provided if (dfree) dfree( tree->data); // , , free structure free ( tree ); } int appendchild(narynode *root , int *data) { // increment degree of node root->n++; // reallocate child array (n children in child array) root->child = (narynode**) realloc ( root->child , ( root->n)*sizeof (narynode*) ); // add newnode child array , increment degree root->child [ root->n - 1] = createnode (0 , data); // return index of child inserted return root->n - 1; } void deletechild (narytree *root , int idx , datafreefunc dfree) { unsigned i; // delete child freetree ( root->child [ idx ] , dfree); // remove defunct child ( = idx ; < root->n - 1; ++i ) root->child [ ] = root->child [ - 1]; // , , reallocate root->n--; root->child = (narynode**) realloc ( root->child , ( root->n)*sizeof (narynode*) ); }
main.c
#include <stdio.h> #include <stdlib.h> #include "lm_array2.h" #include "lm_array2.c" void menu(int*); int main() { narytree *atree = (narynode*)malloc(sizeof(narynode)); int choice = 0, value; printf("\nenter root value: "); scanf("%d", &value); atree = createnode (0, &value); { menu(&choice); switch (choice) { case 1: printf("\nenter new child value: "); scanf("%d", &value); //narytree *anode = createnode (0, value); appendchild(atree, &value); break; case 2: //printf("%d", *atree->data); printarraytree(atree); break; case 3: printf("\n\n***the end***\n\n"); break; } } while (choice > 0 && choice <= 2); system("pause"); return 0; } void menu(int *choice){ printf("\n"); printf("1. insert new child\n"); printf("2. print narytree\n"); printf("3. exit"); printf("\nenter choice: "); scanf("%d", &*choice); }
all code (except main) taken here --> click here
every or suggestion appreciated. in advance :)
your error in initialization of node: don't alloc memory buffer data. make point parameter, on stack , not on heap. createnode
function end, stack emptied , parameter (so data value) lost.
narytree *createnode ( int children , int *data) { // allocate space new narynode in memory narynode *node = (narynode*) calloc (1 , sizeof (narynode) ); // set contents of narynode appropriately // node->data = data; node->data = malloc(sizeof(node->data)); *node->data = *data; node->n = children ; node->child = (narynode**) calloc ( children , sizeof (narynode*) ); // return node initialized return node; }
or, more simpler:
typedef struct snarynode { // int *data; // point node ’s data int data; // contains node ’s data int n; // number of children struct snarynode **child; // child list } narynode;
and pass createnode
int data directly , not pointer:
narytree *createnode ( int children , int data) { // allocate space new narynode in memory narynode *node = (narynode*) calloc (1 , sizeof (narynode) ); // set contents of narynode appropriately // node->data = data; node->n = children ; node->child = (narynode**) calloc ( children , sizeof (narynode*) ); // return node initialized return node; }
Comments
Post a Comment