11. Recursion_outside

# Binary recursion occurs whenever there are two

• Notes
• 21

This preview shows page 12 - 17 out of 21 pages.

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

Subscribe to view the full document.

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
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

Subscribe to view the full document.

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
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!

Subscribe to view the full document.

You've reached the end of this preview.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern