non-recursive free of binary tree
Hello,
I have a very deep binary tree to delete and I'm getting a stack overflow using the usual recursive solution so I'd like to get a handle on how to do a free of a binary tree without blowing up the stack with endless recursive calls.
A snippet of C-code that accomplishes this task would be very much appreciated, as would an explanation of the algorithm.
This is what I've been able to get so far, gleaned from a google search. It's easy to find the deepest point in the tree, but then I'm not sure as to how to back myself up the tree, deleting nodes as I go.
insert
Code:
node = *Tree;
while (*Tree) {
while (node) {
if (node->left) {
node = node->left;
} else if (n->right) {
node = node->right;
} else {
if (node == *Tree) {
*Tree = 0;
}
PGS_MEM_Free(node);
node = 0;
break;
}
}
}
Thanks for any help,
Catherine