Presentation is loading. Please wait.

Presentation is loading. Please wait.

Recursion ITFN 2313. The Stack. A data structure maintained by each program at runtime. Push Pop.

Similar presentations


Presentation on theme: "Recursion ITFN 2313. The Stack. A data structure maintained by each program at runtime. Push Pop."— Presentation transcript:

1 Recursion ITFN 2313

2 The Stack. A data structure maintained by each program at runtime. Push Pop

3 The Stack When a function is called, a block of memory is allocated to it in a run-time structure called the stack. It will contain: The function’s local variables, Local copies of the function’s call-by-value parameters, and A return address from where the function was called.

4 Recursion A recursive method is one that calls itself, either directly or indirectly, through another method.

5 Recursion vs. Iteration Conventional problem solving methods decompose the solution into steps, then executes each step to reach the solution Recursive problem solving reduces the same problem into another problem of the exact same type, and solves the new problem. How? By reducing the new problem into another problem of the same type, etc. etc.

6 Recursion vs. Iteration Recursion can replace loops Recursion provides a more “elegant” solution Iterative solutions are typically faster and easier to read Recursion is less efficient due to the overhead associated with maintaining the stack

7 Recursion vs. Iteration So you understand looping, but not recursion? Recursion is more challenging to understand simply because our brains solve problems iteratively.

8 Real World Example You don’t remember the number of your new house You ask the neighbor his number – he doesn’t know his house number either. He suggests checking with his neighbor, then add 1 to get his number.

9 Real World Example And so on … The last house you come to tells you their number is 10 … What do you know now? House 10 Example taken from: http://www.devshed.com/c/a/Practices/Solving-Problems-with-Recursion/1/

10 Real World Example And so on … The last house you come to tells you their number is 10 … What do you know now? 10 + 1House 10 Example taken from: http://www.devshed.com/c/a/Practices/Solving-Problems-with-Recursion/1/

11 Real World Example And so on … The last house you come to tells you their number is 10 … What do you know now? 10 + 1House 1010 + 2 Example taken from: http://www.devshed.com/c/a/Practices/Solving-Problems-with-Recursion/1/

12 Real World Example And so on … The last house you come to tells you their number is 10 … What do you know now? 10 + 1House 1010 + 210 + 3 Example taken from: http://www.devshed.com/c/a/Practices/Solving-Problems-with-Recursion/1/

13 Real World Example And so on … The last house you come to tells you their number is 10 … What do you know now? 10 + 1House 1010 + 210 + 310 + 4 Example taken from: http://www.devshed.com/c/a/Practices/Solving-Problems-with-Recursion/1/

14 Real World Example And so on … The last house you come to tells you their number is 10 … What do you know now? 10 + 1House 1010 + 210 + 310 + 4House 15 Example taken from: http://www.devshed.com/c/a/Practices/Solving-Problems-with-Recursion/1/

15 Two Requirements for Recursion Base Case Non-recursive The simplest case Terminating condition Recursive Call Calls to the current method Solves a smaller problem Should make progress toward the base case

16 Example static int Product(int n) { if (n == 1) return 1; else return n * Product(n-1); }

17 Example static int Product(int n) { if (n == 1) return 1; else return n * Product(n-1); } Terminating Condition Recursive Call

18 Recursive Evaluation To evaluate 5! evaluate 5 * 4! To evaluate 4! evaluate 4 * 3! To evaluate 3! evaluate 3 * 2! To evaluate 2! evaluate 2 * 1! To evaluate 1! Evaluation terminates, returns 1 Evaluate 2 * 1 Return 2 Evaluate 3 * 2 Return 6 Evaluate 4 * 6 Return 24 Evaluate 5 *24 Return 120

19 When to use recursion? There are procedures that are very naturally programmed recursively, and all but unmanageable iteratively Binary Search Trees Turn-Based games in which the computer “thinks”

20 Recursion More to come…


Download ppt "Recursion ITFN 2313. The Stack. A data structure maintained by each program at runtime. Push Pop."

Similar presentations


Ads by Google