Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Recursion vs. Iteration The original Lisp language was truly a functional language: –Everything was expressed as functions –No local variables –No iteration.
Factorial Recursion stack Binary Search Towers of Hanoi
Programming with Recursion
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion Recursion Just be thankful you're not getting all the government you're.
Recursion and Implementation of Functions
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Data Structures and Abstractions with Java, 4e Frank Carrano
Comp 245 Data Structures Recursion. What is Recursion? A problem solving concept which can be used with languages that support the dynamic allocation.
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
Cosc236/recursion1 Recursion Recursive method calls itself Recursion frequently occurs in mathematics Recursive definition –2 parts base part (basis) 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.
1 Joe Meehean.  call themselves directly  or indirectly void f(){... f();... } void g(){... h();... } void h(){... g();... }
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
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.
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
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.
Implementing Subprograms
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.
Chapter 4 Recur ecur ecur ecursion. Chapter 4: Recursion 4.1 – Recursive Definitions, Algorithms and Programs 4.2 – The Three Questions 4.3 – Towers of.
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Recursion Powerful Tool
Programming with Recursion
Implementing Subprograms
Chapter 3: Fundamental Data Structures: The Array and Linked Structures Data Structures in Java: From Abstract Data Types to the Java Collections Framework.
Chapter 13 Recursion Copyright © 2016 Pearson, Inc. All rights reserved.
to understand recursion you must understand recursion
Chapter 10 : Implementing Subprograms
Run-Time Environments Chapter 7
Topic 6 Recursion.
Recursion DRILL: Please take out your notes on Recursion
Data Structures and Algorithms
Abdulmotaleb El Saddik University of Ottawa
Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursion Chapter 10.
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Java Software Structures: John Lewis & Joseph Chase
Programming with Recursion
to understand recursion you must understand recursion
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 10: Implementing Subprograms Sangho Ha
Recursion Output Input
Implementing Subprograms
Recursive Definitions
Chapter 12 Recursion (methods calling themselves)
Recursion Chapter 11.
Recursion Data Structures.
The University of Adelaide, School of Computer Science
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 17: Recursion.
Basics of Recursion Programming with Recursion
UNIT V Run Time Environments.
Java Programming: Chapter 9: Recursion Second Edition
Linked Lists.
Yan Shi CS/SE 2630 Lecture Notes
Implementing Subprograms
Presentation transcript:

Chapter 8: Recursion Data Structures in Java: From Abstract Data Types to the Java Collections Framework by Simon Gray

Introduction A recursive definition is one in which the thing being defined is part of the definition Application in specification of programming language elements data structures functions For computing, this is another way to do iteration

Recursion & Data Structures: Linked List A singly linked list viewed from a recursive perspective

Recursion & Data Structures: Linked List Rule Number Rule Term Definition 1 <LinkedList> ::= null | 2 <SLNode> 3 <DataField> < Successor > 4 <Successor> A formal definition of a singly linked data structure Note: The recursion here is indirect. LinkedList  SLNode  Successor  LinkedList

Java Corresponding to Formal Description 1 /** 2 * A single node in a singly linked structure. 3 */ 4 public class SLNode<E> { 5 private E dataField; private SLNode successor; // Recursive part 7 . . . 8 } Compare this to Rule 3 from the previous slide. Once you have the recursive definition, the implementation frequently follows quite naturally.

Recursion & Functions Trivial example: factorial function 1, if n == 1 base case n (n – 1)!, if n > 1 recursive case

Key Characteristic of Recursive Problem Solving A key characteristic of recursive problem solving, evident from the factorial example, is that each recursive application of the definition deals with a subproblem that has the same organization as the original problem. is closer to the base case than the original problem.

From Definition to Method Earlier we said that once you have the recursive definition, the implementation follows fairly naturally. Recursive Definition Recursive Method n! if n is 1, then n! = 1 if n > 1, then n! = n * (n – 1)! function base case recursive case 1 long factorial( int n ) { 2 if ( n == 1 ) return 1; 3 return n * factorial( n – 1 ); 4 }

Unwinding Recursive Calls Observe that in many recursive methods, the work is done after the base case has been met and the recursion unwinds back to the original call. In the example here, the computation is done as the recursion unwinds.

Recursion as Iteration Components of a loop and loop terminology loop entry condition Also called the continuation condition; this condition determines if the loop body is to be executed. loop exit condition The condition under which the loop exits; the inverse of the entry condition. loop control variable (LCV) The variable in the loop entry condition that determines if the loop terminates or executes again; the LCV must be updated so that eventually the exit condition is met. loop body The block of code that executes each time the loop entry condition is met.

Recursion as Iteration Comparison of elements of a loop and a recursive function Loop Recursive Method loop control variable method input loop exit condition base case loop entry condition recursive case loop body method body

Iterative and Recursive Solutions long factorial ( int n ) { long f = 1; while ( n > 1 ) { // entry condition met; // iterate again f = f * n; n = n – 1; // update LCV to get // closer to exit // condition and // iterate again } // exit condition: iteration ends // return result f is n! for n >= 0 return f; if ( n == 1 ) // base case; iteration ends return 1; // return a result else // recursive case met: update method // input to get closer to base case // and iterate again return n * factorial( n – 1 );

Recursion, Activation Records, Runtime Stack A process is a program that is executing Each process gets a process stack to store data and other information When a method is called, an activation record (AR) is allocated on top of the process stack. An activation record stores variables local to the invoked method the parameters passed to the method the method’s return value administrative information necessary to restore the run time environment of the calling method when the called method returns This is called stack dynamic allocation because the memory is allocated on the stack at run time when it is needed Contributes to the space complexity of recursive methods since each method call generates an AR

Activation Records & the Process Stack Contents of an activation record method calls   method returns (recursion unwinding) State of the process stack and activation records for an initial call to factorial(4)

Tail Recursion A recursive method call is tail recursive if the recursive call is the last statement to be executed before the method returns Such methods are of interest because they can easily be converted to use a loop to do the iteration Why bother? Because tail recursive methods are particularly inefficient in their use of space

An Example tail recursive – data in // print the values from 0 to values.size - 1 void printArray( int[] values, int n ) { if ( n == values.length ) // base case return; System.out.println( values[n] ); n++; printArray( values, n ); // recursive case } tail recursive – data in the activation record will never be used again

Convert Tail Recursion to a Loop Recursive Version Iterative Version (Tail Recursion Removed) void printArray(int[] values, int n){ if ( n == values.length ) // base case return; System.out.println( values[n] ); n++; printArray( values, n ); // recursive // case } void printArray( int[] values, int n ){ while ( n != values.length ){

Evaluation Recursion How much space activation records require depends on two factors: the size of the activation record, which depends on how many parameters and local variables there are the depth of the recursion; that is, how many calls will be made before the base case is met, at which point no more recursive calls are made space Time The “cost” of the method invocation goes up as the execution time of the body goes down