Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and...

31
Chapter 14 Recursion

Transcript of Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and...

Page 1: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Chapter 14

Recursion

Page 2: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Chapter Objectives

• Learn about recursive definitions

• Explore the base case and the general case of a recursive definition

• Learn about recursive algorithms

Page 3: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Chapter Objectives

• Learn about recursive methods

• Become aware of direct and indirect recursion

• Explore how to use recursive methods to implement recursive algorithms

Page 4: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Recursive Definitions

• Recursion– Process of solving a problem by reducing it to

smaller versions of itself

• Recursive definition– Definition in which a problem is expressed in

terms of a smaller version of itself– Has one or more base cases

Page 5: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Recursive Definitions• Recursive algorithm

– Algorithm that finds the solution to a given problem by reducing the problem to smaller versions of itself

– Has one or more base cases– Implemented using recursive methods

• Recursive method– Method that calls itself

• Base case– Case in recursive definition in which the solution is

obtained directly – Stops the recursion

Page 6: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Recursive Definitions

• General solution– Breaks problem into smaller versions of itself

• General case– Case in recursive definition in which a smaller

version of itself is called– Must eventually be reduced to a base case

Page 7: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Tracing a Recursive Method

Recursive method– Has unlimited copies of itself– Every recursive call has its own

• code• set of parameters• set of local variables

Page 8: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Tracing a Recursive Method

After completing a recursive call• Control goes back to the calling environment

• Recursive call must execute completely before control goes back to previous call

• Execution in previous call begins from point immediately following recursive call

Page 9: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Recursive Definitions

• Directly recursive: a method that calls itself• Indirectly recursive: a method that calls another

method and eventually results in the original method call

• Tail recursive method: recursive method in which the last statement executed is the recursive call

• Infinite recursion: the case where every recursive call results in another recursive call

Page 10: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Designing Recursive Methods

• Understand problem requirements

• Determine limiting conditions

• Identify base cases

Page 11: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Designing Recursive Methods

• Provide direct solution to each base case

• Identify general case(s)

• Provide solutions to general cases in terms of smaller versions of general cases

Page 12: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Recursive Factorial Method

public static int fact(int num){ if(num = = 0) return 1; else return num * fact(num – 1);}

Page 13: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Recursive Factorial Trace

Page 14: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Recursive Implementation: Largest Value in Array

Page 15: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Execution of largest(list, 0, 3)

Page 16: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Recursive Fibonacci

public static int rFibNum(int a, int b, int n){ if(n = = 1) return a; else if(n = = 2) return b; else return rFibNum(a, b, n -1) + rFibNum(a, b, n - 2);}

Page 17: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Execution of rFibonacci(2,3,5)

Page 18: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Towers of Hanoi Problem with Three Disks

Page 19: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Towers of Hanoi: Three Disk Solution

Page 20: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Towers of Hanoi: Three Disk Solution

Page 21: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Towers of Hanoi: Recursive Algorithm

Page 22: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Recursion or Iteration?

• Two ways to solve particular problem– Iteration– Recursion

• Iterative control structures: use looping to repeat a set of statements

• Tradeoffs between two options– Sometimes recursive solution is easier– Recursive solution is often slower

Page 23: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Programming Example: Decimal to Binary

public static void decToBin(int num, int base){ if(num > 0) { decToBin(num/base, base); System.out.print(num % base); }}

Page 24: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Execution of decToBin(13,2)

Page 25: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Sierpinski Gaskets of Various Orders

Page 26: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Programming Example:Sierpinski Gasket

• Input: non-negative integer indicating level of Sierpinski gasket

• Output: triangle shape displaying a Sierpinski gasket of the given order

• Solution includes– Recursive method drawSierpinski– Method to find midpoint of two points

Page 27: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Recursive Algorithm to Draw Sierpinski Gasket

Page 28: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Programming Example: Sierpinski Gasket Input

Page 29: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Programming Example: Sierpinski Gasket Input

Page 30: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Chapter Summary

• Recursive Definitions

• Recursive Algorithms

• Recursive Methods

• Base cases

• General cases

Page 31: Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.

Chapter Summary

• Tracing recursive methods

• Designing recursive methods

• Varieties of recursive methods

• Recursion vs. Iteration

• Various recursive functions explored