tail recursion flashback
play

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


  1. 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 series of statements repeatedly... wb1 Point pt1 WallWorld ww position pt1 x 5 until some condition is met: heading EAST y 1 color red statement1; brushDown true statement2; Execution Land statement3; run goToWallRec goToWallRec goToWallRec this this this wb1 wb1 wb1 this ww wendy wb1 if (!isFacingWall()) if (!isFacingWall()) if (!isFacingWall()) WallBuggle wendy = new WallBuggle(); { { { wendy.goToWallRec(); foward(); foward(); foward(); goToWallRec(); goToWallRec(); goToWallRec(); } } } goToWallRec goToWallRec this this wb1 wb1 if (!isFacingWall()) if (!isFacingWall()) { { foward(); foward(); goToWallRec(); goToWallRec(); P - 3 P - 4 } } “While” Construct How to exit from a while loop? Syntax of while loop: Syntax of while loop: while (boolean expression) { while (boolean expression) { statement1; statement1; statement2; statement2; statement3; statement3; } } How it works: In recursion: The problem size must shrink with each recursive invocation. 1. Evaluate boolean expression In a while loop: Some statement in the body of the loop 2. If true → execute body of while and then goto step 1 must lead to a change in the value of the boolean expression (otherwise, we loop forever). 3. If false → go to statements after while loop P - 5 P - 6

  2. While Loop Example Houston, we have a problem! public void blastoff(int n) { public void lameBlastoff(int n) { while (n > 0) { while (n > 0) { blastoff(5) lameBlastoff(5) System.out.println(n); System.out.println(n); n = n - 1; } } System.out.println(“blastoff!”); System.out.println(“blastoff!”); } } P - 7 P - 8 Tail Recursion FlashBack Recursion vs. Iteration A recursive solution: Remember factorial? public static int factRecursive(int n) { goToWall if (n == 1) return 1; return n * factRecursive(n-1); 1! = 1 } 2! = 2 * 1! An iterative solution: A tail recursive solution: An iterative solution: 3! = 3 * 2! public static int factWhile(int n) { public void goToWallRec() { public void goToWallWhile() { ... if (!isFacingWall()) { forward(); goToWallRec(); n! = n * (n-1)! } } } P - 9 P - 10 } factWhile JEM Iteration Table time n num ans 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; P - 11 P - 12 }

  3. Common While Loop Pattern “For” Construct Syntax of for loop: for ( ) { initialization of boolean exprssn update loop ; ; initialize variables; loop counter (that uses loop cntr) counter while (boolean expression with variables) { statement1; action; statement2; update variables; statement3; } } 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 P - 13 4. If false → go to statements after for loop P - 14 Iterative Factorial with a For Loop Iteration Table An iterative solution: time n num ans public static int factWhile(int n) { What happens at each of iteration of Remember factorial? int num = n; // local variable declrtn int ans = 1; // local variable declrtn factFor(4) ? while (num >= 1) { 1! = 1 ans = ans * num; // calculate product num = num - 1; // decrement num } 2! = 2 * 1! return ans; } 3! = 3 * 2! An iterative solution: An iterative solution: public static int factFor(int n) { ... public static int factFor(int n) { int ans = 1; // local variable declrtn for (int num = n; num > 0; num = num - 1) { n! = n * (n-1)! ans = ans * num; // calculate product } return ans; } P - 15 P - 16 } Iterative Factorial with a For Loop Remember SpiralWorld? An iterative solution: Remember factorial? Circle public static int factFor2(int n) { Square spiral int ans = 1; // local variable declrtn for (int num = 1; num <= n; num = num + 1) { 1! = 1 ans = ans * num; // calculate product } return ans; 2! = 2 * 1! } Spiral star Bent square spiral 3! = 3 * 2! An iterative solution: ... public static int factFor(int n) { int ans = 1; // local variable declrtn for (int num = n; num > 0; num = num - 1) { n! = n * (n-1)! ans = ans * num; // calculate product Triangle spiral } Perfect star return ans; } P - 17 P - 18

  4. What is the Java behind this program? SpiralWorld Example • Remember the method spiral ? How do we draw the following picture in • Things you enter in window TurtleWorld? – steps 11 – angle parameters 8 – length 14 – increment 5 spiral(int steps, int angle, int length, int increment); 17 P - 19 P - 20 Example Tail Recursive Spiral Method t n e m h Initial invocation of method: e t s e l g p g n r c e n e t n a l s i public void spiralRec(int steps, int angle, int length, int increment) spiral(5, 90, 5, 3) { if (steps > 0) { spiral(5, 90, 5, 3) remains to be done fd(length); fd(5); lt(angle); spiral(4, 90, 8, 3) remains to be done lt(90); spiralRec(steps-1, angle, length+increment, increment); fd(8); remains to be done } spiral(3, 90, 11, 3) lt(90); } fd(11); spiral(2, 90, 14, 3) remains to be done lt(90); fd(14); spiral(1, 90, 17, 3) remains to be done lt(90); fd(17); spiral(0, 90, 20, 3) remains to be done lt(90); P - 21 P - 22 Iterative Spiral Method public void spiralWhile(int steps, int angle, int length, int increment) { } P - 23

Recommend


More recommend