+#if 1
+#ifdef DEBUG_TREE
+ LOG("newest code for tree destructor (stack based) activating...");
+#endif
+
+ // this version of delete doesn't recurse; it just iterates instead,
+ // which avoids the massive recursive depth of the original approach.
+ // it does use a stack object however, but that will use main memory,
+ // which is usually in greater supply than stack/heap space.
+
+ stack<tree *> sleestak; // accumulates nodes that we still need to clean up.
+
+ // iterate across this node first, to feed the stack.
+ // we feed it in reverse branch order, so that the first branch will be dealt with first (popped off stack first).
+ while (branches()) {
+ // get our current last branch...
+ tree *bran = branch(branches() - 1);
+ // pull that branch off the tree (non-destructively)...
+ prune(bran);
+ // and add that branch to the stack.
+#ifdef DEBUG_TREE
+ LOG(a_sprintf("tree dtor: adding child %p on stack.", bran));
+#endif
+ sleestak.push(bran);
+ }
+
+ // now iterate across our stack until we have dealt with every node in it.
+ while (sleestak.size()) {
+
+ tree *popper = NULL_POINTER;
+ sleestak.acquire_pop(popper);
+#ifdef DEBUG_TREE
+ LOG(a_sprintf("tree dtor: popped a tree %p off stack.", popper));
+#endif
+
+ // familiar scheme below; push last branch first, then we'll pop first branch first.
+ while (popper->branches()) {
+ tree *bran = popper->branch(popper->branches() - 1);
+ popper->prune(bran);
+#ifdef DEBUG_TREE
+ LOG(a_sprintf("tree dtor: inner, adding child %p on stack.", bran));
+#endif
+ sleestak.push(bran);
+ }
+
+ // now the popper gets cleaned up; this should be totally safe since all its kids
+ // have been pruned and put into the stack.
+#ifdef DEBUG_TREE
+ LOG(a_sprintf("tree dtor: about to zap a tree %p.", popper));
+#endif
+ WHACK(popper);
+#ifdef DEBUG_TREE
+ LOG("tree dtor: whacked that tree.");
+#endif
+ }