Chapter 20 Recursion. Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

Similar documents
CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

What Is Recursion? Recursion. Binary search example postponed to end of lecture

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

csci 210: Data Structures Recursion

Recursion and Dynamic Programming. Biostatistics 615/815 Lecture 5

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Data Structures. Algorithm Performance and Big O Analysis

8 Primes and Modular Arithmetic

Section IV.1: Recursive Algorithms and Recursion Trees

10CS35: Data Structures Using C

Analysis of Binary Search algorithm and Selection Sort algorithm

3 length + 23 size = 2

Chapter 5. Recursion. Data Structures and Algorithms in Java

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Patterns in Pascal s Triangle

Pseudo code Tutorial and Exercises Teacher s Version

1. Define: (a) Variable, (b) Constant, (c) Type, (d) Enumerated Type, (e) Identifier.

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

Solving Problems Recursively

Module 2 Stacks and Queues: Abstract Data Types

3.5 RECURSIVE ALGORITHMS

Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., Barnette ND, McQuain WD

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

Recursion and Recursive Backtracking

Unit 6. Loop statements

Recursion vs. Iteration Eliminating Recursion

Programming Exercises

PES Institute of Technology-BSC QUESTION BANK

Zabin Visram Room CS115 CS126 Searching. Binary Search

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Exercises Software Development I. 11 Recursion, Binary (Search) Trees. Towers of Hanoi // Tree Traversal. January 16, 2013

Algorithm Design and Recursion

Python for Rookies. Example Examination Paper

GCDs and Relatively Prime Numbers! CSCI 2824, Fall 2014!

Elementary Number Theory and Methods of Proof. CSE 215, Foundations of Computer Science Stony Brook University

Charles Dierbach. Wiley

Data Structures and Algorithms Written Examination

Lecture 12 Doubly Linked Lists (with Recursion)

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Grade 6 Math Circles March 10/11, 2015 Prime Time Solutions

CS 2412 Data Structures. Chapter 2 Stacks and recursion

Algorithms. Margaret M. Fleck. 18 October 2010

Syllabus for Computer Science. Proposed scheme for B.Sc Programme under Choice Based Credit System

Chapter 7: Sequential Data Structures

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

What Is Singapore Math?

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

MATH 289 PROBLEM SET 4: NUMBER THEORY

Lab Experience 17. Programming Language Translation

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Computing Concepts with Java Essentials

CS 103X: Discrete Structures Homework Assignment 3 Solutions

APP INVENTOR. Test Review

I PUC - Computer Science. Practical s Syllabus. Contents

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Curriculum Map. Discipline: Computer Science Course: C++

1. The memory address of the first element of an array is called A. floor address B. foundation addressc. first address D.

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

Ordered Lists and Binary Trees

Introduction to Parallel Programming and MapReduce

Grade 7 & 8 Math Circles October 19, 2011 Prime Numbers

Chapter 3. if 2 a i then location: = i. Page 40

Example of a Java program

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Chapter 1. Computation theory

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

BCS2B02: OOP Concepts and Data Structures Using C++

recursion, O(n), linked lists 6/14

Algorithm Analysis [2]: if-else statements, recursive algorithms. COSC 2011, Winter 2004, Section N Instructor: N. Vlajic

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Digital Logic Design. Basics Combinational Circuits Sequential Circuits. Pu-Jen Cheng

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Chapter 7: Additional Topics

Tests for Divisibility, Theorems for Divisibility, the Prime Factor Test

CHAPTER 5. Number Theory. 1. Integers and Division. Discussion

CompSci-61B, Data Structures Final Exam

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

Lecture 13 - Basic Number Theory.

Chapter 13: Fibonacci Numbers and the Golden Ratio

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

St S a t ck a ck nd Qu Q eue 1

1. True or False? A voltage level in the range 0 to 2 volts is interpreted as a binary 1.

Lecture 11: Tail Recursion; Continuations

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

Lecture Notes on Linear Search

Figure 1: Graphical example of a mergesort 1.

Chapter 6 Load Balancing

Chapter 5 Functions. Introducing Functions

Transcription:

Chapter 20 Recursion 1

Motivations Suppose you want to find all the files under a directory that contains a particular word. How do you solve this problem? There are several ways to solve this problem. An intuitive solution is to use recursion by searching the files in the subdirectories recursively. 2

Motivations H-trees, depicted in Figure 20.1, are used in a very largescale integration (VLSI) design as a clock distribution network for routing timing signals to all parts of a chip with equal propagation delays. How do you write a program to display H-trees? A good approach is to use recursion. 3

