Search code examples
javarecursionconceptual

Java How to define a fundamental operation in a recursive method?


In a recursive method, we will have to set up some base cases. Can we define these base cases as the fundamental operations of the method? As far as I know, fundamental operations are the core functions of the method, which means every time the method is running must pass through these functions. (Please let me know if I am wrong)

Another question is if I got a if statement in the base cases, for example,

if (a != b && b != c){}

Will it be count as 1 or 2 fundamental operations? Cause it is checking 2 part of things: a != b and b != c in a single if statement.

This is quite confused.

One more thing is : I am not sure will this kind of code is suitable or not:

Recursive method()

Base case:

XXXXXXX XXXXXXX XXXXXXX

Recursive case:

// Should I move this part to the base case?

if(conditions){

    return (Recursive method() || Recursive method());

// ********************************************************

    else if(conditions){

         do something;

    return Recursive method();

Cause I think base cases are just used to defined a exact value when meeting the conditions. Therefore I leave this part in the recursive case. I am just not so sure about this.

I am not asking for answer for coursework, just to ensure the concept is correct or not. So I didn't put my algorithm on here. Sorry if this makes you cant understand my question. I will try my best to explain.

Thanks.


Solution

  • Based on the definitions provided, a base case or terminating case is a condition which stops the recursion calls.

    The definition of a fundamental operation is a bit unclear from the question and I am honestly getting lost in here. But from my understanding it's or it should be a set of operations which are done in a function regardless the base case. Link to the definition would help!

    Let's have a short example:

    /**
     * Let's assume the result is not obvious here regardless it's a nth triangular number.
     * Added there a System.out though.
     */
    public void calc(int i) {
        System.out.println(i);
    
        if (i == 0)
            return 0;
        return i + calc(i - 1);
    }
    

    The only case how to stop the recursion evaluating the first condition if (i == 0) to true. Meaning that this condition represents the base case. When i has any other value than zero, the recursion continues.

    In the example, the only operation which is done regardless the outcome of the method is printing the value i. Thus that's the only fundamental operation of the method. (Based on the definition, an evaluation of a condition might and might not be considered an operation since it's not changing any value nor has any side effects, like printing.)

    Can we define these base cases as the fundamental operations of the method?

    Generally no, as these represent different situations. The first one defines a case when recursion is stopped and the second one what's always done. Thus if you combine them, you end up with a function which always stops the recursion.

    For the following case they could be the same though.

    public void calc(int i) {
        return i + 1;
    }