chapter 7 recursion recursive methods recursion in two- dimensional grids recursive helper method...

10
Chapter 7 Recursion Recursive methods Recursion in two-dimensional grids Recursive helper method Analysis of recursive algorithms

Upload: roland-harmon

Post on 31-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms

Chapter 7 Recursion

Recursive methodsRecursion in two-dimensional grids

Recursive helper

methodAnalysis of recursive algorithms

Page 2: Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms

Recursive Methods• A recursive method is a method that calls

itself. • Terminating case (Base Case) – Stops the

recursive calls

Page 3: Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms

Examplepublic class WordPlay{

public static void stackWords(){String words = IO.readString();if(word.equals(“.”))System.out.println();else stackWords();System.out.println(word);}

public static void main(String args []){System.out.println(“Enter list of word”);System.out.println(“Final word should be “.”);stackWords();}

}Enter hold my hand.

Page 4: Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms

Answer

.handmyhold

***Note: Computer must go back to finish each method call which is to output word.The first time the method actually terminates, the program returns to complete the most recently invoked previous call.

Page 5: Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms

General Form of a Recursive Method

• Base Case or termination condition that causes the method to end. Usually 1 or 0 or end of file is reached.

• A nonbase case whose actions move the algorithm toward the base case and termination.

Page 6: Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms

Example 2public void drawLine( int n){

if(n ==0) System.out.println(“That’s all, folks!”);else{ for(int i = 1; i <=n; i++)

System.out.pprintln(“*”); System.out.println(); drawLine(n-1);}

}

Method call drawLine(3)

Page 7: Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms

Answer******That’s all, folks!***Note: A method that has no pending statement following the recursive call is an example of tail recursion. Method drawLine is such a case, but not stackWord.Infinite Recursion – Never reaching the base case.StackOverflowError - run out of memory because of infinite recursion.

Page 8: Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms

Fibonacci• Fibonacci – 1, 1, 2, 3,5,8,13…public static int fib(int n){

if(n == 1 || n == 2) return 1;

else return fib(n-1) + fib(n-2);

}

Page 9: Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms

Fibonacci

Fib(5)

Fib(4)

Fib(3)

Fib(2) Fib(1)

Fib(2)

Fib(3)

Fib(2) Fib(1)

Page 10: Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms

General Rules for Recursion• Avoid recursion for algorithms that involve large

local arrays – too many recursive calls cause memory overflow

• Use recursion when it significantly simplifies code.• Avoid recursion for simple iterative methods like

factorial, Fibonacci, and the linear search.• Recursion is especially useful for – Branching processes like traversing trees or

directories. – Divide-and-Conquer algorithms like mergesort and

binary search