Chapter 22: Recursion Flashcards
What is an anonymous array?
We can avoid declaring a variable to reference an array by providing a list of the elements.
New Type[] {value, value, value}
What is a private constructor method?
Constructors are public because we want to permit other classes to construct objects. Occasionally we want the creation of objects to be tightly controlled by the class itself.
What is recursion?
Something is defined in terms of itself
What is a recursive algorithm?
Algorithm: procedure for undertaking some class.
Recursive: expressed in terms of itself
How do we design a recursive algorithm?
- Base cases- when is the problem solved easily
- Recursive cases- an occurrence of the problem of the same kind but simpler.
- The solution of the simpler problem helps to solve the given one
What is a recursive method?
One that contains one or more method call to itself. Any route not containing a recursive call is the base case
What are two common misunderstandings?
A recursive method call makes execution go back to the beginning: It starts another separate occurrence of the method
There is one copy of each method variable belonging to a method: Each call creates a new set of method variables to be used in that execution
What makes a recursive algorithm well defined?
Clearly identified range of arguments for which it is meant to work
At least one base case that doesn’t make a recursive call
In all recursive cases, arguments passed to the call are nearer to the base case than those given.
Describe the difference between recursion and iteration
Many recursive methods could be easily implemented by iteration. Iteration is an optimized implementation of tail recursion
What is tail recursion?
There is no more work to do after the recursive call has been made
What is multiple recursion?
The activation of a method can cause more than one recursive activation of the same method. Hard to express iteratively
What is a recursive data structure?
A data structure that consists of objects containing a reference to other objects of the same type i.e. binary trees