Download presentation
Presentation is loading. Please wait.
Published byLeon Kelly Modified over 9 years ago
1
Recursion
2
Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm toward the base case and termination
3
General Form: public static void recursiveMethod() { if(base case) //something else { //something recursiveMethod(); }
4
Base Case Often times a numeric value like 0 or 1 A sentinel value End of file
5
Example 1 public static void stackWords() – The program will read in a list of words terminated with a period, and then print the list in reverse order Trace through a run of the program Notes: – Each time stackWords() is called, a new local variable is created – When an invocation of the method actually terminates, the program returns to complete the most recently invoked previous call
6
Example 2 public void drawTriangle(int n) Produces the following output for drawTriangle(3): *** ** * That’s all folks!
7
Notes/Terms/Exercises A method that has no pending statements following a recursive call is an example of tail recursion – Identify the examples we did that demonstrate tail recursion Identify the base case in each example Infinite recursion occurs if you have no base case, or the base case is never reached. This will eventually cause a StackOverflowError – Write a method to demonstrate infinite recursion
9
Independent Examples 1.Write a method that returns n! – Write two versions of this method, an iterative version and a recursive version 2.Write a method that returns the nth term in the fibonacci sequence – Write two versions of this method, an iterative version and a recursive version
10
Analyzing Recursive Algorithms Every recursive algorithm can be written iteratively Let’s examine Fibonacci’s recursive version: – How many calls to Fib() are required to find Fib(5)? – When a method call results in two or more other method calls, this is a red flag for an exponential algorithm (very inefficient!!)
11
Rules for Recursion 1.Avoid recursion for algorithms that involve large local arrays – too many recursive calls can cause memory overflow 2.Use recursion when it significantly simplifies code 3.Avoid recursion for simple iterative methods like factorial and Fibonacci 4.Recursion is especially useful for: – Branching processes – Divide and conquer algorithms like binary search (we’ll see this soon!)
12
Recursion in Two-D Grids Example Scenarios: – A game board from which you must remove pieces – A maze with walls and paths from which you must try to escape – White “containers” enclosed by black “walls” into which you must “pour paint” Generally, you will be given a starting point (row, col), and instructions on what to do The recursive solution generally looks something like this: Check that starting point is not out of range if(starting position satisfies some requirement) perform some action to solve problem recursiveCall(row + 1, col); recursiveCall(row - 1, col); recursiveCall(row, col + 1); recursiveCall(row, col - 1);
13
Example Picture a square grid that can contain either black or white cells. Two cells in the grid are part of the same “blob” if each is black and there is a sequence of moves from one cell to the other, where each move is either horizontal or vertical to an adjacent black cell. (See example). See Image class and implement the eraseBlob method using recursion
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.