Objectives To describe what a recursive method is and the benefits of using recursion ( 20.1). To develop recursive methods for recursive mathematical functions ( 20.2 20.3). To explain how recursive method calls are handled in a call stack ( 20.2 20.3). To solve problems using recursion ( 20.4). To use an overloaded helper method to derive a recursive method ( 20.5). To implement a selection sort using recursion ( 20.5.1). To implement a binary search using recursion ( 20.5.2). To get the directory size using recursion ( 20.6). To solve the Towers of Hanoi problem using recursion ( 20.7). To draw fractals using recursion ( 20.8). To discover the relationship and difference between recursion and iteration ( 20.9). To know tail-recursive methods and why they are desirable ( 20.10). 4

factorial(0) = 1; Computing Factorial factorial(n) = n*factorial(n-1); n! = n * (n-1)! ComputeFactorial Run 5

animation Computing Factorial factorial(0) = 1; factorial(n) = n*factorial(n-1); factorial(3) 6

animation Computing Factorial factorial(3) = 3 * factorial(2) factorial(0) = 1; factorial(n) = n*factorial(n-1); 7

animation Computing Factorial factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) factorial(0) = 1; factorial(n) = n*factorial(n-1); 8

animation Computing Factorial factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) factorial(0) = 1; factorial(n) = n*factorial(n-1); 9

animation Computing Factorial factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) factorial(0) = 1; factorial(n) = n*factorial(n-1); 10

animation Computing Factorial factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1) factorial(0) = 1; factorial(n) = n*factorial(n-1); 11

animation Computing Factorial factorial(3) = 3 * factorial(2) = 3 * (2 * factorial(1)) = 3 * ( 2 * (1 * factorial(0))) = 3 * ( 2 * ( 1 * 1))) = 3 * ( 2 * 1) = 3 * 2 factorial(0) = 1; factorial(n) = n*factorial(n-1); 12

animation Computing Factorial factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * (2 * factorial(1)) = 4 * 3 * ( 2 * (1 * factorial(0))) = 4 * 3 * ( 2 * ( 1 * 1))) = 4 * 3 * ( 2 * 1) = 4 * 3 * 2 = 4 * 6 = 24 factorial(0) = 1; factorial(n) = n*factorial(n-1); 13

animation Trace Recursive factorial Executes factorial(4) Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) Step 7: return 2 return 3 * factorial(2) return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 6: return 1 Step 3: executes factorial(1) return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 Main method 14

animation Trace Recursive factorial factorial(4) Step 9: return 24 Step 8: return 6 Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(3) Step 7: return 2 Step 6: return 1 return 2 * factorial(1) Step 2: executes factorial(2) Step 3: executes factorial(1) Stack return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(4) Main method 15

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(2) Step 7: return 2 Step 6: return 1 return 2 * factorial(1) Step 2: executes factorial(2) Step 3: executes factorial(1) Stack return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(3) for factorial(4) Main method 16

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(1) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(2) for factorial(3) for factorial(4) Main method 17

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) Executes factorial(0) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(1) for factorial(2) for factorial(3) for factorial(4) Main method 18

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns 1 Step 2: executes factorial(2) Step 7: return 2 return 2 * factorial(1) Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 Stack for factorial(0) for factorial(1) for factorial(2) for factorial(3) for factorial(4) Main method 19

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(0) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(1) for factorial(2) for factorial(3) for factorial(4) Main method 20

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(1) Step 7: return 2 return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 3: executes factorial(1) Step 6: return 1 return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(2) for factorial(3) for factorial(4) Main method 21

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(2) Step 7: return 2 Step 6: return 1 return 2 * factorial(1) Step 2: executes factorial(2) Step 3: executes factorial(1) Stack return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(3) for factorial(4) Main method 22

animation Trace Recursive factorial Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) return 3 * factorial(2) returns factorial(3) Step 7: return 2 Step 6: return 1 return 2 * factorial(1) Step 2: executes factorial(2) Step 3: executes factorial(1) Stack return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 for factorial(4) Main method 23

animation Trace Recursive factorial returns factorial(4) Step 9: return 24 Step 8: return 6 factorial(4) Step 0: executes factorial(4) return 4 * factorial(3) Step 1: executes factorial(3) Step 7: return 2 return 3 * factorial(2) return 2 * factorial(1) Step 2: executes factorial(2) Stack Step 6: return 1 Step 3: executes factorial(1) return 1 * factorial(0) Step 4: executes factorial(0) Step 5: return 1 return 1 Main method 24

