简体   繁体   中英

Non-recursive destructor binary search tree USING A STACK

I was thinking of making a non recursive destructor using a stack of binary node pointers. Would this code run?

binaryNode* parent = root;
while (!empty())
{
  if (parent->left)
  {
    stack1.push(parent)
    parent = parent->left;
  }
  else if (parent->right)
  {
    stack1.push(parent)
    parent = parent->right;
  } else 
  {
    delete parent;
    parent = stack1.pop();
  }
}

I haven't completed the basic program so the code above has not been tested. I feel there should be nothing wrong with it. Although it has not been tested to run, I traced a binary search tree and it does just fine. Plus, I could not find a stack implementation with binary search tree traversals in stackoverflow.

To me, it looks like you have quite a few problems in the code there, but also it's hard to be sure what your intention was when you were implementing the code (ie which problems are issues with C++ and which would be issues with "design-level" pseudo code). One problem in the code is that the delete parent only frees the space occupied by the object that parent refers to. So when you pop the stack, you may end up re-executing the if (parent->left) branch, and therefore delete ing the same object again. And you only ever seem to delete leaf nodes.

The biggest problem I have is that the design of your algorithm isn't clear. The usual way to get a stack-based version of a recursive algorithm going is to get a recursive version going first (at least on paper), then factor out the recursion. In this case, your standard post-order traversal , implemented as a call to do_action , below, with node = root , is along the lines of

do_action(node)
    if node has left descendant
        do_action(left descendant)
    if node has right descendant
        do_action(right descendant)
    perform action on node

In your case, action is delete the node. Now, you can implement this without explicit recursion (and as shown by the answer to Non recursive Depth first search algorithm this can be quite straightforward for some tree traversal problems). However it arguable as to why you would want to in this case? Recursive calling uses the program's own stack, whereas you want to implicitly recurse using your own data structure instead. It is only in the case of removing tail end recursion that the advantages are black and white, because here recursion is really eliminated, rather than simulated with a stack.

If you do go down the route of removing recursion, it seems like your code might get pretty ugly: see http://leetcode.com/2010/10/binary-tree-post-order-traversal.html and Non-recursive post order traversal . It's possible your implicitly recursive code would perform worse in time and/or space than your original, explicitly recursive one. It's certainly likely to be more buggy!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM