int common(node *temp,int c,int d)
    int a=0;

    return 0;
    else if(temp->a==c || temp->a==d)
    return 1;


         //   break;
            return 0;
    return a;


this is my recusrion code. Actually, I want to end this recusrion process by adding some statement where i have written "//break". I have return 0; to end this, but it will recurse without any use. I have to end my recusrion process when a==2, no matter how many recusrion are already called , or how many recusrion are still to be called. I just want my control to transfer back to main (from where it is called up) when that condition become true.(a==2 here) . please help in this case.

thanks in advance.

Edited by I_m_rude

5 Years
Discussion Span
Last Post by I_m_rude

You have multiple problems but most of them stem from lines 10 and 16.

At line 16 you have detected that you have an end condition but rather than return an end condition flag you return the same value as you leaf condition at line 6. If your end condition is 2 you should return 2 so in fact you should probably always return a rather than conditionally returning it only when it is not 2.

Then at line 10 you do not check the return condition of one call to common before making the next one, so even if the first call returned the end condition you still carry on with recursion in the second call. It probably wants to change to something like

    a = common(temp->left,c,d);
    if (a != 2)
        a += common(temp->right,c,d);

There is a problem with that, if the first call returns 1 and the second call returns 2 the value of a is 3 which has no defined meaning in you value scheme

0 = leaf reach with no match
1 = match found in branch
2 = end condition reached    

So you will need to modify it to prevent a getting or at least returning the value 3


Barring truly hideous solutions like setjmp and longjmp, or terminating the program entirely with exit() or abort(), you really have no choice but to roll back up the recursive chain. This is one of the down sides of recursion, especially when dealing with things like ad hoc traversal over a binary search tree.


@banfa I have got something from your post, but i am not clear what exaclty you want to say. you saying that i should change or modify my code ? will you please rewrite the code in the way u thinking it so that my problem is solved ? thanks in advance.


@deceptikon sir, So that means there isn't any solution for this problem as you said it must roll back fully ? am i right or there is any solution except that abort(), exit() becuase i dnt want to exit from my program. it still must work after returning from this function to main().
please reply.


@deceptikon sir, So that means there isn't any solution for this problem as you said it must roll back fully ?

No offense, being fully aware that leading with "no offense" means something offensive will follow, but I find this back and forth of saying the same damn thing with slightly different words tiresome. I said something very clearly and with no ambiguity (ie. "you really have no choice but to roll back up the recursive chain"). You repeated it equally clearly and with full understanding (ie. "there isn't any solution for this problem as you said it must roll back fully"). Why should I repeat myself when it's obvious that you understood?


:( sir, actually I am stupid seriously. please don't mind that because I sometimes asks things when I got too excited. please really sorry sir! :( no, You dont need to repaet , it's my duty to understand it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.