title: Searching a Binary Search Tree, using counter for iterations
body: <p>I  have a function I'm using that searches for a number in a binary search tree. I use an external int that can be incremented by the function with each iteration, since the function calls itself if the number is not the root.</p>
  
  <p>I'm just learning binary search trees, but I know there's a better way of doing it that I'm overlooking... using an int method that returns the counter for example, but I can't figure it out...</p>
  
  <p>edit:
  I know the number will definitely be in the BST, I just need to see how effective BST searching is vs. searching through an array for the same number.</p>
  
  <pre><code>// This external int can be incremented by the searchBST function 
  // to keep track of iterations
  
  int bcounter = 0;
  
  // Search Binary Search Tree function
  
  node* searchBST(node ** tree, int num){
  
  bcounter++;
  
  if(num &lt; (*tree)-&gt;data) {
      searchBST(&amp;((*tree)-&gt;left), num);
  }
  else 
      if(num &gt; (*tree)-&gt;data) {
      searchBST(&amp;((*tree)-&gt;right), num);
  }
  else 
      if(num == (*tree)-&gt;data) {
      return *tree;
  }
  }
  </code></pre>