Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Recursive: See recursive
Message
Information générale
Forum:
ASP.NET
Catégorie:
Autre
Divers
Thread ID:
01083365
Message ID:
01083795
Vues:
12
Rick,
thanks for your reply. The following code works for me:
private void CallRecursive(TreeView treeView)
{
   foreach (TreeNode n in treeView.Nodes)
   {
      if (RecursiveMethod(n))
      {
        break;
      }
   }
}


private bool RecursiveMethod(TreeNode treeNode)
{
   if (treeNode.Id == foo.Id)
   {
     treeNode.Selected = true;
     return true;
   }

   foreach (TreeNode tn in treeNode.Nodes)
   {
      if (RecursiveMethod(tn))
      {
        return true;
      }
   }
   return false;
}
I think I understand the point you are trying to make that the best way to "break out" for a recursive loop is to stop calling the recursive method, but in my case a level (or branch) can have multiple sub-levels (or branches). So for a "simple" case like calculating factorial where each branch has one and only one branch it is easy to get away from a return value.

The code above should describe exactly what I attempt to accomplish. I was twisting my mind for about 10 minutes over lunch to try to find a way to do it without a return value. (I'll probably wake up in the middle of the night with the solution<s>)

It is not that I am really confused, but if I pretend to be more people are willing to help <s>.


Einar

>>Rick,
>>I don't see how I can accomplish what I want without the bool return value.
>>Your example will always recurse through all nodes in the tree (please correct me if I am wrong because I find recursion very mind bogling).
>
>The problem I see is that you're using the return value of the recursive method as a flag that determines whether the recursion should continue. Lets put aside the boolean value for a moment.
>
>Lets go back to your original question: What is the best way to "break-out" of the recursion when my condition is true?
>
>The short answer is that the best way to breakout is to just stop calling the recursive function when the condition is met.
>
>Usually recursive methods return a value (you're usually looking for or calculating something). The classic example of this is a method that calculates the factorial of a number. I'm sure you know what a factorial is, but for the sake of other readers, the factorial of a number is the product of all positive integers less than or equal to the integer.
>
>So for example the factorial of 5 (usually written 5!) is
>5*4*3*2*1
>
>Now you could calculate this with a loop
>
>
>private int factorial (int nValue)
>{
>   int iReturnValue =1;
>
>   for(int n=nValue;n>1;n--)
>      iReturnValue *=n;
>
>   return iReturnValue
>}
>
>
>but another way is to use recursion:
>
>
>private int factorial(int nValue)
>{
>   if(nValue==1)
>      return 1;
>
>   return nValue*factorial(nValue-1);
>}
>
>
>
>When the condition is met (nValue==1) a concrete value is returned instead of returning the result of the a recursive call.
>
>If you're still confused, maybe you could describe the problem you are trying to solve?
>
>Hope this helps,
>Rick
Semper ubi sub ubi.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform