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

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

View Full Document Right Arrow Icon
Click to edit Master subtitle style © 2009 Dr. Tim Margush Recursion Dr. Tim Margush University of Akron © 2009
Image of page 1

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

View Full Document Right Arrow Icon
© 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
Image of page 2
© 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
Image of page 3

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

View Full Document Right Arrow Icon
© 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
Image of page 4
© 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; }
Image of page 5

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

View Full Document Right Arrow Icon
© 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); }
Image of page 6
© 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
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern