0% found this document useful (0 votes)
6 views35 pages

Recursion Class

Uploaded by

duxingrui0628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views35 pages

Recursion Class

Uploaded by

duxingrui0628
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

Introduction to Java Programming and Data

Structures
Twelfth Edition

Recursion and Generics

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Motivations (1 of 2)
Suppose you want to find all the files under a directory that
contains a particular word. How do you solve this problem?
There are several ways to solve this problem. An intuitive
solution is to use recursion by searching the files in the
subdirectories recursively.

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Motivations (2 of 2)
H-trees, depicted in Figure 18.1, are used in a very large-
scale integration (VLSI) design as a clock distribution
network for routing timing signals to all parts of a chip with
equal propagation delays. How do you write a program to
display H-trees? A good approach is to use recursion.

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Computing Factorial (1 of 11)
Mathematic notation:
n! = n * (n-1)!, n > 0
0! = 1

Function:
factorial(0) = 1;
factorial(n) = n*factorial(n-1); n > 0

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Computing Factorial (2 of 11)
factorial(0) 1;
factorial(n)  n*factorial(n  1);
factorial(4)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Computing Factorial (3 of 11)
factorial(0) 1;
factorial(n)  n*factorial(n  1);
factorial(4) 4 * factorial(3)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Computing Factorial (4 of 11)
factorial(0) 1;
factorial(n)  n*factorial(n  1);
factorial(4) 4 * factorial(3)
4 * 3 * factorial(2))

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Computing Factorial (5 of 11)
factorial(0) 1;
factorial(n)  n*factorial(n  1);
factorial(4) 4 * factorial(3)
4 * 3 * factorial(2)
4 * 3 * (2 * factorial(1))

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Computing Factorial (6 of 11)
factorial(0) 1;
factorial(n)  n*factorial(n  1);
factorial(4) 4 * factorial(3)
4 * 3 * factorial(2)
4 * 3 * (2 * factorial(1))
4 * 3 * (2 * (1* factorial(0)))

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Computing Factorial (7 of 11)
factorial(0) 1;
factorial(n)  n*factorial(n  1);
factorial(4) 4 * factorial(3)
4 * 3 * factorial(2)
4 * 3 * (2 * factorial(1))
4 * 3 * (2 * (1* factorial(0)))
4 * 3 * (2 * (1* 1)))

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Computing Factorial (8 of 11)
factorial(0) 1;
factorial(n)  n*factorial(n  1);
factorial(4) 4 * factorial(3)
4 * 3 * factorial(2)
4 * 3 * (2 * factorial(1))
4 * 3 * (2 * (1* factorial(0)))
4 * 3 * (2 * (1* 1)))
4 * 3 * (2 * 1)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Computing Factorial (9 of 11)
factorial(0) 1;
factorial(n)  n*factorial(n  1);
factorial(4) 4 * factorial(3)
4 * 3 * factorial(2)
4 * 3 * (2 * factorial(1))
4 * 3 * (2 * (1* factorial(0)))
4 * 3 * (2 * (1* 1)))
4 * 3 * (2 * 1)
4 * 3 * 2

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Computing Factorial (10 of 11)
factorial(0) 1;
factorial(n)  n*factorial(n  1);
factorial(4) 4 * factorial(3)
4 * (3 * factorial(2))
4 * (3 * (2 * factorial(1)))
4 * (3 * (2 * (1* factorial(0))))
4 * (3 * (2 * (1* 1))))
4 * (3 * (2 * 1))
4 * (3 * 2)
4 * (6)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Computing Factorial (11 of 11)
factorial(0) 1;
factorial(n)  n*factorial(n  1);
factorial(4) 4 * factorial(3)
4 * (3 * factorial(2))
4 * (3 * (2 * factorial(1)))
4 * (3 * (2 * (1* factorial(0))))
4 * (3 * (2 * (1* 1))))
4 * (3 * (2 * 1))
4 * (3 * 2)
4 * (6)
24
Copyright © 2020 Pearson Education, Inc. All Rights Reserved
Trace Recursive Factorial (1 of 11)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Trace Recursive Factorial (2 of 11)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Trace Recursive Factorial (3 of 11)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Trace Recursive Factorial (4 of 11)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Trace Recursive Factorial (5 of 11)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Trace Recursive Factorial (6 of 11)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Trace Recursive Factorial (7 of 11)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Trace Recursive Factorial (8 of 11)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Trace Recursive Factorial (9 of 11)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Trace Recursive Factorial (10 of 11)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Trace Recursive Factorial (11 of 11)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


factorial(4) Stack Trace

5 Space Required
for factorial(0)

4 Space Required Space Required


for factorial(1) for factorial(1)

3 Space Required Space Required Space Required


for factorial(2) for factorial(2) for factorial(2)

2 Space Required Space Required Space Required Space Required


for factorial(3) for factorial(3) for factorial(3) for factorial(3)

1 Space Required Space Required Space Required Space Required Space Required
for factorial(4) for factorial(4) for factorial(4) for factorial(4) for factorial(4)

6 Space Required
for factorial(1)
Space Required 7 Space Required
for factorial(2) for factorial(2)
Space Required Space Required 8 Space Required
for factorial(3) for factorial(3) for factorial(3)
Space Required Space Required Space Required 9 Space Required
for factorial(4) for factorial(4) for factorial(4) for factorial(4)

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Other Examples
f(0) 0;
f(n) n  f(n  1);

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Fibonacci Numbers (1 of 2)
Fibonacci series: 0 1 1 2 3 5 8 13 21 34 55
89…
indices: 0 1 2 3 4 5 6 7 8 9 10 11
fib(0) 0;
fib(1) 1;
fib(index) fib(index  1)  fib(index  2); index 2
fib(3) fib(2)  fib(1) (fib(1)  fib(0))  fib(1) (1  0)
fib(1) 1  fib(1) 1  1 2

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Problem Solving Using Recursion (1 of 2)
In general, to solve a problem using recursion, you break it
into subproblems. If a subproblem resembles the original
problem, you can apply the same approach to solve the
subproblems recursively. A subproblem is almost the same
as the original problem in nature with a smaller size.

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Characteristics of Recursion
• All recursive methods have the following characteristics:
– The method is implemented using a conditional
statement that leads to different cases.
– One or more base cases (the simplest case) are used
to stop recursion.
– Every recursive call reduces the original problem,
bringing it increasingly closer to a base case until it
becomes that case.

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Generics

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


What Is Generics?
Generics is the capability to parameterize types. With this
capability, you can define a class or a method with generic
types that can be substituted using concrete types by the
compiler. For example, you may define a generic stack
class that stores the elements of a generic type. From this
generic class, you may create a stack object for holding
strings and a stack object for holding numbers. Here,
strings and numbers are concrete types that replace the
generic type.

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Why Generics?
The key benefit of generics is to enable errors to be
detected at compile time rather than at runtime. A generic
class or method permits you to specify allowable types of
objects that the class or method may work with. If you
attempt to use the class or method with an incompatible
object, a compile error occurs.

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


No Casting Needed
ArrayList<Double> list = new ArrayList<>();
list.add(5.5); // 5.5 is automatically converted to new
Double(5.5)
list.add(3.0); // 3.0 is automatically converted to new
Double(3.0)
Double doubleObject = list.get(0); // No casting is needed
double d = list.get(1); // Automatically converted to double

Copyright © 2020 Pearson Education, Inc. All Rights Reserved


Declaring Generic Classes and Interfaces

Copyright © 2020 Pearson Education, Inc. All Rights Reserved

You might also like