10_Recursion

# 10_Recursion - Recursion Click to edit Master subtitle...

This preview shows pages 1–8. Sign up to view the full content.

Click to edit Master subtitle style © 2009 Dr. Tim Margush Recursion Dr. Tim Margush University of Akron © 2009

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© 2009 Dr. Tim Margush Goals Be able to describe a recursive solution to a problem Know the basic techniques for writing a recursive method Identify tail recursion and convert it to iteration (c) 2009 Tim margush
© 2009 Dr. Tim Margush Recursion A seemingly circular definition or solution The solution requires the use of the solution, usually applied to solve a simpler sub problem that will in turn help solve the original problem A method that calls itself as one or more of its internal steps (c) 2009 Tim margush

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© 2009 Dr. Tim Margush a+b Many simple operations can be cast into a recursive definition If we know how to increment and decrement, but are not good with general addition, then a+b = (a++) + (b--) if b>0 = a if b==0 = (a--) + (b++) if b<0 If powers give us problems, then an = a(an-1) if n>0 = 1 if n==0
© 2009 Dr. Tim Margush Translating to Java public int add(int a, int b){ if (b>0) return add(a++, b--); else if (b==0) return a; else return add(a--, b++); } private int power(int a, int n){ if (n>0) return a*power(a,n-1); else return 1; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
© 2009 Dr. Tim Margush contains In a linked list, contains might be written recursively (c) 2009 Tim margush public boolean contains(E target){ return contains(head, target); } private static boolean contains(Node h, E target){ if (h==null) return false; else if (h.data.equals(target)) return true; else return contains(h.next, target); }
© 2009 Dr. Tim Margush Recursive Definitions Require a base case An answer that does not make a circular reference b = 0; a + b = a n = 0; power (a,0) = 1 Require a recursive case

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

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