Meljun Cortes Data Structures Recursion

Embed Size (px)

Citation preview

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    1/19

    Data Structures

    Recursion

    technique in programming that calls itself

    has the capability to save the condition it was inor the particular process it was serving when

    calling itself

    The design of a recursive method consists ofthe following elements:

    One or more stopping conditions that can

    Recursion * Property of STI Page 1 of 19

      .

    One or more recursive steps , in which acurrent value of the method can be

    computed by repeated calling of the methodwith arguments that will eventually arrive at

    a stopping condition

    Two classes of recursion

    Direct Recursion

    Indirect Recursion

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    2/19

    Data Structures

    Recursion

    Recursion is viewed as a somewhat mysticaltechnique which is only useful for some very

    special class of problems

    Can be written using

    Assignment statement

    If-else statement

    While statement

     

    Recursion * Property of STI Page 2 of 19

    an a so e wr en us ng

    o Assignment statement

    o If-else statemento recursion

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    3/19

    Data Structures

    Triangular Numbers

    the sum of 1 to n numbers arranged inequilateral triangle

    seen by row where each row is longer than the

    previous row

    the sum of consecutive integers

    Recursion * Property of STI Page 3 of 19

    Total = 6

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    4/19

    Data Structures

    Triangular Numbers

    int triangle(int n)

    {

    int total = 0;

    while(n > 0) // until n

    is 1{

    total = total + n; // add n

    (column height) to total

    Recursion * Property of STI Page 4 of 19

    --n; // decrement

    column height

    }return total;

    }

    Finding the Nth term using Recursion 

    The first (tallest) column, which has the value of

    n. The sum of all the remaining columns.

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    5/19

    Data Structures

    Triangular Numbers

    To find the remaining column:

    int triangle(int n)

    {

    return (n +

    sumRemainingColumns(n));}

    find the sum of all the remaining columns for

    Recursion * Property of STI Page 5 of 19

    term n:

    int triangle(int n)

    {

    return(n + sumAllColumns(n-1));

    }

    Instead

    int triangle(n)

    {

    return(n + triangle(n-1));

    }

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    6/19

    Data Structures

    Triangular Numbers

     

    Recursion * Property of STI Page 6 of 19

     

       

     

     

     

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    7/19

    Data Structures

    Triangular Numbers

    Recursive algorithm

    There must be at least one case (the basecase), for a small value of n that can be

    solved directly

    A problem of a given size n can be split intoone or more smaller versions of the sameproblem

    Identify the base case and provide asolution to it

    Recursion * Property of STI Page 7 of 19

     

    Create a strategy to split the problem intosmaller versions of itself while making

    progress toward the base case Merge the solution that will solve the larger

    problem

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    8/19

    Data Structures

    Factorial

    It is the product of a series of consecutivepositive integers from 1 to a given number, n

    Expressed by the symbol !

    Number Calculation Factorial0 By definition 1

    1 1 * 1 1

     

    Recursion * Property of STI Page 8 of 19

    Factorial of 6

    2 2 * 1 2

    3 3 * 2 6

    4 4 * 6 24

    5 5 * 24 120

    6 6 * 120 720

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    9/19

    Data Structures

    Anagrams

    forming of real English word using the sameletters, no more, no less, the process is calledanagramming 

    Farm Armf Rmfa Mfar

    Famr Arfm Rmaf Mfra

    Recursion * Property of STI Page 9 of 19

    Anagram of the word “Farm”

    Frma Amfr Rfam Marf

    Fram Amrf Rfma Mafr

    Fmar Afrm Ramf Mrfa

    Fmra Afmr Rafm Mraf

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    10/19

    Data Structures

    Anagrams

    Algorithm to anagram a word

    Anagram the rightmost n-1 letters

    Rotate all n letters

    Repeat these steps n time

    Recursion * Property of STI Page 10 of 19

    Rotating a word

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    11/19

    Data Structures

    Anagrams

     public static void rotate(int newSize)

    {

    int i;

    int position = size - newSize;

    // save first letterchar temp = charArray[position];

    //shift others left

    for (i = position + 1; i < size;

    Recursion * Property of STI Page 11 of 19

    i++)

    charArray[i - 1] = charArray[i];

    //put first on right

    charArray[i - 1] = temp;

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    12/19

    Data Structures

    Recursive Binary Search

    Algorithm for recursive binary search

    Search in the array A in positions numberedloIndex to hiIndex inclusive, for the specified

    value

    If the value is found, return the index in thearray where it occurs.

    If the value is not found, return -1

    Precondition: The array must be sorted into

    Recursion * Property of STI Page 12 of 19

     

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    13/19

    Data Structures

    Towers of Hanoi

    invented by Edouard Lucas in 1883

    its objective is to transfer the entire tower to thelast peg, moving only one disk at a time without

    moving a larger one onto a smaller

    Recursion * Property of STI Page 13 of 19

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    14/19

    Data Structures

    Towers of Hanoi

    Algorithm for Towers of Hanoi

    Move the smaller disc from the top n-1 from A toB

    Move the remaining (larger) disc from A to C

    Move the smaller disc from B to C

    Recursion * Property of STI Page 14 of 19

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    15/19

    Data Structures

    Towers of Hanoi

    Recursion * Property of STI Page 15 of 19

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    16/19

    Data Structures

    Towers of Hanoi

    doTowers(int topN, char from, char inter,

    char to)

    {

    if(topN==1)

    System.out.println("Disc 1 from "

    + from + " to " + to);

    else

    {

    doTowers(topN-1, from, to,

    Recursion * Property of STI Page 16 of 19

    nter ; rom--> nter

    System.out.println("Disc " +

    topN + "from " + from + "to " + to);

    doTowers(topN-1, inter, from,

    to); // inter-->to

    }

    }

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    17/19

    Data Structures

    Mergesort

    requires another array in the memory

    Algorithm for mergesort

    Divide the list into two, with your desiredsize

    Sort the first and second list Megre the two sublists into one sorted list

    Recursion * Property of STI Page 17 of 19

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    18/19

    Data Structures

    Mergesort

    Step Comparison Copy

    1 Compare 12 and 8 Copy 8 from arr1 to arr3

    2 Compare 12 and 17 Copy 12 from arr1 to arr3

    3 Compare 17 and 15 Copy 15 from arr1 to arr3

    4 Compare 17 and 40 Copy 17 from arr2 to arr3

    Recursion * Property of STI Page 18 of 19

    Merging Process

    6 Compare 40 and 56 Copy 40 from arr1 to arr3

    7 Compare 56 and 42 Copy 42 from arr1 to arr3

    8 Compare 56 and 57 Copy 56 from arr2 to arr3

    9 Compare 57 and 88 Copy 57 from arr1 to arr3

    10 Compare 88 and 89 Copy 88 from arr2 to arr3

    11 Copy 89 from arr1 to arr3

  • 8/21/2019 Meljun Cortes Data Structures Recursion

    19/19

    Data Structures

    Types of Recursion

    Linear Recursion 

    Tail Recursion 

    Binary Recursion 

    Exponential Recursion 

    Nested Recursion 

    Mutual Recursion 

    Recursion * Property of STI Page 19 of 19

    Depth of Recursion

    number of times the procedure is called

    recursively in the process of evaluating agiven argument