Tail Recursion FlashBack goToWall Iteration A tail recursive - - PDF document

tail recursion flashback
SMART_READER_LITE
LIVE PREVIEW

Tail Recursion FlashBack goToWall Iteration A tail recursive - - PDF document

Tail Recursion FlashBack goToWall Iteration A tail recursive solution: public void goToWallRec() { if (!isFacingWall()) { forward(); goToWallRec(); } } P - 1 P - 2 JEM Iteration Object Land WallBuggle Suppose we want to execute a


slide-1
SLIDE 1

P - 1

Iteration

P - 2

Tail Recursion FlashBack

A tail recursive solution:

public void goToWallRec() { if (!isFacingWall()) { forward(); goToWallRec(); } }

goToWall

P - 3

JEM

WallBuggle wendy = new WallBuggle(); wendy.goToWallRec(); if (!isFacingWall()) { foward(); goToWallRec(); }

goToWallRec run

Execution Land

this wendy

ww wb1

this

wb1 if (!isFacingWall()) { foward(); goToWallRec(); }

goToWallRec

this

wb1 if (!isFacingWall()) { foward(); goToWallRec(); }

goToWallRec

this

wb1 if (!isFacingWall()) { foward(); goToWallRec(); }

goToWallRec

this

wb1 if (!isFacingWall()) { foward(); goToWallRec(); }

goToWallRec

this

wb1 position heading color brushDown

true WallBuggle

wb1 red

WallWorld

ww EAST

Object Land

x

Point 5

y

1

pt1 pt1

P - 4

Iteration

Suppose we want to execute a series of statements repeatedly... until some condition is met:

statement1; statement2; statement3;

P - 5

“While” Construct

How it works:

  • 1. Evaluate boolean expression
  • 2. If true → execute body of while and then goto step 1
  • 3. If false → go to statements after while loop

Syntax of while loop:

while (boolean expression) { statement1; statement2; statement3; }

P - 6

How to exit from a while loop?

Syntax of while loop:

while (boolean expression) { statement1; statement2; statement3; }

In recursion: The problem size must shrink with each recursive invocation. In a while loop: Some statement in the body of the loop must lead to a change in the value of the boolean expression (otherwise, we loop forever).

slide-2
SLIDE 2

P - 7

While Loop Example

public void blastoff(int n) { while (n > 0) { System.out.println(n); n = n - 1; } System.out.println(“blastoff!”); } blastoff(5)

P - 8

Houston, we have a problem!

public void lameBlastoff(int n) { while (n > 0) { System.out.println(n); } System.out.println(“blastoff!”); } lameBlastoff(5)

P - 9

Tail Recursion FlashBack

A tail recursive solution:

public void goToWallRec() { if (!isFacingWall()) { forward(); goToWallRec(); } }

An iterative solution:

public void goToWallWhile() { }

goToWall

P - 10

Recursion vs. Iteration

Remember factorial? 1! = 1 2! = 2 * 1! 3! = 3 * 2! ... n! = n * (n-1)! A recursive solution:

public static int factRecursive(int n) { if (n == 1) return 1; return n * factRecursive(n-1); }

An iterative solution:

public static int factWhile(int n) { }

P - 11

factWhile JEM

P - 12

Iteration Table

What happens at each of iteration of factWhile(4) ?

An iterative solution:

public static int factWhile(int n) { int num = n; // local variable declrtn int ans = 1; // local variable declrtn while (num >= 1) { ans = ans * num; // calculate product num = num - 1; // decrement num } return ans; }

time n num ans

slide-3
SLIDE 3

P - 13

Common While Loop Pattern

initialize variables; while (boolean expression with variables) { action; update variables; }

P - 14

“For” Construct

How it works:

  • 1. Initialize loop counter
  • 2. Evaluate boolean expression
  • 3. If true → execute body of for loop, then update loop counter,

and go to step 2

  • 4. If false → go to statements after for loop

initialization of loop counter boolean exprssn (that uses loop cntr) update loop counter

; ;

Syntax of for loop:

for ( ) { statement1; statement2; statement3; }

P - 15

Iterative Factorial with a For Loop

Remember factorial? 1! = 1 2! = 2 * 1! 3! = 3 * 2! ... n! = n * (n-1)!

An iterative solution:

public static int factFor(int n) { }

An iterative solution:

public static int factWhile(int n) { int num = n; // local variable declrtn int ans = 1; // local variable declrtn while (num >= 1) { ans = ans * num; // calculate product num = num - 1; // decrement num } return ans; }

P - 16

Iteration Table

What happens at each of iteration of factFor(4) ?

An iterative solution:

public static int factFor(int n) { int ans = 1; // local variable declrtn for (int num = n; num > 0; num = num - 1) { ans = ans * num; // calculate product } return ans; }

time n num ans

P - 17

Iterative Factorial with a For Loop

Remember factorial? 1! = 1 2! = 2 * 1! 3! = 3 * 2! ... n! = n * (n-1)!

An iterative solution:

public static int factFor(int n) { int ans = 1; // local variable declrtn for (int num = n; num > 0; num = num - 1) { ans = ans * num; // calculate product } return ans; }

An iterative solution:

public static int factFor2(int n) { int ans = 1; // local variable declrtn for (int num = 1; num <= n; num = num + 1) { ans = ans * num; // calculate product } return ans; }

P - 18

Remember SpiralWorld?

Square spiral Triangle spiral Circle Spiral star Perfect star Bent square spiral

slide-4
SLIDE 4

P - 19

SpiralWorld Example

How do we draw the following picture in TurtleWorld?

5 8 11 14 17

P - 20

What is the Java behind this program?

  • Remember the method spiral?
  • Things you enter in window

– steps – angle – length – increment

spiral(int steps, int angle, int length, int increment);

parameters

P - 21

Example

Initial invocation of method:

spiral(5, 90, 5, 3)

s t e p s a n g l e l e n g t h i n c r e m e n t

spiral(5, 90, 5, 3) spiral(0, 90, 20, 3) spiral(1, 90, 17, 3) spiral(2, 90, 14, 3) spiral(3, 90, 11, 3) spiral(4, 90, 8, 3)

fd(5); lt(90); fd(8); lt(90); fd(11); lt(90); fd(14); lt(90); fd(17); lt(90);

remains to be done remains to be done remains to be done remains to be done remains to be done remains to be done

P - 22

Tail Recursive Spiral Method

public void spiralRec(int steps, int angle, int length, int increment) { if (steps > 0) { fd(length); lt(angle); spiralRec(steps-1, angle, length+increment, increment); } }

P - 23

Iterative Spiral Method

public void spiralWhile(int steps, int angle, int length, int increment) { }