I have this function that I hope to be a good alternative to the recursive (thus performance expensive) approach.
#define _free_all(node, yes_no) \
do { \
Tree *__i;
for (__i = node; __i; __i = __i->yes_no); \
free(; node != __i; __i = __i->parent) { \
free(__i->value); \
free(__i); \
} \
} while (0)
void Tree_destroy(tree)
Tree *tree;
{ /* Destroys a Tree object - Non Recursive */
if (!tree)
return;
_free_all(tree, yes);
_free_all(tree, no);
if (tree->parent) {
if (tree->parent->no == info)
tree->parent->no = NULL;
else
tree->parent->yes = NULL;
}
free(tree);
}
The two children of each parent are no
and yes
.
Are there any issues with this approach ?
Info
andTree
defined? – Olaf Dietsche Nov 14 '14 at 22:45_
and__
are reserved and best if avoided. – glampert Nov 15 '14 at 1:30free_all
macro either frees only left nodes or only right nodes, which means that any node in your tree that isn't on the left edge or right edge won't be freed. – JS1 Nov 15 '14 at 10:50