Please send questions to st10@humboldt.edu .

/**
 * RecursionExamples --- really just a motley collection of classic
 *                       recursion examples quickly cast into Java
 * 
 * @Sharon Tuttle
 * @last modified 4-1-03
 */
public class RecursionExamples extends JPFalt
{
    // is this enough of a constructor for executing examples
    // for methods that NEED to be non-static? (because I
    // want to use Java Power Tools' println()?)
    RecursionExamples()
    {}
    
    /*-----------------------------------------------------------
     Purpose: sum the Integers within a linked series of
              Cs132ListNodes containing Integer objects; return 0 
              if linked series of Cs132ListNodes is empty
     -----------------------------------------------------------*/
    static int sumList(Cs132ListNode currNode)
    {
	    // BASE case --- no recursion required!
 	    if (currNode == null)
	    {
		    return 0;
	    }

  	    // RECURSIVE case --- function calls itself with a provably "smaller" 
  	    // piece of the original input;
	    else
	    {
		    return ((Integer) currNode.getData()).intValue() + 
		           sumList(currNode.getNext());
	    }
    }
    
    //----------------------------------------------------------------------
    // Purpose: search for an int value within a SORTED array arr of int's;
    //          return index of element if found, return -1 otherwise
    //----------------------------------------------------------------------
    static int binarySearch(int arr[], int value)
    {
        return bSearch(arr, 0, arr.length-1, value);
    }

    //-----------------------------------------------------------------------
    // Purpose: auxiliary recursive function for binarySearch() to actually
    //          perform the recursive binary search for some value
    //-----------------------------------------------------------------------

    private static int bSearch(int arr[], int first, int last, int value)
    {
        int index;
        int mid;	// NOTE that this is an integer!

        // see if range contains any values --- a BASE CASE
        if (first > last)
        {
            index = -1;   // value is NOT in original array
        }

        else
        {
            // invariant: if value is in arr, 
            //            arr[first] <= value <= ar[last]
            mid = (first + last) / 2;	// INTEGER division --- floor!
        
            // have we found the value? (ANOTHER BASE case!)
            if (value == arr[mid])
            {
                // we FOUND the value!
                index = mid;	
            }

            else if (value < arr[mid])
            {
                // item is in "TOP" half remaining - RECURSIVE case
                index = bSearch(arr, first, mid-1, value);
            }

            else
            {
                // item is in "BOTTOM" half remaining - RECURSIVE case
                index = bSearch(arr, mid+1, last, value);
            }
        } // end else

        return index;
    } // end bSearch

    // Purpose: give instructions for solving the Towers of Hanoi
    //          puzzle for count disks, all initially on pole
    //          source, that you want to end of on pole dest,
    //          with pole spare available as a spare.
    void solveTowers(int count, char source, char dest, char spare)
	{
	    // BASE case --- if 1 disk, simply move it from source to destination
		if (count == 1)
		{
			println ("Move top disk on pole " + source
    		         + " to pole " + dest);
		}
		
		// RECURSIVE case
		else
		{
		    // solve for the TOP count-1 disks, using the SPARE as the
		    // destination...
			solveTowers(count-1, source, spare, dest);
			
			// now, move the count-th disk from the source to the 
			// destination 
			solveTowers(1, source, dest, spare);
			
			// now, solve for the count-1 disks currently on the
			// spare, moving THEM to the destination
			solveTowers(count-1, spare, dest, source);
		}
	}

}