Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Breaking out of a recursion in java

Tags:

java

recursion

The recursion is sort of a 'divide and conquer' style, it splits up while getting smaller (Tree data structure), and I want it to break completely if a violation is found, meaning break all the recursive paths, and return true. Is this possible?

like image 823
Stasis Avatar asked May 13 '09 04:05

Stasis


People also ask

How do you break a recursive function?

One way to break out of a recursive function in Python is to throw an exception and catch that at the top level. Some people will say that this is not the right way to think about recursion, but it gets the job done.

How do you escape from recursion?

A first way to escape recursion is to evaluate everything then return 0 when the input list is empty. A second way to escape recursion is to evaluate everything but the last element, then either return the last thing or do something to the last thing and then return the result of that last function.

How do I stop recursive calls in Java?

A couple of tips for reducing extra function calls in recursion is to add a check for the base class before recursing. Another option is to handle more in each calls so that subsequent calls are unnecessary. Really though Java needs tail recursion which would just turn your recusrive method into a loop an...

Which statement is used to stop the recursive call?

The void exit () keyword immediately terminates the process or function.


2 Answers

No matter what you do, you are going to have to unwind the stack. This leaves two options:

  1. Magic return value (as described by one of the Toms)
  2. Throw an exception (as mentioned by thaggie)

If the case where you want things to die is rare, this may be one of those situations where throwing an exception might be a viable choice. And before everyone jumps down my throat on this, remember that one of the most important rules of programming is knowing when it's appropriate to break the rule.

As it turns out, I spent today evaluating the zxing library from google code. They actually use exception throws for a lot of control structures. My first impression when I saw it was horror. They were literally calling methods tens of thousands of times with different parameters until the method doesn't throw an exception.

This certainly looked like a performance problem, so I made some adjustments to change things over to using a magic return value. And you know what? The code was 40% faster when running in a debugger. But when I switched to non-debugging, the code was less than 1% faster.

I'm still not crazy about the decision to use exceptions for flow control in this case (I mean, the exceptions get thrown all the time). But it's certainly not worth my time to re-implement it given the almost immeasurable performance difference.

If your condition that triggers death of the iteration is not a fundamental part of the algorithm, using an exception may make your code a lot cleaner. For me, the point where I'd make this decision is if the entire recursion needs to be unwound, then I'd use an exception. IF only part of the recursion needs to be unwound, use a magic return value.

like image 89
Kevin Day Avatar answered Sep 17 '22 13:09

Kevin Day


You could return an error code, or modify some global variable so that each recursive instance knows to "kill itself".

Something of the sort.

int foo(bar){      int to_the_next;        if (go_recursive){             to_the_next = foo(whisky_bar);              if (to_the_next ==DIE) return DIE;       }        if (something_unexpected_happened) return DIE;        process;//may include some other recursive calls, etc etc } 
like image 23
Tom Avatar answered Sep 21 '22 13:09

Tom