Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE110 Lecture 26A

CSE110 Lecture 26A

Principles of Programming with Java
Recursion
(202007)

Javier Gonzalez-Sanchez
PRO

June 27, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. CSE110
    Principles of Programming
    with Java
    Lecture 26:
    Recursion
    Javier Gonzalez-Sanchez
    [email protected]
    javiergs.engineering.asu.edu | javiergs.com
    Office Hours: By appointment

    View Slide

  2. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 3
    class
    Previously
    main()

    View Slide

  3. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 4
    class
    Previously
    main()
    class
    class
    class

    View Slide

  4. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 5
    One last thing
    Recursion is a programming technique in which a
    method can call itself.

    View Slide

  5. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 6
    Case A
    public static void main(String[]args) {
    sayHello();
    }
    public static void sayHello() {
    System.out.println("Hello");
    sayHello(); // recursive
    }

    View Slide

  6. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 7
    Case B
    public static void main(String[]args) {
    sayHello(5);
    }
    // a recursive method will always have a condition
    public static void sayHello(int num) {
    if (num == 0)
    System.out.println("Hello and Bye!");
    else {
    System.out.println("Hello");
    sayHello(num-1); // recursive
    }
    }

    View Slide

  7. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 8
    Case C
    public static void main(String[]args) {
    printNumbers(10);
    }
    // a recursive method will always have a condition
    public static void printNumbers(int num) {
    if (num == 0)
    System.out.println(num);
    else {
    System.out.println(num);
    printNumbers(num-1); // recursive
    System.out.println(num);
    }
    }

    View Slide

  8. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 9
    Recursion
    • Each time a recursive method is called, a new copy
    of the method runs, with new instances of
    parameters and local variables being created
    • As each copy finishes executing, it returns to the
    copy of the method that called it
    • When the initial copy finishes executing, it returns to
    the part of the program that made the initial call to
    the method

    View Slide

  9. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 10
    Types of Recursion
    • Direct recursion: a method calls itself
    • Indirect recursion: method A calls method B, and
    method B calls methodA. Or, method A calls
    method B, which calls ..., which calls method A

    View Slide

  10. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 11
    Recursive Factorial Method
    The natural definition of some problems leads to a
    recursive solution
    • The factorial of a nonnegative integer n is the
    product of all positive integers less or equal to n
    • Factorial of n is
    n * (n-1) * ... * 2 x 1
    • The factorial of 0 is 1

    View Slide

  11. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 12
    Recursive Factorial Method
    // Factorial of n can be expressed in terms of
    // the factorial of n-1
    // 0 ! =1
    // n ! = n x (n-1) !
    public static int factorial(int n) {
    if (n == 0)
    return 1;
    else
    return n*factorial(n-1);
    }

    View Slide

  12. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 13
    Recursive Fibonacci Numbers
    The natural definition of some problems leads to a
    recursive solution
    • The Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...
    • After the starting 0, 1, each term is the sum of the
    two preceding terms
    • Recursive solution:
    fib(n) = fib(n – 1) + fib(n – 2);
    • Base cases: fib(0) =0, fib(1) =1

    View Slide

  13. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 14
    Recursive Fibonacci Numbers
    public static int fib (int n) {
    if (n <= 0)
    return 0;
    else if (n == 1)
    return 1;
    else
    return fib(n – 1) + fib(n – 2);
    }

    View Slide

  14. Javier Gonzalez-Sanchez | CSE110 | Summer 2020 | 15
    Reference
    Chapter 5, pages 228 - 232

    View Slide

  15. CSE110 - Principles of Programming
    Javier Gonzalez-Sanchez
    [email protected]
    Summer 2020
    Disclaimer. These slides can only be used as study material for the class CSE110 at ASU. They cannot be distributed or used for another purpose.

    View Slide