title: Count number of comparisons in binary search tree
body: <p>I would like to count the number of comparisons needed to find data in binary search tree.
  So far I have function that checks whether data is in the tree. I am confused about how to count comparisons because function is recursive. Do I need to use global variable for that? But in this case it will remain the same all the time and I cannot use it second time. Thanks!</p>
  
  <pre><code>#pragma warning(disable: 4996)
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  typedef int Titem;
  typedef struct node* Tpointer;
  typedef struct node{
      Titem item;
      Tpointer left;
      Tpointer right;
  }Tnode;
  typedef Tpointer TBinSTree;
  
  void initialize(TBinSTree* bintree);
  void insert_to_tree(TBinSTree* bintree, Titem item);
  int isInTree(TBinSTree* bintree, Titem item, int* counter);
  void print_tree(TBinSTree bintree);
  
  #include "bintree.h"
  
  void initialize(TBinSTree* bintree){
      *bintree = NULL;
  }
  void insert_to_tree(TBinSTree* bintree, Titem newitem){
      Tpointer newnode = *bintree;
      if (*bintree == NULL){// if tree is empty
          newnode = (Tpointer)malloc(sizeof(Tnode));
          newnode-&gt;left = NULL;
          newnode-&gt;right = NULL;
          newnode-&gt;item = newitem;
          *bintree = newnode;
      }
      else{
          if (newnode-&gt;item &gt; newitem)
              insert_to_tree(&amp;newnode-&gt;left, newitem);
          if (newnode-&gt;item &lt; newitem)
              insert_to_tree(&amp;newnode-&gt;right, newitem);
      }
  }
  int isInTree(TBinSTree* bintree, Titem item, int* counter){
      Tpointer current = *bintree;
      (*counter)++;
      if (current == NULL){// Base case == empty tree
          printf("Item %d is not in the tree\n", item);
          printf("Number of comparisons (case NULL) is: %d\n", *counter);
          return 1;
      }
      else {
          if (current-&gt;item == item){ //see if found here
              printf("Item %d is in the tree\n", item);
              printf("Number of comparisons (case node.item == item) is: %d\n", *counter);
              return 0;
          }
          else{ //otherwise recur down the correct subtree
              if (item &lt; current-&gt;item)
                  return isInTree(&amp;current-&gt;left, item, counter);
              else
                  return isInTree(&amp;current-&gt;right, item, counter);
          }
      }
  }
  void print_tree(TBinSTree bintree){ // inorder traverse
      if (bintree != NULL) {
          print_tree(bintree-&gt;left);
          printf("%d\n", bintree-&gt;item);
          print_tree(bintree-&gt;right);
      }
  }
  
  #include "bintree.h"
  #include &lt;time.h&gt;
  
  #define NUMBERS 10000
  
  int main(void){
  
      srand(time(NULL));
      TBinSTree tree;
      int item_to_search;
      int count = 0;
  
      initialize(&amp;tree);
      for (int i = 0; i &lt; NUMBERS; i++){
          insert_to_tree(&amp;tree, rand() % 10000);
      }
      for (int i = 0; i &lt; 3; i++){
          count = 0;
          printf("Input %d item to search: ", i+1);
          scanf("%d", &amp;item_to_search);
          isInTree(&amp;tree, item_to_search, &amp;count);
      }
      return 0;
  }
  </code></pre>
  
  <p><strong>UPD1</strong>
  I have updated code but now NULL case print statement is called twice.
  <strong>UPD2</strong>
  This is final version of code that works perfectly for my application.</p>