Understanding Recursion: A Comprehensive Guide, Slides of Java Programming

An in-depth exploration of recursion, a fundamental concept in computer science. Recursion is a method where a function calls itself, and this document covers its basics, examples, and applications. Topics include recursive calls, ending cases, recursive delete, fibonacci sequence, root finding, and dictionary search.

Typology: Slides

2012/2013

Uploaded on 04/23/2013

sarmistha
sarmistha 🇮🇳

3.8

(22)

112 documents

1 / 16

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Recursion
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Understanding Recursion: A Comprehensive Guide and more Slides Java Programming in PDF only on Docsity!

Recursion

Announcements

Change in schedule!

  • Tomorrow we are doing Ch 12 (not more of Ch 11) 70 people filled out the survey (10 short) We will do the whole book, but in less detail

Highlights

  • Recursion

Recursion

No fancy blue words or classes this chapter Recursion is simply calling a method from inside itself This copy will re-run the method on any new arguments or information (See: BadRecursion.java)

Recursion

Recursion basics

Good recursion must have 2 parts:

  • A recursive call on a smaller problem
  • An ending case In order to use recursion, you must be able to identify a subproblem that is very similar to the original problem Each step must get you closer to the solution

Recursion: In words

A child couldn't sleep, so her mother told a story about a little frog, who couldn't sleep, so the frog's mother told a story about a little bear, who couldn't sleep, so bear's mother told a story about a little weasel ...who fell asleep. ...and the little bear fell asleep; ...and the little frog fell asleep; ...and the child fell asleep. Docsity.com

Recursion: Basic example

Remember, code starts in main and runs from top to bottom in sequence (normally) When you call a method you go execute all the method code is run before going back to the original code This means code order is important in recursion (See: StringRecursion.java)

Recursion: Fibonacci

The Fibonacci numbers are defined as: F(n) = F(n-1) + f(n-2) In other words, you add the previous two to get the next This is recursion! Computing F(n) involves solving smaller problems of F(n-1) (See: FibonacciRecursion.java) Docsity.com

Recursion: Root finding

Find a root of:

Ending remarks

"To understand recursion, you must understand recursion." Try googling “recursion” and click on the spelling suggestion We will probably see more of this when we talk about linked lists (Some data types are conducive to recursion)Docsity.com