title: Efficient Tree Traversal for simple search
body: <p>I am using a MIB parser I have written using PHP for a SNMP web interface project I am building.</p>
  
  <p>The node object is in the flowing structure:</p>
  
  <pre><code>class MibNode 
  {
      public $name;
      public $oid;
      public $type;
      public $status;
      public $description;
      public $canRead;
      public $canWrite;
      public $parent = null;
      public $children = array();
  }
  </code></pre>
  
  <p></p>
  
  <p>I am trying to get a node object by it's name.
  I have written a recursive function for that:</p>
  
  <pre><code>public function getNodeByName($name)
  {
      if($this-&gt;name === $name)
      {
          return $this;
      }
      else
      {
          foreach($this-&gt;children as $child)
          {
              $node = $child-&gt;getNodeByName($name);
              if($node != null)
                  return $node;
          }
          return null;
      }
  }
  </code></pre>
  
  <p>I have some performance issues with my project, so I have profiled the code, and saw that this function is called ~106,000 times.</p>
  
  <p>Is there any way I can reduce calls to this function, or increase performance?</p>
  
  <p>The full parser code is available <a href="https://github.com/AYECKa/WebInterface/blob/development/lib/MibParser.php" rel="nofollow">here</a>.</p>
  
  <p>edit: I have profiled the usage of the function using xdebug profile tool, the results are availible <a href="https://dl.dropboxusercontent.com/u/7493096/callgrind._WebInterface_parse_mibs_php.1430243227" rel="nofollow">here</a>. </p>