### Summing and printing all values in a list

• Key to understanding recursion

• The key to understanding recursion is:

 Figure out how to solve the original problem using the solution of a smaller version of the problem

• Introductory example

• Problem description:

 Compute the sum of the values stored in a list

Example:

• Input list:

The answer is of course: 4 + 9 + 3 = 16

• Developing a recursive solution

• Pretend you (= me) need to solve this problem:

This problem is:

 ``` sum( head ) // sum all value in the list starting at head ```

• You make the following astute observation:

• If you can find someone (e.g., your friend Joe) to solve this smaller problem:

(This is the same list but without the first node !!!)

The solution of this smaller problem is:

 ``` smallerSol = 12 ```

• You can solve your problem using the smaller solution:

as follows:

• Recusive solution in pseudo code:

 ``` compute sum( list starting at head ): smallerSol = sum( list starting at head.next ); // Solve a smaller problem solution = head.value + smallerSol; // Use the solution of // a smaller problem to // solve the original prob. ```

• The other important part of recursion: the base case(s)

• Fact:

• To complete the recursive algorithm, we must find easy to solve cases (base cases) and handle the base cases separately

 Otherwise, the recursive method will not terminate --- it will keep calling itself forever

• How to find base cases:

• Find out when it is easy to solve the problem

Note:

 Each problem has its own set of base cases So I cannot give you any more details....

• The base case in the sum problem is:

• When the list is empty

• How to detect this case:

 ``` if ( head == null ) ```

• The answer of this base case: 0

• Writing a recursive algorithm in a Programming Language

• A recursive algorithm in general is as follows:

 ``` public static .... SolveProblem( n ) { variables: solution, smallerSol; /* =============================================== Detect and handle base cases FIRST ! =============================================== */ if ( base case detected ) { solution = solve base case; return solution; } else // Then followed by the recursive algorithm { smallerSol = SolveProb( n-1 ); // Recursion !!! solution = find solution using smallerSol; return solution; } } ```

Note:

 Recursive methods are almost always a class method (i.e.: static)

• The sum algorithm

• Java:

 ``` public static double sum( Node head ) { double solution, smallerSol; if ( head == null ) { solution = 0; // Solution for the base case return solution; // Return solution } else { smallerSol = sum( head.next ); // Solve smaller problem solution = head.value + smallerSol; // Solve my problem using // solution of smaller problem return solution; // Return solution } } ```

• How to use the method

• Because it is a static method, you must invoke it using the syntax:

 ``` ClassName.sum( .... ) ```

(I put the sum() inside the test program file testProg --- normally, you should put list manipulation methods inside the list class.

Since sum() is a class method, I can put sum() inside any class as long as I can access the head variable of the list to pass it to the sum() method....)

• Test program:

 ``` public static void main( String[] args ) { List myList = new List(); // I Use my old list insert program // to create a list myList.put(4.0); myList.put(9.0); myList.put(3.0); System.out.println("myList = " + myList); double s; /* ======================================================= This is how to use the sum() method: pass the head of the list as a parameter ======================================================= */ s = testProg.sum( myList.head ); System.out.println("s = " + s); } ```

Output:

 ``` myList = [4.0 , 9.0 , 3.0] s = 16.0 ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac testProg.java To run:          java testProg

• Printing all values in a list

• We can easily modify the sum algorithm to print the values in a list:

• Recall the sum algorithm:

 ``` public static double sum( Node head ) { double solution, smallerSol; if ( head == null ) { solution = 0; // Solution for the base case return solution; // Return solution } else { smallerSol = sum( head.next ); // Solve smaller problem solution = head.value + smallerSol; // Solve my problem using // solution of smaller problem return solution; // Return solution } } ```

• Instead of computing the sum, we can print the value head.value:

 ``` public static void print( Node head ) { if ( head == null ) { return; // Done printing } else { /* ======================================= Change sum() to print() ======================================= */ print( head.next ); // Print a smaller list // ** We changed the method name, so // ** we must use a different method name here ! System.out.print( head.value + " " ); // Print the first value // This is the only "real" change !!! return; // Done } } ```

This recursive program prints a list backwards:

 ``` myList = [1.0 , 2.0 , 4.0 , 9.0 , 3.0] print(myList): 3.0 9.0 4.0 2.0 1.0 ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac testProg2.java To run:          java testProg2

• We can fix the reversed order printing easily by switching the order of these 2 statements:

 ``` public static void print( Node head ) { if ( head == null ) { return; // Done printing } else { /* ======================================= Change sum() to print() ======================================= */ System.out.print( head.value + " " ); // Print the first value FIRST print( head.next ); // Print a smaller list // without the first value NEXT return; // Done } } ```

Output:

 ``` myList = [1.0 , 2.0 , 4.0 , 9.0 , 3.0] print(myList): 1.0 2.0 4.0 9.0 3.0 ```

• Example Program: (Demo above code)

How to run the program:

 Right click on link and save in a scratch directory To compile:   javac testProg3.java To run:          java testProg3