Chapter 5 - Recursion Revision & Exercises A quick & not complete revision Please study well the slides and watch the recorded video lectures. This revision is not complete. Recursion is a simple concept. Any function that calls itself is recursive. Recursion is useful for tasks that can be defined in terms of similar subtasks. For example, sort, search, and traversal problems often have simple recursive solutions. A recursive function performs a task in part by calling itself to perform the subtasks. At some point, the function encounters a subtask that it can perform without calling itself. This case, in which the function does not recur, is called the base case; the former, in which the function calls itself to perform a subtask, is referred to as the recursive case. So in recursion there MUST be two types of cases: recursive cases & base cases (see video lectures). Without the base cases the recursion would go infinitely until the program crashes. Base cases are where the recursion stops and starts returning back. Every recursive case MUST eventually lead to a base case. Example: Factorial function Recall the factorial example from the lecture: So we learned that n! can be written recursively as n * (n -1)! Look: you know 4! as 4*3*2*1 from mathematics in school but we can also express 4! as 4 * 3! right? same as n * (n-1)! so also 3! can be expressed in turn as 3 * 2! and so on and so forth... This means something like 4 * 3 * 2! and then we continue with 2! in terms of 2 * 1! etc… until we reach the base case right? The base case in the image above is if n=0 the factorial would be 1 i.e. 0! = 1. We need this base case without it we won’t find the solution. When reaching the base we start returning back see image below (after the Java code). The task is to determine the value of n! and the subtask is to determine the value of (n – 1)!. In the recursive case, when n is greater than 1, the function calls itself to determine the value of (n – 1)! and multiplies that by n. In the base case, when n is 0, the function simply returns 1. Look at the code in the lecture: Page 1 of 3 If we trace what is going on with calling factorial(4) i.e. 4! what will happen? Notice that n decreases by 1 each time the function recur (we have factorial (n-1) right?). This ensures that the base case will eventually be reached. If a function is written incorrectly such that it does not always reach a base case, it will recur infinitely eventually a stack overflow will occur in memory and the program will crash if there is no base case. Figure 1: recursion trace NB: Recursion is used to repeat tasks and I have told you in the lectures that some programming languages don’t even have while and for loops at all. That means repetition of code can be easily done with recursion. So every problem in the world that needs repetition of some sort can be written either with a recursive solution or with a traditional iterative solution i.e. using loops and vice versa. A solution that uses loops is called iterative or non-recursive solution. In the exam, if we ask you to use recursion, you MUST write a solution using recursion in it otherwise you will get 0 on the question. Of course you could use recursion with loops and other nonrecursive constructs if the problem requires it but the solution should be recursive ie there should be a certain function calling itself with certain parameters. Later in the coding interviews, you can give whatever solution you want to the interviewer. Page 2 of 3 Exercises 1. Write a recursive function that finds the maximum element in an array of integers of n elements. Hint: The array should be given to the function and the size of the array n. 2. Write a recursive function that takes a character string s and output its reverse. For example: the reverse of ‘pots&pans’ would be ‘snap&stop’. 3. Write a recursive Java method that determines if a string s is a palindrome, that is, it is equal to its reverse. Examples of palindromes include 'racecar' and 'gohangasalamiimalasagnahog'. 4. Write a recursive function that converts as String of digits into the integer it represents. For example: ‘13531’ would be transformed to the integer 13531. Your solution has to have recursion. (difficulty: medium) 5. More difficult (asked in an interview) – try to solve at home: Implement a recursive algorithm that prints all possible orderings of the characters in a string. In other words, print all permutations that use all the characters from the original string. For example, given the string “hat”, your function should print the strings “tha”, “aht”, “tah”, “ath”, “hta”, and “hat”. Treat each character in the input string as a distinct character, even if it is repeated. Given the string “aaa”, your routine should print “aaa” six times. You may print the permutations in any order you choose. Solution See Netbeans Project. Please try to solve them on your own – no point of looking at the solution. Page 3 of 3