11. Recursion_outside

Binary recursion occurs whenever there are two

Info icon This preview shows pages 12–17. Sign up to view the full content.

View Full Document Right Arrow Icon
Binary recursion occurs whenever there are  two   recursive calls for each non-base case. Example: the DrawTicks method for drawing ticks  on an English ruler. ©  2010 Goodrich, Tamassia
Image of page 12

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using Recursion 13 A Binary Recursive Method for  Drawing Ticks // draw a tick with no label public static void drawOneTick ( int tickLength ) { drawOneTick ( tickLength , - 1 ); } // draw one tick public static void drawOneTick ( int tickLength , int tickLabel ) { for ( int i = 0 ; i < tickLength ; i ++) System . out . print ( "-" ); if ( tickLabel > = 0 ) System . out . print ( " " + tickLabel ); System . out . print ( "\n" ); } public static void drawTick s ( int tickLength ) { // draw ticks of given length if ( tickLength > 0 ) { // stop when length drops to 0 drawTicks ( tickLength- 1 ); // recursively draw left ticks drawOneTick ( tickLength ); // draw center tick drawTicks ( tickLength- 1 ); // recursively draw right ticks } } public static void drawRuler ( int nInches , int majorLength ) { // draw ruler drawOneTick ( majorLength , 0 ); // draw tick 0 and its label for ( int i = 1 ; i < = nInches ; i ++) { drawTicks ( majorLength- 1 ); // draw ticks for this inch drawOneTick ( majorLength , i ); // draw tick i and its label } } Note the two  recursive calls ©  2010 Goodrich, Tamassia
Image of page 13
Using Recursion 14 Another Binary Recusive Method Problem: add all the numbers in an integer array A: Algorithm  BinarySum( A, i, n ):       Input:  An array  and integers  and  n       Output:  The sum of the  integers in  starting at index  i      if  = 1  then return  A [ ]      return  BinarySum( A, i, n/  2) + BinarySum( A, i  n/  2 , n/  2) Example trace: 3, 1 2, 2 0, 4 2, 1 1, 1 0, 1 0, 8 0, 2 7, 1 6, 2 4, 4 6, 1 5, 1 4, 2 4, 1 ©  2010 Goodrich, Tamassia
Image of page 14

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using Recursion 15 Computing Fibonacci  Numbers Fibonacci numbers are defined recursively: F 0 = 0 F 1 = 1 F i = F i - 1 + F i - 2 for i > 1. Recursive algorithm (first attempt): Algorithm BinaryFib ( k ) : Input: Nonnegative integer k Output: The k th Fibonacci number F k if k = 1 then return k else return BinaryFib ( k - 1 ) + BinaryFib ( k - 2 ) ©  2010 Goodrich, Tamassia
Image of page 15
Using Recursion 16 Analysis Let n k  be the number of recursive calls by  BinaryFib (k) n 0  = 1 n 1  = 1 n 2  =  n 1  +  n 0  + 1 = 1 + 1 + 1 = 3 n 3  =  n 2  +  n 1  + 1 = 3 + 1 + 1 = 5 n 4  =  n 3  +  n 2  + 1 = 5 + 3 + 1 = 9 n 5  =  n 4  +  n 3  + 1 = 9 + 5 + 1 = 15 n 6  =  n 5  +  n 4  + 1 = 15 + 9 + 1 = 25 n 7  =  n 6  +  n 5  + 1 = 25 + 15 + 1 = 41 n 8  =  n 7  +  n 6  + 1 = 41 + 25 + 1 = 67 . Note that n k  at least doubles every other time That is, n k  > 2 k/2 . It is exponential!
Image of page 16

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 17
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern