270 likes | 287 Views
COSC 2006 Data Structures I Recursion II. Topics. More Recursive Examples Writing Strings backward Binary Search. Recursive Functions Criteria. 1. Function calls itself 2. Each call solves an simpler (smaller in size), but identical problem
E N D
Topics • More Recursive Examples • Writing Strings backward • Binary Search
Recursive Functions Criteria 1. Function calls itself 2. Each call solves an simpler (smaller in size), but identical problem 3. Base case is handled differently from all other cases and enables recursive calls to stop 4. The size of the problem diminishes and ensures reaching the base case
What Should be Considered in Recursive Solutions? • How to define the problem in terms of smaller problems of the same type? • How will each recursive call diminish the size of the problem? • What instance will serve as the base case? • As the problem size diminishes, will it reach the base case?
Example: Writing Strings Backward • Given: • A string of characters • Required: • Write the string in reverse order • Recursive Algorithm: • Idea: • Divide the string of length n into two parts; one of length 1 and the other of length n-1, • Exchange both strings and perform the same operation on the n-1 length string; • Stop when the length of the n-1 string becomes either 0 or 1 (base case)
WriteBackward ( S ) WriteBackward ( S minus last character ) Example: Writing Strings Backward • A recursive solution:
Example: Writing Strings Backward • Algorithm: First Draft WriteBackward1 (in s: string) if (string is empty) Do nothing - - Base case else { write the last character of S writeBackward1( S minus its last character) }
Writing a String Backward void writeBackward1(string s, int size) // --------------------------------------------------- // Writes a character string backward. // Precondition: The string s contains size characters, where size>= 0 // Postcondition: s is written backward, but remains unchanged. // --------------------------------------------------- { if (size > 0) // Enforcing the pre-condition { // write the last character System.out.print( s.substr (size-1, 1)); // write the rest of the string backward writeBackward1 (s, size-1); // Point A } // end if // size == 0 is the base case - do nothing } // end writeBackward
Figure 2-7a: Box trace of writeBackward("cat", 3) Example: Writing Strings Backward • Algorithm box trace:
Figure 2-7b: Box trace of writeBackward("cat", 3) Example: Writing Strings Backward • Algorithm box trace:
Figure 2-7c: Box trace of writeBackward("cat", 3) Example: Writing Strings Backward • Algorithm box trace:
Example: Writing Strings Backward • 2rd Option: (WriteBackward2) • Attach first character to the end WriteBackward2 (in s: string) if (string is empty) Do nothing - - Base case else { writeBackward2( S minus its first character) System.out.print( “About to write last character of string: “+S); write the first character of S } System.out.println(“Leave WriteBackward with string: “+S );
Writing a String Backward • Observations: • The 1-length string can be chosen either as the • first character from the n-length string • last character from the n-length string • Recursive calls to WriteBackward function use smaller values of Size • WriteBackward1 writes a character just before generating a new box • WriteBackward2 writes character after returning from recursive call
Example: Binary Search • Assumptions: • Array must be sorted • Size = size of the array • A[0] A[1] A[3] . . . A[Size-1] • Idea: • Divide the array into 3 parts • One half from A [First] to A [Mid - 1] • An element A [Mid] • Another half from A [Mid + 1] to A [Last] • Check if A[Mid] equals, less than, or greater than the value you are seeking
Example: Binary Search • Pseudocode binarySearch(in A: ArrayType, in Value: ItemType) { if (A is of size 1) Determine if A’s only item = Value // Base-case else { Find the midpoint of A Determine which half of A contains Value if (Value in first half of A) binarySearch(first half of A, Value) else binarySearch (second half of A, Value) } }
Binary Search Details How do you pass half an array? • first, last parameters How do you determine which half contains the value? • Split around a middle value array(mid) What should the base case(s) be? • Value found at mid • Array empty How to indicate the result, including failure? • Return index or negative number
Example: Binary Search • Two base cases • First > Last: • Value not found in original array • Search fails • Return either a Boolean value or a negative index • Value == A [Mid]: • Value found • Search succeeds • Return the index corresponding to Value • The array should be passed to the function by reference. It shouldn't be considered as part of the local environment.
Binary Search Code (abbreviated) binarySearch (int anArray[], int first, int last, int value) { int index; if (first > last) index = -1; else { int mid = (first + last)/2; if (value == anArray[mid]) index = mid; else if (value < anArray[mid]) index = binarySearch(anArray, first, mid-1, value); else index = binarySearch(anArray, mid+1, last, value); } return index;
Example: Binary Search • Using Run-Time Stack to trace • Box contents: • Value • First • Last • Mid • The array is not considered a part of the box. It is passed by reference
Value = 9 First = 2 Last = 2 Mid = (2+2)/2=2 Value = A[2] return 2 Value = 9 First = 0 Last = 7 Mid =(0+7)/2=3 Value < A[3] Value = 9 First = 0 Last = 2 Mid = (0+2)/2=1 Value < A[1] Value = 6 First = 0 Last = 7 Mid =(0+7)/2=3 Value < A[3] Value = 6 First = 0 Last = 2 Mid =(0+2)/2=2 Value < A[2] Value = 6 First = 2 Last = 2 Mid =(2+2)/2=2 Value < A[2] Value = 6 First = 2 Last = 1 First > Last return -1 Example: Binary Search • Example: A = <1, 5, 9, 12, 15, 21, 29, 31> • Searching for 9 • Searching for 6
Review • In a recursive method that writes a string of characters in reverse order, the base case is ______. • a string with a length of 0 • a string whose length is a negative number • a string with a length of 3 • a string that is a palindrome
Review • Which of the following is a precondition for a method that accepts a number n and computes the nth Fibonacci number? • n is a negative integer • n is a positive integer • n is greater than 1 • n is an even integer
Review • The midpoint of a sorted array can be found by ______, where first is the index of the first item in the array and last is the index of the last item in the array. • first / 2 + last / 2 • first / 2 – last / 2 • (first + last) / 2 • (first – last) / 2
Review • If the value being searched for by a recursive binary search algorithm is in the array, which of the following is true? • the algorithm cannot return a nonpositive number • the algorithm cannot return a nonnegative number • the algorithm cannot return a zero • the algorithm cannot return a negative number
Review • An array is a(n) ______. • class • method • object • variable
Review • For anArray = <2, 3, 5, 6, 9, 13, 16, 19>, what is the value returned by a recursive binary search algorithm if the value being searched for is 10? • –1 • 0 • 1 • 10
Review • A recursive binary search algorithm always reduces the problem size by ______ at each recursive call. • 1 • 2 • half • one-third