Algorithms for traversing unordered tree I have been doing some programming 'exercises'.  I'm trying to come up with the most efficient algorithm for tree traversal.

Consider the following function signature: 

CNode * CNode::find(int number) const


where CNode structure is not organized as a binary search tree (otherwise obviously a faster algorithm would have been used - not involving the entire tree traversal), so all the nodes have to be inspected.

I have implemented 3 algorithms so far:

Stack-based search

CNode * CNode::findStackBased(int number)
{
  Node *tmp = this;
  stack&lt;CNode*&gt; onStack;
  while (tmp != NULL || !onStack.empty())
  {
    if (tmp == NULL)
    {
      tmp = onStack.top();
      onStack.pop();
    }

    if (tmp-&gt;number == number)
    {
      return tmp;
    }

    if (tmp-&gt;right)
    {
      onStack.push(tmp-&gt;right);
    }

    tmp = tmp-&gt;left;
  }

  return NULL;
}


Recursive search

CNode * CNode::findRecursion(int number)
{
  if (this-&gt;number == number)
  {
    return this;
  }

  Node * result = NULL;
  if (this-&gt;left != NULL &amp;&amp; (result = left-&gt;findRecursion(data)))
  {
    return result;
  }

  if (this-&gt;right != NULL)
  {
    return this-&gt;right-&gt;findRecursion(data);
  }

  return NULL;
}


Walking along the edge

CNode * CNode::findWalkAlongTheEdge(int number)
{
  Node *tmp = this;
  while (tmp != NULL)
  {
    if (tmp-&gt;number == number)
    {
      return tmp;
    }

    if (tmp-&gt;left != NULL)
    {
      tmp = tmp-&gt;left;
      continue;
    }
    else if (tmp-&gt;right != NULL)
    {
        tmp = tmp-&gt;right;
        continue;
    }
    else
    {
      while (1)
      {
        if (tmp-&gt;parent-&gt;left == tmp)
        {
          if (tmp-&gt;parent-&gt;right != NULL)
          {
            tmp = tmp-&gt;parent-&gt;right;
            break;
          }
        }
        else
        {
          if (tmp-&gt;parent == this)
          {
            return NULL;
          }

          tmp = tmp-&gt;parent;
        }
      } 
    }
  }
  return NULL;
}


Each of the above have have some advantages/drawbacks, but performance wise 'walking along the edge is the winner so far(out of the 3 given above). 

My question is: 

Do you see any way to optimize it further (recursive implementation already benefits from the tail recursion optimization, at least gcc seems to optimize it this way)?
Do you see any way to optimize it further


I don't see why you have (i.e. don't think you should have) while (tmp != NULL) at the top:


On entry you know that this != NULL
You verify that the result won't be NULL before you execute the statement before each continue or break


Removing that would make it slightly faster.



Also I don't understand ...

    if (tmp-&gt;parent-&gt;left == tmp)
    {
      if (tmp-&gt;parent-&gt;right != NULL)
      {
        tmp = tmp-&gt;parent-&gt;right;
        break;
      }
    }


... because if (tmp-&gt;parent-&gt;left == tmp) and (tmp-&gt;parent-&gt;right == NULL) then you do nothing and therefore stay inside while(1) forever. So perhaps either your code is wrong, or one of those if conditions is unecessary.



Apart from the two items above, the existing code looks simple? Multi-threading, using a more-optimizing compiler, or trying to write in assembly are possibilities but not clever. Good optimizations sometimes come from changing the data layout.

For example, one faster algorithm might be to change the way in which the tree is contained in memory: use a custom allocator or similar, to ensure that nodes are physically contained in a contiguous array or vector.

Walking the whole tree would then mean, simply, iterating through the contiguous array: which is pretty quick.

If you do that, it would be easier to change your left, right, and parent pointers to indexes (into the array): because then you wouldn't need to change them if you reallocate (move) the array.

Alternatively keep the nodes in random heap-allocated memory but store the numbers themselves in a contiguous array. Instead of a number member, nodes would have an index member which says which slot in the array contains their number. Making the numbers contiguous (without extraneous tree-node pointers) might be the fastest way to iterate them.