recursion - What is recursion? Solving a problem...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Recursion What is recursion? • Solving a problem recursively involves solving a simplified version of that same problem • Let’s look at some examples…. Triangle Numbers • Compute the area of a triangle of width n • Assume each [ ] square has an area of 1 • Also called the n th triangle number • The third triangle number is 6 [ ] [ ][ ] [ ][ ][ ] Outline of Triangle Class public class Triangle { public Triangle(int aWidth) { width = aWidth; } public int getArea() { . . . } private int width; } Handling Triangle of Width 1 • The triangle consists of a single square • Its area is 1 • Add the code to getArea method for width 1 public int getArea() { if (width == 1) return 1; . . . } Handling the General Case • Assume we know the area of the smaller, colored triangle • Area of larger triangle can be calculated as: [] [][] [][][] [][][][] smallerArea + width
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Handling the General Case • To get the area of the smaller triangle – Make a smaller triangle and ask it for its area Triangle smallerTriangle = new Triangle(width - 1); int smallerArea = smallerTriangle.getArea(); Completed getArea method public int getArea() { if (width == 1) return 1; Triangle smallerTriangle = new Triangle(width - 1); int smallerArea = smallerTriangle.getArea(); return smallerArea + width; } Computing the area of a triangle with width 4 • getArea method makes a smaller triangle of width 3 – It calls getArea on that triangle • That method makes a smaller triangle of width 2 – It calls getArea on that triangle » That method makes a smaller triangle of width 1 » It calls getArea on that triangle – That method returns 1 Computing the area of a triangle with width 4 • The method returns smallerArea + width = 1 + 2 = 3 – The method returns smallerArea + width = 3 + 3 = 6 • The method returns smallerArea + width = 6 + 4 = 10 Recursion • A recursive computation solves a problem by using the solution of the same problem with simpler values • For recursion to terminate, there must be special cases for the simplest inputs. • To complete our Triangle example, we must handle width <= 0 if (width <= 0) return 0; Recursion • Two key requirements for recursion success: – Every recursive call must simplify the computation in some way – There must be special cases to handle the simplest computations directly
Background image of page 2
3 Other Ways to Compute Triangle Numbers • The area of a triangle equals the sum • Using a simple loop: • Using math: double area = 0; for (int i = 1; i <= width; i++) area = area + i; 1 + 2 + . . . + n = n × (n + 1)/2 => width * (width + 1) / 2 1 + 2 + 3 + . . . + width Self Check 1. Why is the statement in the getArea method unnecessary? 2. How would you modify the program to
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

recursion - What is recursion? Solving a problem...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online