CS 2110 Lecture Notes - Lecture 9: Pariah Dog, Popping

12 views7 pages
Published on 16 Mar 2017
School
Cornell University
Department
Computer Science
Course
CS 2110
Professor
Lecture 9 More Recursion
Primitive types vs Reference types
== vs equals
a== opaes a ad ’s alues
a.equals(b) compares the two objects using the equals method
Executing Recursive Methods
1. Push frame for call onto stack
2. Assign arg values to pars
3. Execute method bodies
4. Pop frame from stack and push return value on stack
Understanding Recursive Methods
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 7 pages and 3 million more documents.

Already have an account? Log in
1. Have a precise specification
2. Check that the method works in the base case(s).
3. Look at the recursive case(s). In your mind, replace each recursive call by
what it does according to the spec and verify correctness.
4. (No infinite recursion) Make sure that the args of recursive calls are in
some sense smaller than the pars of the method
Examples with len, dup, isPal
Efficiency with recursion
Calculating 2^n
Rathe tha 2*2*2*2…. N ties
(2*2)^n/2!!!!!!! If n is even
Recursion is a convenient and powerful way to define functions
Poles that see isuoutale a ofte e soled i a diide-and-
oue fashio:
Reduce a big problem to smaller
problems of the same kind, solve the smaller problems
Recombine the solutions to smaller
problems to form solution for big problem
find more resources at oneclass.com
find more resources at oneclass.com
Unlock document

This preview shows pages 1-2 of the document.
Unlock all 7 pages and 3 million more documents.

Already have an account? Log in

Document Summary

A. equals(b) compares the two objects using the equals method. Executing recursive methods: push frame for call onto stack, assign arg values to pars, execute method bodies, pop frame from stack and push return value on stack. Understanding recursive methods: have a precise specification, check that the method works in the base case(s), look at the recursive case(s). Recursion is a convenient and powerful way to define functions. P(cid:396)o(cid:271)le(cid:373)s that see(cid:373) i(cid:374)su(cid:396)(cid:373)ou(cid:374)ta(cid:271)le (cid:272)a(cid:374) ofte(cid:374) (cid:271)e sol(cid:448)ed i(cid:374) a (cid:862)di(cid:448)ide-and- (cid:272)o(cid:374)(cid:395)ue(cid:396)(cid:863) fashio(cid:374): Reduce a big problem to smaller problems of the same kind, solve the smaller problems. Recombine the solutions to smaller problems to form solution for big problem. Collection objects of type e. no order. Imagine just a box with a bunch of. This is also an interface, but has. Key is the first argument in put. So this creates a mapping of an obejct from one type to another. Does(cid:374)"t (cid:374)eed to (cid:271)e the sa(cid:373)e types.