Presentation is loading. Please wait.

Presentation is loading. Please wait.

COSC 2006 Data Structures I Recursion II

Similar presentations


Presentation on theme: "COSC 2006 Data Structures I Recursion II"— Presentation transcript:

1 COSC 2006 Data Structures I Recursion II

2 Topics More Recursive Examples Writing Strings backward Binary Search

3 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

4 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?

5 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)

6 Example: Writing Strings Backward
A recursive solution: WriteBackward ( S ) WriteBackward ( S minus last character )

7 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) }

8 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

9 Example: Writing Strings Backward
Algorithm box trace: Figure 2-7a: Box trace of writeBackward("cat", 3)

10 Example: Writing Strings Backward
Algorithm box trace: Figure 2-7b: Box trace of writeBackward("cat", 3)

11 Example: Writing Strings Backward
Algorithm box trace: Figure 2-7c: Box trace of writeBackward("cat", 3)

12 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 );

13 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

14 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

15 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) binarySearch (second half of A, Value) }

16 Binary Search Details Split around a middle value array(mid)
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

17 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.

18 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;

19 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

20 Example: Binary Search
Searching for 9 Searching for 6 Value = 9 First = 0 Last = 7 Mid =(0+7)/2=3 Value < A[3] Last = 2 Mid = (0+2)/2=1 Value < A[1] First = 2 Mid = (2+2)/2=2 Value = A[2] return 2 Value = 6 First = 0 Last = 7 Mid =(0+7)/2=3 Value < A[3] Last = 2 Mid =(0+2)/2=2 Value < A[2] First = 2 Mid =(2+2)/2=2 Last = 1 First > Last return -1

21 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

22 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

23 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

24 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

25 Review An array is a(n) ______. class method object variable

26 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 1 10

27 Review A recursive binary search algorithm always reduces the problem size by ______ at each recursive call. 1 2 half one-third


Download ppt "COSC 2006 Data Structures I Recursion II"

Similar presentations


Ads by Google