Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 212 More Recursion, Stacks, & Queues. Linear Recursion Test for the bases cases  At least one base case needs to be defined If not at a base case,

Similar presentations


Presentation on theme: "CSC 212 More Recursion, Stacks, & Queues. Linear Recursion Test for the bases cases  At least one base case needs to be defined If not at a base case,"— Presentation transcript:

1 CSC 212 More Recursion, Stacks, & Queues

2 Linear Recursion Test for the bases cases  At least one base case needs to be defined If not at a base case, recur  Every time method is executed, 1 recursive call made  Code may include multiple recursive calls But includes tests to choose between calls  Each possible recursive call should/must make progress towards a base case

3 LinearSum int LinearSum(int[] A, int n) n  n - 1 if n = 0 then return A[n] else return LinearSum(A, n) + A[n] fi Example recursion trace: LinearSum(A,5) (, (A,4) call A3)LinearSum call LinearSum(A,1) call (A,2)LinearSum call returnA[0] =4 return4+A[1] =4+3=7 return7+A[2] =7+6=13 return13+A[3] =13+2=15 callreturn15+A[4] =15+5=20

4 Removing Space From String String removeSpace(String str, int n) if n = str.getLength() then return “”; else if str.charAt(n) = ‘ ’ then return str.substring(0, n – 1) + removeSpace(str, n+1); else return str.substring(0, n) + removeSpace(str, n+1); fi

5 Creating Linear Recursive Algorithms 1. Define the base case(s) that are easily solved 2. Define recursive case Advance solution one step Assume previous result is available Evaluate current data point only Combine current result and previous result Return combined data

6 Writing Linear Recursive Methods Write test(s) for base case(s) and return these results  May need to define additional method parameters If multiple recursive steps defined, test which recursive step should be used  Define recursive step(s) to return combined current & recursively-derived result

7 Compute Exponential Function Could define power function recursively  E.g., compute exp(x, y) = x y recursively Base Case: exp(x,0) = 1 Recursive Case: exp(x, y) = x * x y-1 What should Big-Oh analysis consider the input? What is algorithm complexity?

8 Compute Exponential Function Base Case: exp(x,0) = 1 Recursive Case: exp(x, y) = x * x y-1 Could this recur infinitely? If yes, what base cases are we missing?

9 More Efficient Algorithm Base Case: exp(x, 0) = 1 Recursive Case 1: (y is odd) exp(x, y) = x * exp(x, (y-1)/2) 2 Recursive Case 2: (y is even) exp(x, y) = x * exp(x, y/2) 2 Write the code for this algorithm What is the complexity of this algorithm?

10 Recursive Exercise Write a recursive method that determines if a String is a palindrome (contains a word that is the same forward or backward like “otto”, “racecar”) A useful method of class String:  char charAt(int index)  returns the character at the (0-based) index within the String

11 Daily Quiz Write a method:  That runs in time O(1)  That runs in time O(n)  That runs in time O(n 2 )  That runs in time O(n 5 log n)  That runs in time O(2 n )


Download ppt "CSC 212 More Recursion, Stacks, & Queues. Linear Recursion Test for the bases cases  At least one base case needs to be defined If not at a base case,"

Similar presentations


Ads by Google