c - Binary tree, cannot free memory -
i've made binary tree (bst) , works fine, can't free allocated memory. nodes made of pointers (left, right, parent) , data, first name, name , phone number. first name , name static char arrays, , phone number of int , it's key tree. use function free memory:
void freeyourtree(node* x) { if (!x) return; freeyourtree(x->left); freeyourtree(x->right); free(x); x = null; }
but when use function , show tree inorder still shows records trash chars instead of first name. can't find what's wrong that. give insert function:
void insert(node** root) { node* x = (node*) malloc(sizeof(node)); if (x == null) { printf("malloc error"); return; } printf("put first name: "); scanf("%s", x->f_name); printf("put name: "); scanf("%s", x->s_name); printf("put number: "); scanf("%ld", &(x->number)); x->left = null; x->right = null; x->parent = null; node* = null; node* walk = *root; while (walk != null) { = walk; if (x->number < walk->number) { walk = walk->left; } else { walk = walk->right; } } x->parent = a; if (a == null) { *root = x; return; } if (x->number < a->number) { a->left = x; } else { a->right = x; } }
everything else works fine - showing inorder, showing max , min (everything recurrent), searching, counting nodes , tree height, looks free(x) freeing static first name char array, first variable in struct definition. here structure:
typedef struct wezel { char f_name[size]; char s_name[size]; long int number; struct wezel* left; struct wezel* right; struct wezel* parent; } node;
size defined 64. thank help, i'm stuck :(
Comments
Post a Comment