#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    [Function not working]


    Can somebody please help me? Why is this recursive fuction only working on one of the nodes? It should return a node of a binary tree, and it does, but only one side (either left node or right node). Here is the function:

    Code:
    Tree *searchProduct(Tree *T, char *name) {
    
    	if(T == NULL)
    		return (NULL);	
    
    	if(strcmp(T->name, name) == 0)
    		return (T);
    
    	T->rightchild = searchProduct(T->rightchild, name);
    	T->leftchild = searchProduct(T->leftchild, name);	
    
    }
    
    //And here is main:
    
    int main(int argc, char *argv) {
    
    
    	Tree *BA = NULL;
    	Tree *AB = build123();
    	BA = searchProduct(AB, "antenna");
    	printf(" %s \n", BA->name);	
    	return (0);
    
    }
    Need help badly!!!!
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    I'm guessing use the code tags message isn't getting through to you.

    No code tags, no help (yet).
    I suppose someone may say something eventually, but it might be a while.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Still Learning
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Montreal, Canada
    Posts
    55
    Rep Power
    39
    How do you know that I searches one side only?

    Create a running piece of code with simple sample data and show us the results that show the behavior you describe.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    Sorry about the code tags, i forgot!!!
    Next time I will insert them.

    Originally Posted by salem
    I'm guessing use the code tags message isn't getting through to you.

    No code tags, no help (yet).
    I suppose someone may say something eventually, but it might be a while.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    I know because it only shows the results on either the right child or the left, depending on the order of the recursive calls. If I change the data inserted in the "name" parameter it tells me segmentation fault.

    Originally Posted by admiraln
    How do you know that I searches one side only?

    Create a running piece of code with simple sample data and show us the results that show the behavior you describe.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0
    The code tags aren't working. I am not sure how to use them...

    Originally Posted by salem
    I'm guessing use the code tags message isn't getting through to you.

    No code tags, no help (yet).
    I suppose someone may say something eventually, but it might be a while.
  12. #7
  13. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,392
    Rep Power
    1871
    > T->rightchild = searchProduct(T->rightchild, name);
    > T->leftchild = searchProduct(T->leftchild, name);
    Look at what your previous statements return.
    You're trashing your tree with your search.

    Try something like
    Code:
    Tree *temp = searchProduct(T->rightchild, name);
    if ( temp != NULL ) return temp;
    return searchProduct(T->leftchild, name);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    9
    Rep Power
    0

    Smile [Solved]


    Thank you Salem!

    I had already spoken to my teacher before I read your reply and he told me exacly the same thing. Thanks anyway.

    Originally Posted by salem
    > T->rightchild = searchProduct(T->rightchild, name);
    > T->leftchild = searchProduct(T->leftchild, name);
    Look at what your previous statements return.
    You're trashing your tree with your search.

    Try something like
    Code:
    Tree *temp = searchProduct(T->rightchild, name);
    if ( temp != NULL ) return temp;
    return searchProduct(T->leftchild, name);

IMN logo majestic logo threadwatch logo seochat tools logo