of 11
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
  Recursion Contents ã Introduction  o Test Yourself #1  ã How Recursion Really Works  o Test Yourself #2  ã Recursion vs Iteration  o Factorial  o Fibonacci  o Test Yourself #3  ã Usin !at eatical Induction to $rove t e %orrectness of Recursive %ode  ã &uary  ã 'nswers to &elf(&tudy )uestions  Introduction Recursion is* ã ' way of t inkin about +robles, ã ' et od for solvin +robles, ã Related to at eatical induction, ' et od is recursive  if it can call itself- eit er directly* void f() { ... f() ... } or indirectly* void f() { ... g() ...}void g() { ... f() ...} You i t wonder about t e followin issues* )* .oes usin recursion usually ake your code faster / '* 0o,  )* .oes usin recursion usually use less memory / '* 0o, )* T en why  use recursion/ '* It soeties akes your code uc simpler!  ne way to t ink about recursion* ã W en a recursive call is ade t e et od clones itself   akin new co+ies of* o t e code o t e local variables wit t eir initial values4 o t e +araeters ã 5ac co+y of t e code includes a arker indicatin t e current +osition, W en a recursive call is ade t e arker in t e old co+y of t e code is 6ust after t e call- t e arker in t e 7cloned7 co+y is at t e beinnin of t e et od, ã W en t e et od returns that  clone oes away but t e +revious ones are still t ere and know w at to e8ecute ne8t because t eir current +osition in t e code was saved indicated by t e arker4, Here9s an e8a+le of a si+le recursive et od* void printInt( int k ) {1. if (k == 0) {2. return;3. }4. System.out.printn( k );!. printInt( k 1 );#. System.out.printn($% done$);} If t e call  printInt(2)  is ade t ree 7clones7 are created as illustrated below*  T e oriinal call causes 2 to be out+ut and t en a recursive call is ade creatin a clone wit k :: 1, T at clone e8ecutes line 1* t e if condition is false- line ;* +rints 1- and line <* akes anot er recursive call creatin a clone wit k :: =, T at clone 6ust returns oes away4 because t e 7if7 condition is true, T e +revious clone e8ecutes line > t e line after its 7arker74 t en returns and siilarly for t e oriinal clone,  0ow let9s t ink about w at we ave to do to ake sure t at recursive et ods work correctly, First consider t e followin recursive et od* void &%d'rint( int k ) { System.out.printn( k ); &%d'rint( k  1 );}  0ote t at a runtie error will occur w en t e call badPrint(2)  is ade in +articular an error essae like 76ava,lan,&tackverflow5rror7 will be +rinted and t e +rora will sto+4, T is is because t ere is no code t at +revents t e recursive call fro bein ade aain and aain and ,,,, and eventually t e +rora runs out of eory to store all t e clones4, T is is an e8a+le of an infinite recursion , It ins+ires* *** RECURSION RULE  *** 5very recursive et od ust ave a ase case  (( a condition under w ic no recursive call is ade (( to +revent infinite recursion, Here9s anot er e8a+le- t is version does ave a base case but t e call badPrint2(2)  will still cause an infinite recursion* void &%d'rint2( int k ) { if (k  0) return; System.out.printn(k); &%d'rint2( k1 );} T is ins+ires* *** RECURSION RULE # *** 5very recursive et od ust ma$e pro%ress  toward t e base case to +revent infinite recursion, &ES& 'OURSEL(   %onsider t e et od  printInt   re+eated below, void printInt( int k ) { if (k == 0) return; System.out.printn( k ); printInt( k 1 );} .oes it obey recursion rules 1 and 2/ 're t ere calls t at will lead to an infinite recursion/ If yes ow could it be fi8ed/ solution   )ow Recursion Really or$s T is is ow et od calls recursive and non(recursive4 really work* ã 't runtie a stack of activation records 'Rs4 is aintained* one 'R for eac active et od w ere 7active7 eans* as been called as not yet returned, ã 5ac 'R includes s+ace for* o t e et od9s +araeters o t e et od9s local variables o t e return address (( w ere in t e code4 to start e8ecutin after t e et odreturns, ã W en a et od is called its 'R is +us ed onto t e stack, T e return address in t at 'R is t e +lace in t e code 6ust after t e call so t e return address is t e 7arker7 for t e +revious 7clone74, ã W en a et od is about to return t e return address in its 'R is saved its 'R is  +o++ed fro t e stack and control is transferred to t e +lace in t e code referred to by t e return address, 58a+le no recursion4* 1. void print*+%r( ,+%r , ) {2. System.out.print(,);3. }4.!. void m%in (...) {#. ,+%r ,+ = -%-;. print*+%r(,+);/. ,+ = -&-;. print*+%r(,+);10. } T e runtie stack of activation records is s own below first as it would be 6ust before t e first call to  printChar   6ust after line >4 and t en as it would be w ile  printChar   is e8ecutin lines 1 ( 34, 0ote t at t e return address stored in  printChar  9s 'R is t e +lace in ain9s code w ere e8ecution will resue w en  printChar   returns,
We Need Your Support
Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

Thanks to everyone for your continued support.

No, Thanks