c - Algorithm to store complete binary tree into an array -


i learning data structure right now, book know complete binary tree, store in array. cannot come algorithm it, nor transform array complete binary tree. can me in c? think question solved in recursion, traversal in binary tree, cannot it, nor solve in non-recursion method.

you need traversal in-order function calling pointer function.

edit: pointed out @peter skarpetis, can avoid using globals or statics passing parameter after pointer function:

struct container {     void *data;     int count; };  void tree_walk_recurse(const t_node *node, void (*func)(void *, void *), void *data) {     if (node->left) tree_walk_recurse(node->left, func, data);     func(node->data, data);     if (node->right) tree_walk_recurse(node->right, func, data); }  void tree_walk(const t_node *root, void (*func)(void *, void), void *data) {     if (root && func) tree_walk_recurse(root, func, data); }  void insert(void *data, void *ptr) {     struct data *array = ptr;      array->data[array->count++] = data; }  /* traverse in-order using insert */ struct container array;  array.data = malloc(sizeof(struct data) * n); array.count = 0; tree_walk(root, insert, &array); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -