Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Day 25. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Friday Capstones Schedule.

Similar presentations


Presentation on theme: "Chapter Day 25. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Friday Capstones Schedule."— Presentation transcript:

1 Chapter Day 25

2 © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Friday Capstones Schedule  Capstones Due Dec 14 at 3 PM Quiz 3 (last one) on Dec 8  25 M/C questions Today we will discuss recursion programming

3 © 2007 Pearson Addison-Wesley. All rights reserved Outline Recursive Thinking Recursive Programming Using Recursion Recursion in Graphics

4 © 2007 Pearson Addison-Wesley. All rights reserved Towers of Hanoi The Towers of Hanoi is a puzzle made up of three vertical pegs and several disks that slide on the pegs The disks are of varying size, initially placed on one peg with the largest disk on the bottom with increasingly smaller ones on top The goal is to move all of the disks from one peg to another under the following rules:  We can move only one disk at a time  We cannot move a larger disk on top of a smaller one

5 © 2007 Pearson Addison-Wesley. All rights reserved Towers of Hanoi Original ConfigurationMove 1Move 3Move 2

6 © 2007 Pearson Addison-Wesley. All rights reserved Towers of Hanoi Move 4Move 5Move 6Move 7 (done)

7 © 2007 Pearson Addison-Wesley. All rights reserved Solution To move N discs from Start Peg to End Peg  Move the topmost N-1 discs from Start to extra peg  Move the largest from start peg to end peg  Move the N-1 discs from extra peg to end peg Two recursive steps MoveTH (N, Start, End, Extra) { MoveTH (N-1, Start, Extra, End) Move N from start to end Move TH (N-1, Extra, End, Start) }

8 © 2007 Pearson Addison-Wesley. All rights reserved Towers of Hanoi An iterative solution to the Towers of Hanoi is quite complex A recursive solution is much shorter and more elegant See SolveTowers.java (page 594) SolveTowers.java See TowersOfHanoi.java (page 595) TowersOfHanoi.java

9 © 2007 Pearson Addison-Wesley. All rights reserved Maze Traversal We can use recursion to find a path through a maze From each location, we can search in each direction Recursion keeps track of the path through the maze The base case is an invalid move or reaching the final destination See MazeSearch.java (page 587) MazeSearch.java See Maze.java (page 588) Maze.java

10 © 2007 Pearson Addison-Wesley. All rights reserved Outline Recursive Thinking Recursive Programming Using Recursion Recursion in Graphics

11 © 2007 Pearson Addison-Wesley. All rights reserved Tiled Pictures Consider the task of repeatedly displaying a set of images in a mosaic  Three quadrants contain individual images  Upper-left quadrant repeats pattern The base case is reached when the area for the images shrinks to a certain size See TiledPictures.java (page 598) TiledPictures.java

12 © 2007 Pearson Addison-Wesley. All rights reserved Tiled Pictures

13 © 2007 Pearson Addison-Wesley. All rights reserved Fractals A fractal is a geometric shape made up of the same pattern repeated in different sizes and orientations The Koch Snowflake is a particular fractal that begins with an equilateral triangle To get a higher order of the fractal, the sides of the triangle are replaced with angled line segments See KochSnowflake.java (page 601) KochSnowflake.java See KochPanel.java (page 604) KochPanel.java

14 © 2007 Pearson Addison-Wesley. All rights reserved Koch Snowflakes Becomes

15 © 2007 Pearson Addison-Wesley. All rights reserved Koch Snowflakes

16 © 2007 Pearson Addison-Wesley. All rights reserved Koch Snowflakes

17 © 2007 Pearson Addison-Wesley. All rights reserved Summary Chapter 11 has focused on:  thinking in a recursive manner  programming in a recursive manner  the correct use of recursion  recursion examples


Download ppt "Chapter Day 25. © 2007 Pearson Addison-Wesley. All rights reserved Agenda Day 25 Problem set 5 Posted (Last one)  Due Dec 8 Friday Capstones Schedule."

Similar presentations


Ads by Google