More on Recursive Recursion vs. Iteration Why Recursion?

Slides:



Advertisements
Similar presentations
Recursion Outline of the upcoming lectures: Review of Recursion
Advertisements

Factorial Recursion stack Binary Search Towers of Hanoi
More on Recursive Methods KFUPM- ICS Data Structures.
More on Recursive Recursion vs. Iteration Why Recursion?
Recursion Outline of the upcoming lectures: –Review of Recursion –Types of Recursive Methods –Final Remarks on Recursion.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
RecursionRecursion Recursion You should be able to identify the base case(s) and the general case in a recursive definition To be able to write a recursive.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
Reading – Chapter 10. Recursion The process of solving a problem by reducing it to smaller versions of itself Example: Sierpinski’s TriangleSierpinski’s.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Lecture 7 b Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems b Today: thinking in a recursive.
Data Structures R e c u r s i o n. Recursive Thinking Recursion is a problem-solving approach that can be used to generate simple solutions to certain.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Recursion.
Recursion A recursive definition is one which uses the word or concept being defined in the definition itself Example: “A computer is a machine.
Recursion Chapter 17 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013.
Chapter 15: Recursion. Recursive Definitions Recursion: solving a problem by reducing it to smaller versions of itself – Provides a powerful way to solve.
Chapter 15: Recursion. Objectives In this chapter, you will: – Learn about recursive definitions – Explore the base case and the general case of a recursive.
CS 116 Object Oriented Programming II Lecture 13 Acknowledgement: Contains materials provided by George Koutsogiannakis and Matt Bauer.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
Recursion Powerful Tool
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
CS212: Data Structures and Algorithms
Recursion.
Recursion Data Structure Submitted By:- Dheeraj Kataria.
Functions.
Abdulmotaleb El Saddik University of Ottawa
RECURSION.
Unit 5 Recursion King Fahd University of Petroleum & Minerals
Methods Chapter 6.
More on Recursive Methods
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Programming: Program Design Including Data Structures
Java Software Structures: John Lewis & Joseph Chase
Loops in C C has three loop statements: the while, the for, and the do…while. The first two are pretest loops, and the the third is a post-test loop. We.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Announcements Final Exam on August 17th Wednesday at 16:00.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Definitions
Recursion Chapter 11.
Fundamentals of Programming
Announcements Final Exam on August 19th Saturday at 16:00.
Unit 3 Test: Friday.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
Announcements Final Exam on December 25th Monday at 16:00.
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
Recursion Great fleas have little fleas upon their backs to bite 'em, And little fleas have lesser fleas, and so ad infinitum. And the great fleas themselves,
When a function is called...
The structure of programming
Yan Shi CS/SE 2630 Lecture Notes
Review of Recursion What is a Recursive Method?
Dr. Sampath Jayarathna Cal Poly Pomona
ICS103 Programming in C Lecture 11: Recursive Functions
Recursion.
ITEC324 Principle of CS III
Complexity Analysis (Part I)
Presentation transcript:

More on Recursive Recursion vs. Iteration Why Recursion? Common Errors in Writing Recursive Methods: COSC 301: Data Structures

Recursion vs. Iteration In general, an iterative version of a method will execute more efficiently in terms of time and space than a recursive version. This is because the overhead involved in entering and exiting a function in terms of stack I/O is avoided in iterative version. Sometimes we are forced to use iteration because stack cannot handle enough activation records - Example: power(2, 5000)) COSC 301: Data Structures

Why Recursion? Usually recursive algorithms have less code, therefore algorithms can be easier to write and understand - e.g. Towers of Hanoi. However, avoid using excessively recursive algorithms even if the code is simple. Sometimes recursion provides a much simpler solution. Obtaining the same result using iteration requires complicated coding - e.g. Quicksort, Towers of Hanoi, etc. Recursive methods provide a very natural mechanism for processing recursive data structures. A recursive data structure is a data structure that is defined recursively – e.g. Tree. Functional programming languages such as Clean, FP, Haskell, Miranda, and SML do not have explicit loop constructs. In these languages looping is achieved by recursion. COSC 301: Data Structures

Why Recursion? Some recursive algorithms are more efficient than equivalent iterative algorithms. Example: public static long power1 (int x, int n) { long product = 1; for (int i = 1; i <= n; i++) product *= x; return product; } public static long power2 (int x, int n) { if (n == 1) return x; else if (n == 0)return 1; else { long t = power2(x , n / 2); if ((n % 2) == 0) return t * t; else return x * t * t; } COSC 301: Data Structures

Common Errors in Writing Recursive Methods The method does not call itself directly or indirectly. Non-terminating Recursive Methods (Infinite recursion): No base case. The base case is never reached for some parameter values. int badFactorial(int x) { return x * badFactorial(x-1); } int anotherBadFactorial(int x) { if(x == 0) return 1; else return x*(x-1)*anotherBadFactorial(x -2); // When x is odd, we never reach the base case!! } COSC 301: Data Structures

Common Errors in Writing Recursive Methods Post increment and decrement operators must not be used since the update will not occur until AFTER the method call - infinite recursion. Local variables must not be used to accumulate the result of a recursive method. Each recursive call has its own copy of local variables. public static int sumArray (int[ ] x, int index) { if (index == x.length)return 0; else return x[index] + sumArray (x, index++); } public static int sumArray (int[ ] x, int index) { int sum = 0; if (index == x.length)return sum; else { sum += x[index]; return sumArray(x,index + 1); } COSC 301: Data Structures

Common Errors in Writing Recursive Methods Wrong placement of return statement. Consider the following method that is supposed to calculate the sum of the first n integers: When result is initialized to 0, the method returns 0 for whatever value of the parameter n. The result returned is that of the final return statement to be executed. Example: A trace of the call sum(3, 0) is: public static int sum (int n, int result) { if (n >= 0) sum(n - 1, n + result); return result; } COSC 301: Data Structures

Common Errors in Writing Recursive Methods A correct version of the method is: Example: A trace of the call sum(3, 0) is: public static int sum(int n, int result){ if (n == 0) return result; else return sum(n-1, n + result); } COSC 301: Data Structures

Common Errors in Writing Recursive Methods The use of instance or static variables in recursive methods should be avoided. Although it is not an error, it is bad programming practice. These variables may be modified by code outside the method and cause the recursive method to return wrong result. public class Sum{ private int sum; public int sumArray(int[ ] x, int index){ if(index == x.length) return sum; else { sum += x[index]; return sumArray(x,index + 1); } COSC 301: Data Structures