27
COSC 2006 Data Structures I Recursion II

COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

Embed Size (px)

Citation preview

Page 1: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

COSC 2006 Data Structures IRecursion II

Page 2: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

Topics

More Recursive Examples

Writing Strings backward

Binary Search

Page 3: 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

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

Page 4: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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?

Page 5: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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)

Page 6: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

Example: Writing Strings Backward

A recursive solution:

WriteBackward ( S )

WriteBackward ( S minus last character )

Page 7: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 8: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 9: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

Example: Writing Strings Backward

Algorithm box trace:

Figure 2-7a: Box trace of writeBackward("cat", 3)

Page 10: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

Example: Writing Strings Backward

Algorithm box trace:

Figure 2-7b: Box trace of writeBackward("cat", 3)

Page 11: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

Example: Writing Strings Backward

Algorithm box trace:

Figure 2-7c: Box trace of writeBackward("cat", 3)

Page 12: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 13: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 14: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 15: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

Example: Binary Search

PseudocodebinarySearch(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 Valueif (Value in first half of A)

binarySearch(first half of A, Value)else

binarySearch (second half of A, Value)}

}

Page 16: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 17: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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.

Page 18: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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;

Page 19: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 20: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

Example: Binary Search Example:

A = <1, 5, 9, 12, 15, 21, 29, 31> Searching for 9

Searching for 6

Value = 9First = 0Last = 7Mid =(0+7)/2=3Value < A[3]

Value = 9First = 0Last = 2Mid = (0+2)/2=1Value < A[1]

Value = 9First = 2Last = 2Mid = (2+2)/2=2Value = A[2]return 2

Value = 6First = 0Last = 7Mid =(0+7)/2=3Value < A[3]

Value = 6First = 0Last = 2Mid =(0+2)/2=2Value < A[2]

Value = 6First = 2Last = 2Mid =(2+2)/2=2Value < A[2]

Value = 6First = 2Last = 1First > Lastreturn -1

Page 21: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 22: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 23: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 24: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 25: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

25

Review An array is a(n) ______.

class method object variable

Page 26: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

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

Page 27: COSC 2006 Data Structures I Recursion II. Topics More Recursive Examples Writing Strings backward Binary Search

27

Review A recursive binary search algorithm

always reduces the problem size by ______ at each recursive call. 1 2 half one-third