Difference between these two sets of codes

1.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
btree::node* btree::find(node *temp,int item)
{
             if(temp==NULL)
             return temp;
             
             else if(item>temp->data)
            {
                 if (temp->right==NULL)
                return temp;
                 else return find(temp->right,item);
            }
             
            else if(item<temp->data)
           {
                 if (temp->left==NULL)
                  return temp;
                 else return find(temp->left,item);
            }
             
             else return temp;
}


2.
1
2
3
4
5
6
7
8
9
10
11
12
13
btree::node* btree::find(node *temp,int item)
{
             if(temp==NULL)
             return temp;

             else if(item>temp->data)
             return find(temp->right,item);
                         
             else if(item<temp->data)
             return find(temp->left,item);
                        
             else return temp;
}


the 1st one works and the second one doesn't. What is the reason?
Last edited on
The temp that is returned by the first one is not NULL when the recursion of find unwraps.

The temp that is returned by the second one is NULL when the recursion of find unwraps.

Which means:
The first one will be 'connected'.

The second one will not be 'connected'.
Last edited on
Topic archived. No new replies allowed.