factorial(4) Stack Trace 5 for factorial(0) 4 for factorial(1) for factorial(1) 3 for factorial(2) for factorial(2) for factorial(2) 2 for factorial(3) for factorial(3) for factorial(3) for factorial(3) 1 for factorial(4) for factorial(4) for factorial(4) for factorial(4) for factorial(4) 6 for factorial(1) for factorial(2) 7 for factorial(2) for factorial(3) for factorial(3) 8 for factorial(3) for factorial(4) for factorial(4) for factorial(4) 9 for factorial(4) 25

Other Examples f(0) = 0; f(n) = n + f(n-1); 26

Fibonacci Numbers Fibonacci series: 0 1 1 2 3 5 8 13 21 34 55 89 indices: 0 1 2 3 4 5 6 7 8 9 10 11 fib(0) = 0; fib(1) = 1; fib(index) = fib(index -1) + fib(index -2); index >=2 fib(3) = fib(2) + fib(1) = (fib(1) + fib(0)) + fib(1) = (1 + 0) +fib(1) = 1 + fib(1) = 1 + 1 = 2 ComputeFibonacci Run 27

Fibonnaci Numbers, cont. 17: return fib(4) fib(4) 0: call fib(4) 10: return fib(3) return fib(2) + fib(1) return fib(3) + fib(2) 1: call fib(3) 16: return fib(2) 11: call fib(2) return fib(1) + fib(0) 7: return fib(2) 2: call fib(2) 8: call fib(1) 13: return fib(1) 12: call fib(1) 14: return fib(0) return fib(1) + fib(0) 9: return fib(1) 15: return fib(0) return 1 return 1 return 0 4: return fib(1) 5: call fib(0) 3: call fib(1) return 1 6: return fib(0) return 0 28

Characteristics of Recursion All recursive methods have the following characteristics: One or more base cases (the simplest case) are used to stop recursion. Every recursive call reduces the original problem, bringing it increasingly closer to a base case until it becomes that case. In general, to solve a problem using recursion, you break it into subproblems. If a subproblem resembles the original problem, you can apply the same approach to solve the subproblem recursively. This subproblem is almost the same as the original problem in nature with a smaller size. 29

