title: Tree iterator, can you optimize this any further?
body: <p>As a follow up to my original question about a small piece of this code I decided to ask a follow up to see if you can do better then what we came up with so far.</p>
  
  <p>The code below iterates over a binary tree (left/right = child/next ). I do believe there is room for one less conditional in here (the <code>down</code> boolean). The fastest answer wins!</p>
  
  <ol>
  <li>The <code>cnt</code> statement can be multiple statements so lets make sure this appears only once</li>
  <li>The <code>child()</code> and <code>next()</code> member functions are about 30x as slow as the hasChild() and hasNext() operations.</li>
  <li>Keep it iterative &lt;-- dropped this requirement as the recursive solution presented was faster.</li>
  <li>This is C++ code</li>
  <li>visit order of the nodes must stay as they are in the example below. ( hit parents first then the children then the 'next' nodes).</li>
  <li>BaseNodePtr is a boost::shared_ptr as thus assignments are slow, avoid any temporary BaseNodePtr variables.</li>
  </ol>
  
  <p>Currently this code takes 5897ms to visit 62200000 nodes in a test tree, calling this function 200,000 times.</p>
  
  <pre><code>void processTree (BaseNodePtr current, unsigned int &amp; cnt )
  {
      bool down = true;
  
      while ( true )
      {
          if ( down )
          {
              while (true) {
  
                  cnt++; // this can/will be multiple statesments
  
                 if (!current-&gt;hasChild()) break;
                 current = current-&gt;child();
              }
          }
  
          if ( current-&gt;hasNext() )
          {
              down = true;
              current = current-&gt;next();
          }
          else
          {
              down = false;
              current = current-&gt;parent();
              if (!current)
                  return; // done.
          }
      }
  }
  </code></pre>