Upcoming SlideShare
×

# Week11

165 views
92 views

Published on

Published in: Technology, Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total views
165
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
2
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Week11

1. 1. Week 11Recursion
2. 2. © 2004 Pearson Addison-Wesley. All rights reserved 5-2Copyright WarningCOMMONWEALTH OF AUSTRALIACopyright Regulations 1969WARNINGThis material has been copied and communicated to you by oron behalf of Bond University pursuant to Part VB of theCopyright Act 1968 (the Act).The material in this communication may be subject to copyrightunder the Act. Any further copying or communication of thismaterial by you may be the subject of copyright protectionunder the Act.Do not remove this notice.
3. 3. © 2004 Pearson Addison-Wesley. All rights reserved 5-3What is Recursion?• Recursion is a technique, which is used to solve aspecific class of problems• With these type of problems: You can solve one part of the problem, and This leaves a smaller problem which is exactly the sametype as the original problem• 99% of problems do not need recursion.• However, 1% of problems do need recursion!• The hardest part of recursion is how to visualise aproblem and see how we can apply the recursiontechnique• Hint: if you are asked to do something recursively,it can be done that way. Try to visualise how
4. 4. © 2004 Pearson Addison-Wesley. All rights reserved 5-4What Problems Can Use Recursion?• In order to solve a problem with recursion, youneed 3 things to be true:1 Given a problem, you can solve part of the problemimmediately2 This leaves a smaller problem of exactly the same type3 There is a “base case”, i.e. a simplest problem, which canbe solved without step 2.• Step 2 is the “recursion” part of the problemsolving• Lets have a look at a game which uses recursion
5. 5. © 2004 Pearson Addison-Wesley. All rights reserved 5-5Pass the Parcel• Problem: unwrap the layers of paper around aparcel in order to reveal the present inside• We can solve this iteratively: While (there is paper) remove paper.• Lets also solve this recursively:1 Unwrap one piece of paper2 If (we find the prize) stop3 Otherwise, pass the parcel to someone else• We solve part of the problems with step 1. Thisleaves a smaller problem which is solved with step3. The base case is step 2.• Note: the 3 steps for recursion dont have to occurin order
6. 6. © 2004 Pearson Addison-Wesley. All rights reserved 5-6Implementing Recursion in Java• Remember, we have to think of a recursivesolution to the problem first!• We use a method to solve part of the problem, i.e.step 1.• To solve the smaller problem, we call a newinstance of the same method with the left-overproblem: this is step 2, i.e. the recursion.• We need to ensure that, at some point, we wontcall a new instance of the same method: this isstep 3, i.e. the base case.• At this point, a typical question: how can a Javamethod call itself? What does this mean?
7. 7. © 2004 Pearson Addison-Wesley. All rights reserved 5-7Methods Can Call Methods• We know that methods can call other methods:public static void main(){int age;System.out.print("Whats your age: ");age= scan.nextInt();}• What would happen if a method called itself?• Lets see...
8. 8. © 2004 Pearson Addison-Wesley. All rights reserved 5-8The Blah Methodint blah(int value){int result;result = value * blah(value -1);return(result);}• If in main() we do x= blah(4); what happens? blah() tries to calculate 4 * blah(3) but then another blah() tries to calculate 3*blah(2)• This thing is never going to stop! So its prettyuseless.• What if we make it stop when the value is 1? blah(X) is X * blah(X-1) but blah(1) is always exactly 1
9. 9. © 2004 Pearson Addison-Wesley. All rights reserved 5-9A Blah Method That Stopsint blah(int value){int result;if (value == 1)result = 1;elseresult = value * blah(value-1);return(result);}• This is known as a recursive method or a recursivefunction: the operation depends on itself.• For it to work properly, the function at some pointmust not call itself. It must bottom out.
10. 10. © 2004 Pearson Addison-Wesley. All rights reserved 5-10The Factorial Function• In maths, Fact(X) = X * all the numbers below it• For example, Fact(7) = 7 * 6 * 5 * 4 * 3 * 2 * 1• But note: Fact(7) = 7 * (6 * 5 * 4 * 3 * 2 * 1), soFact(7) = 7 * Fact(6)• So, we can also define Fact(X) recursively: Fact(X) = X * Fact(X-1), when X>1 Fact(1) = 1• Hmm, thats exactly the blah() method we havealready written. Lets rename it factorial().