Problem Solving Using Recursion Let us consider a simple problem of printing a message for n times. You can break the problem into two subproblems: one is to print the message one time and the other is to print the message for n-1 times. The second problem is the same as the original problem with a smaller size. The base case for the problem is n==0. You can solve this problem using recursion as follows: public static void nprintln(string message, int times) { if (times >= 1) { System.out.println(message); nprintln(message, times - 1); } // The base case is times == 0 } 30

Think Recursively Many of the problems presented in the early chapters can be solved using recursion if you think recursively. For example, the palindrome problem in Listing 7.1 can be solved recursively as follows: public static boolean ispalindrome(string s) { if (s.length() <= 1) // Base case } return true; else if (s.charat(0)!= s.charat(s.length() - 1)) // Base case return false; else return ispalindrome(s.substring(1, s.length() - 1)); 31

Recursive Helper Methods The preceding recursive ispalindrome method is not efficient, because it creates a new string for every recursive call. To avoid creating new strings, use a helper method: public static boolean ispalindrome(string s) { return ispalindrome(s, 0, s.length() - 1); } public static boolean ispalindrome(string s, int low, int high) { } if (high <= low) // Base case return true; else if (s.charat(low)!= s.charat(high)) // Base case return false; else return ispalindrome(s, low + 1, high - 1); 32

Recursive Selection Sort 1. Find the smallest number in the list and swaps it with the first number. 2. Ignore the first number and sort the remaining smaller list recursively. RecursiveSelectionSort 33

Recursive Binary Search 1. Case 1: If the key is less than the middle element, recursively search the key in the first half of the array. 2. Case 2: If the key is equal to the middle element, the search ends with a match. 3. Case 3: If the key is greater than the middle element, recursively search the key in the second half of the array. RecursiveBinarySearch 34

Recursive Implementation /** Use binary search to find the key in the list */! public static int recursivebinarysearch(int[] list, int key) {! int low = 0;! int high = list.length - 1;! return recursivebinarysearch(list, key, low, high);! }!! /** Use binary search to find the key in the list between! list[low] list[high] */! public static int recursivebinarysearch(int[] list, int key,! int low, int high) {! if (low > high) // The list has been exhausted without a match! return -low - 1;!! int mid = (low + high) / 2;! if (key < list[mid])! return recursivebinarysearch(list, key, low, mid - 1);! else if (key == list[mid])! return mid;! else! return recursivebinarysearch(list, key, mid + 1, high);! } 35

Directory Size The preceding examples can easily be solved without using recursion. This section presents a problem that is difficult to solve without using recursion. The problem is to find the size of a directory. The size of a directory is the sum of the sizes of all files in the directory. A directory may contain subdirectories. Suppose a directory contains files,,...,, and subdirectories,,...,, as shown below. directory f 1 1 f 2 1... f m 1 d 1 1 d 2 1... d n 1 36

Directory Size The size of the directory can be defined recursively as follows: size d) = size( f ) + size( f ) +... + size( f ) + size( d ) + size( d ) +... + ( 1 2 m 1 2 size( dn ) DirectorySize Run 37

Towers of Hanoi There are n disks labeled 1, 2, 3,..., n, and three towers labeled A, B, and C. No disk can be on top of a smaller disk at any time. All the disks are initially placed on tower A. Only one disk can be moved at a time, and it must be the top disk on the tower. 38

Towers of Hanoi, cont. A B C A B C Original position Step 4: Move disk 3 from A to B A B C A B C Step 1: Move disk 1 from A to B Step 5: Move disk 1 from C to A A B C A B C Step 2: Move disk 2 from A to C Step 6: Move disk 2 from C to B A B C A B C Step 3: Move disk 1 from B to C Step 7: Mve disk 1 from A to B 39

Solution to Towers of Hanoi The Towers of Hanoi problem can be decomposed into three subproblems. n-1 disks n-1 disks...... A B C A B C Original position Step2: Move disk n from A to C n-1 disks n-1 disks...... A B C A B C Step 1: Move the first n-1 disks from A to C recursively Step3: Move n-1 disks from C to B recursively 40

Solution to Towers of Hanoi Move the first n - 1 disks from A to C with the assistance of tower B. Move disk n from A to B. Move n - 1 disks from C to B with the assistance of tower A. TowersOfHanoi Run 41

gcd(2, 3) = 1 gcd(2, 10) = 2 gcd(25, 35) = 5 gcd(205, 301) = 5 gcd(m, n) Exercise 20.3 GCD Approach 1: Brute-force, start from min(n, m) down to 1, to check if a number is common divisor for both m and n, if so, it is the greatest common divisor. Approach 2: Euclid s algorithm Approach 3: Recursive method 42

Approach 2: Euclid s algorithm // Get absolute value of m and n; t1 = Math.abs(m); t2 = Math.abs(n); // r is the remainder of t1 divided by t2; r = t1 % t2; while (r!= 0) { t1 = t2; t2 = r; r = t1 % t2; } // When r is 0, t2 is the greatest common // divisor between t1 and t2 return t2; 43

Approach 3: Recursive Method gcd(m, n) = n if m % n = 0; gcd(m, n) = gcd(n, m % n); otherwise; 44

Fractals? A fractal is a geometrical figure just like triangles, circles, and rectangles, but fractals can be divided into parts, each of which is a reduced-size copy of the whole. There are many interesting examples of fractals. This section introduces a simple fractal, called Sierpinski triangle, named after a famous Polish mathematician. 45

Sierpinski Triangle 1. It begins with an equilateral triangle, which is considered to be the Sierpinski fractal of order (or level) 0, as shown in Figure (a). 2. Connect the midpoints of the sides of the triangle of order 0 to create a Sierpinski triangle of order 1, as shown in Figure (b). 3. Leave the center triangle intact. Connect the midpoints of the sides of the three other triangles to create a Sierpinski of order 2, as shown in Figure (c). 4. You can repeat the same process recursively to create a Sierpinski triangle of order 3, 4,..., and so on, as shown in Figure (d). 46

Sierpinski Triangle Solution p1 midbetweenp1p2 midbetweenp3p1 p2 p3 midbetweenp2p3 SierpinskiTriangle Run 47

Sierpinski Triangle Solution p1 Draw the Sierpinski triangle displaytriangles(g, order, p1, p2, p3) p2 p3 SierpinskiTriangle Run 48

Sierpinski Triangle Solution p1 Recursively draw the small Sierpinski triangle displaytriangles(g, order - 1, p1, p12, p31) ) Recursively draw the small Sierpinski triangle displaytriangles(g, order - 1, p12, p2, p23) p2 p12 p23 p31 p3 Recursively draw the small Sierpinski triangle displaytriangles(g, order - 1, p31, p23, p3) ) SierpinskiTriangle Run 49

Recursion vs. Iteration Recursion is an alternative form of program control. It is essentially repetition without a loop. Recursion bears substantial overhead. Each time the program calls a method, the system must assign space for all of the method s local variables and parameters. This can consume considerable memory and requires extra time to manage the additional space. 50

Advantages of Using Recursion Recursion is good for solving the problems that are inherently recursive. 51

Tail Recursion A recursive method is said to be tail recursive if there are no pending operations to be performed on return from a recursive call. Non-tail recursive Tail recursive ComputeFactorial ComputeFactorialTailRecursion 52