title: Efficient delete of a tree data structure
body: <p>I have a tree data structure :</p>
  
  <ul>
  <li>The root of the tree has no parents ans no previous siblings (it can
  have next siblings). </li>
  <li>Every node has a unique parent.</li>
  <li>Every node has a pointer to it's next and previous siblings, children and parent.</li>
  </ul>
  
  <p>This tree data structure is being populated with millions of nodes. When I delete a tree having 
  an enormous amount of nodes, a stack-overflow exception is thrown. The data structure works well when the number of nodes is relatively small or when I build in release mode.</p>
  
  <p>This is the destructor of a node :</p>
  
  <pre><code>    Entity::~Entity(void)
      {
          Entity* child = NULL;
  
          if (firstChild != NULL)
              child = firstChild-&gt;getNextSibling();
  
          while(child != NULL)
          {
              delete child-&gt;getPreviousSibling();
              child-&gt;setPreviousSibling(NULL);
  
              child = child-&gt;getNextSibling();
          }
  
          if (lastChild != NULL)
              delete lastChild;
  
          if (isRoot())
          {
              if (nextSibling != NULL)
              {
                  nextSibling-&gt;setPreviousSibling(NULL);
                  delete nextSibling;
              }
          }
      }
  </code></pre>
  
  <p>One can implement a non recursive algorithm to traverse the tree and delete all it's nodes. </p>
  
  <p>Could you suggest an efficient postorder traversal algorithm to delete a non binary tree ? </p>