Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop the recursion completely when returning something

I do recursion to find a long value within a List with multiple children that also can have children.

following method:

public TaxonomyData getTaxonomyData(long taxId, List<TaxonomyData> TaxonomyTree, TaxonomyData output)
{
    //find taxid in the taxonomy tree list and return the taxonomydata

    foreach (TaxonomyData td in TaxonomyTree)
    {
        if (td.TaxonomyId == taxId)
        {
                output = td;
                //return td; => when doing a return here means I already found a match so it is not necessary to do all the recursion.
        }
        else if (td.Taxonomy.Length > 0)
        {
            getTaxonomyData(taxId, td.Taxonomy.ToList(), output);
        }
    }

    return output;
}

Is it possible when I do return td; (see commented row) that my whole recursion stops?

Thanks

like image 436
Ozkan Avatar asked Dec 21 '11 11:12

Ozkan


People also ask

Does a return statement stop a recursion?

Aside from that, if you've called your recursive function a given number of times, returning from the function would return you to the previous call of that function. A return statement won't stop all the prior recursive calls made from executing.

How do you stop a recursive function?

A recursive function is a function that makes a call to itself. To prevent infinite recursion, you need at least one branch (i.e. of an if/else statement) that does not make a recursive call. Branches without recursive calls are called base cases; branches with recursive calls are called recursive cases.

How do you stop a recursion in Python?

You can make use of the class. Initialize a variable self. flag=False in the init method, when you find the solution change self. flag = True and return None when self.

How do you stop a recursive function in C#?

You may be looking for something like: public object GetObjectValue(object obj, int condition) { if(condition > 10) { //exit from method return null; } else { IChild current = (IChild)obj //Do stuffs HERE return GetObjectValue(current. Parent, condition + 1); recursive call. } }


1 Answers

I suspect you want something like:

public TaxonomyData GetTaxonomyData(long taxId, IEnumerable<TaxonomyData> tree)
{
    foreach (TaxonomyData td in tree)
    {
        if (td.TaxonomyId == taxId)
        {
            return td;
        }
        else
        {
            // See if it's in the subtree of td
            TaxonomyData data = GetTaxonomyData(taxId, td.Taxonomy);
            if (data != null)
            {
                return data;
            }
        }
    }
    // Haven't found it anywhere in this tree
    return null;
}

Each return only returns one level, but by checking the return value in the else clause, we can return all the way up the stack when we find the right value.

The final result returned to the caller will be a null reference if it hasn't been found.

Note that I've removed the "output" parameter, which wouldn't have been effective anyway as it wasn't a ref parameter, and isn't as clear as just using the return value.

like image 176
Jon Skeet Avatar answered Oct 15 '22 18:10

Jon Skeet