title: search in a binary tree
body: <p>I have written the following function to search for a value in a binary tree storing integer values (the function is part of a larger program):</p>
  
  <pre><code>bool tree::search(int num)       //the function belongs to class 'tree'
  {
     node *temp=head;      //'head' is pointer to root node
  
     while(temp!=NULL)
     {
        if(temp-&gt;data==num)
           break;
  
        if(num&gt;temp-&gt;data)
           temp=temp-&gt;right;
  
        if(num&lt;temp-&gt;data)
           temp=temp-&gt;left;
     }
  
     if(temp==NULL)
        return false;
     else if(temp-&gt;data==num)
           return true;   
  }    
  </code></pre>
  
  <p>The problem is: when I search for a value present in the tree, it runs fine. But if I search for a value not present in the tree, the program just hangs, and I have to close it.
  One more thing - I know we can implement the search function recursively by passing node *temp as an argument, instead of declaring it inside, and I have done so which caused the program to run correctly, but I want to know what is the problem in the above code.</p>
  
  <p>I am giving the full program here, just in case it makes fault- finding easier( please note that I have written only two functions yet):</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  struct node
  {
  int data;
  node *left;
  node *right;
  };
  
  class tree
  {
  public:
      node *head;    //pointer to root
      int count;     //stores number of elements in tree
      tree();
      void addnode(int);
      void deletenode(int);
      bool search(int);
      int minimum();
      int maximum();
      void inorder();
      void preorder();
      void postorder();
      void printtree();
      int mthlargest();     //finds 'm'th largest element
      int mthsmallest();    //finds 'm'th smallest element
      void convert();       //converts binary tree to linked list
  };
  
  tree::tree()
  {
     head=NULL;
     count =0;
  }
  
  void tree::addnode(int num)
  {
     node *temp= new node;
     temp-&gt;data=num;
     temp-&gt;left=NULL;
     temp-&gt;right=NULL;
  
     node **ptr=&amp;head;          //double pointer
  
     while(*ptr!=NULL)
     {
        if(num&gt;(*ptr)-&gt;data)
           ptr=&amp;((*ptr)-&gt;right);
  
        if(num&lt;(*ptr)-&gt;data)
           ptr=&amp;((*ptr)-&gt;left);
     }
  
     *ptr=temp;
  }
  
  
  bool tree::search(int num)
  {
     node *temp=head;
  
     while(temp!=NULL)
     {
        if(temp-&gt;data==num)
           break;
  
        if(num&gt;temp-&gt;data)
           temp=temp-&gt;right;
  
        if(num&lt;temp-&gt;data)
           temp=temp-&gt;left;
     }
  
     if(temp==NULL)
        return false;
     else if(temp-&gt;data==num)
        return true;   
  }    
  
  
  
  
  int main()
  {
     tree ob;
     ob.addnode(2);
  
     ob.search(2);
  
     ob.search(3);
  
     ob.search(-1);
     ob.search(2);
     cout&lt;&lt;endl&lt;&lt;endl;
  
     system("pause");
     return 0;
  }               
  </code></pre>
  
  <p>Side note : I am using Dev C++ compiler and Windows 7 OS.</p>