RecursionRecursive Function CallFinding a Recursive SolutionMathemetical InductionGeneral format for Many Recursive FunctionsA recursive definitionWhen a function is called...Stack Activation FramesA Stake of Activation RecordsA recursive functionRun-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100Slide 12Slide 13Slide 14Slide 15Slide 16Too much recursion Can Be DangerousToo much Recursion Can be DangerousTreeTraversalRecursive Traversal ImplementationSome more examples:Divide and ConquerRecursion or Iteration?Pei Zheng, Michigan State University1 RecursionDr. Bernard Chen Ph.D.University of Central ArkansasFall 20082Recursive Function Calla recursion function is a function that either directly or indirectly makes a call to itself. but we need to avoid making an infinite sequence of function calls (infinite recursion)3Finding a Recursive Solutiona recursive solution to a problem must be written carefully the idea is for each successive recursive call to bring you one step closer to a situation in which the problem can easily be solved this easily solved situation is called the base case each recursive algorithm must have at least one base case, as well as a general (recursive) case4Mathemetical InductionTo prove• Let p(n) denote the statement involving the integer variable n. The Principle of Mathematical Induction states: If p(1) is true and, for some integer K >=1 , p(k+1) is true whenever p(k) is true then p(n) is true for all n>=1 . • 4 steps in using Induction: Base cases; --- p(1), p(2), … Induction hypothesis (IH); --- assume p(k) is true Statement to be proved in induction; --- it is true for p(k+1) Induction step. --- prove p(k+1) is true based on IH5General format forMany Recursive Functions if (some easily-solved condition) // base case solution statement else // general case recursive function call6A recursive definitionint s (int n){ if (n ==1) return 1; else return s(n-1) + n;}A few of problems:• n 0 at the beginning;• return value might be too large to fit in an int.7When a function is called...a transfer of control occurs from the calling block to the code of the function--it is necessary that there be a return to the correct place in the calling block after the function code is executed; this correct place is called the return address when any function is called, the run-time stack is used--on this stack is placed an activation record for the function call8Stack Activation Framesthe activation record contains the return address for this function call, and also the parameters, and local variables, and space for the function’s return value, if non-void the activation record for a particular function call is popped off the run-time stack when the final closing brace in the function code is reached, or when a return statement is reached in the function codeat this time the function’s return value, if non-void, is brought back to the calling block return address for use there9A Stake of Activation RecordsS(1)S(2)S(3)S(4)Main()10int Func ( /* in */ int a, /* in */ int b ){ int result; if ( b == 0 ) // base case result = 0;else if ( b > 0 ) // first general case result = a + Func ( a , b - 1 ) ) ; // instruction 50return result; } A recursive function11 FCTVAL ? result ? b 2 a 5Return Address 100 Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 100 original call at instruction 100 pushes on this record for Func(5,2)12 FCTVAL ? result ? b 1 a 5Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5Return Address 100record for Func(5,2)call in Func(5,2) codeat instruction 50 pushes on this recordfor Func(5,1) Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 10013 FCTVAL ? result ? b 0 a 5Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 a 5Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5Return Address 100record for Func(5,2)record for Func(5,1)call in Func(5,1) codeat instruction 50pushes on this record for Func(5,0) Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 10014 FCTVAL 0 result 0 b 0 a 5Return Address 50 FCTVAL ? result 5+Func(5,0) = ? b 1 a 5Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5Return Address 100record for Func(5,0)is popped first with its FCTVALrecord for Func(5,2)record for Func(5,1) Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 10015record for Func(5,2)record for Func(5,1)is popped nextwith its FCTVAL FCTVAL 5 result 5+Func(5,0) = 5+ 0 b 1 a 5Return Address 50 FCTVAL ? result 5+Func(5,1) = ? b 2 a 5Return Address 100 Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 10016 FCTVAL 10 result 5+Func(5,1) = 5+5 b 2 a 5Return Address 100record for Func(5,2)is popped lastwith its FCTVAL Run-Time Stack Activation Records x = Func(5, 2);// original call at instruction 10017Too much recursion Can Be DangerousFibonacci numbers.Long fib (int n){ If (n <=1) return n; Else return fib(n-1) + fib(n-2);}18Too much Recursion Can be DangerousThis definition will lead to exponential running time.Reason: -- too much redundant workNot necessary to use recursive19TreeTree is a fundamental structure in computer science.Recursive definition: A tree is a root and zero or more nonempty subtrees.Nonrecursive definition: A tree consists of s set of nodes and a set of directed edges that connect pairs of nodes. That is, a connected graph without loop.20TraversalThree standard traversal orderpreorder - V L
View Full Document