Java Foundations, Chapter 17, Recursion

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Chapter Objectives

1) explain the concepts of recursion 2) examine recursive methods and unravel their processing steps. 3) Define infinite recursion and discuss ways to avoid it. 4) Explain when recursion should and should not be used. 5) Demonstrate the use of recursion to solve problems.

What is indirect recursion?

Indirect recursion occurs when a method calls another method, which calls another method, and so on until one of the called methods invokes the original method. Indirect recursion is usually more difficult to trace than direct recursion, in which a method calls itself.

Explain the general approach to solving the Towers of Hanoi puzzle. How is it related to recursion?

The Towers of Hanoi puzzle of N disks is solved by moving N-1 disks out of the way onto an extra peg, moving the largest disk to its destination, then moving the N-1 disks from the extra peg to the destination. This solution is inherently recursive because we can use the same process to move the whole substack of N-1 disks.

Infinite recursion

The problem that occurs when a base case is never reached or not defined for an operation.

Indirect recursion

When a method calls another method which calls another method and so on until the original method is called(this is the reverse of direct recursion).

direct recursion

When a method calls itself directly and each time it calls itself the problem set gets smaller until a base case is reached.

Any recursive definition must have a nonrecursive part, called the ___ ___, that permits to recursion to eventually ___.

base case, end

Each recursive call to a method creates new ___ ___ and ____.

local variables, parameters

When is a base case needed for recursive processing?

A base case is always needed to terminate recursion and begin the process of returning through the calling hierarchy. Without the base case, infinite recursion results.

Towers of Hanoi

A classic computing puzzle in which the goal is to move disks from one tower to another under specific rules.

recursion

A programming technique where a method calls itself to fulfill its purpose.

When should recursion be avoided ?

Avoid recursion when the iterative solution is simpler and more easily understood and programmed. Recursion has the overhead of multiple method calls and is not always intuitive.

What is infinite recursion?

Infinite recursion occurs when there is no base case that serves as a terminating condition, or when the base case is improperly specified. The recursive path is followed forever. In a recursive program, infinite recursion often results in an error that indicates that available memory has been exhausted.

base case

It is the simplest possible version of the problem. The part of an operation's definition that is not recursive. Meaning you can't simplify it any more.

____ problems and formulas are often expressed ____.

Mathematical, recursively

What is recursion?

Recursion is a programming technique where a method calls itself, solving a smaller version of the problem each time, until the terminating condition (base case) is reached.

Is recursion necessary?

Recursion is not necessary. Every recursive algorithm can be written in an iterative manner. However, some problem solutions are much more elegant and straightforward when they are written recursively.

Recursion is a programming technique in which a method ______ _____. A key to being able to program recursively is to be able to think recursively.

calls itself

Recursion is the most elegant and appropriate way to solve some problems, but for others it is less intuitive than an _____solution.

iterative

The order of a recursive algorithm can be determined using techniques similar to those used in analyzing _____ processing.

iterative

The Towers of Hanoi solution has exponential complexity, which is very inefficient, but the code is incredibly ____ _ ___.

short and elegant.

A careful trace of recursive processing can provide insight into the way it is used to ___ a ____.

solve, problem


Kaugnay na mga set ng pag-aaral

Human Resource Management Chapter 8: Managing Employee Performance

View Set

Hw: CH 1 INTRODUCTION TO STATISTICS - CH 1.1 Overview

View Set

Unit 3: Expansion and Settlement

View Set

English 10 - Cumulative Exam Review 100%

View Set

Certificate in Principles of PR practice questions

View Set

Period 4 1450 - 1750 CE Crash Course Videos

View Set

Advanced European History 10: Chapter 10 & 11 Test

View Set

Python Cook Book: Chapter 1 Data Structures & Algorithms

View Set

MCN 374 Exam 1 - Ch 1, 2, 4-9, 11, 12, 15, 20

View Set