Thinking Recursively xkcd #878
Recursion Recursive call is divider – Instructions before happen as stack is built – Instructions after happen as stack torn down
Fibonacci Sequence Famous recursively defined sequence
Naïve Implementation Direct code translation
Fibonacci Multiple recursive calls = combinatorial explosion:
Better Fibonacci Redefined Fibonacci function: – Parameters keep track of previous values – Call must include first two terms:
Reduced Load Parameters store old work…
Recursive Helpers This is ugly: Can make it a recursive helper: Call from main:
Moral Parameters are your main tool – Use them to "store" information – Use them to change where work happens
Recursive Design Recursive function design – What is base case? – What is one step? – What parameters do I need? Do I want/need extra ones to simplify problem?
Number of Digits How many digits does an integer have? – What is base case? – What is one step? – What parameters do I need?
Number of Digits How many digits does integer have? – What is base case? Anything < 10 is 1 digit – What is one step? – What parameters do I need?
Number of Digits How many digits does integer have? – What is base case? Anything < 10 is 1 digit – What is one step? Digits(n) = 1 + Digits(n/10) – What parameters do I need?
Number of Digits How many digits does integer have? – What is base case? Anything < 10 is 1 digit – What is one step? Digits(n) = 1 + Digits(n/10) – What parameters do I need? n
Number Of Digits Code: – Assumes number >= 0
Recursion With Array Want to total an array using recursion: – What is base case? Size 0 will equal 0 – What is one step? Total(size n) = nth element + Total(size n-1) – What parameters do I need? Array, size
Recursion With Array Want to total an array using recursion: Work backwards through array, pretending it gets smaller
Palindrome Is a string a Palindrome? (e.g. "radar") – What is base case? – What is one step? – What parameters do I need?
Palindrome Is a string a Palindrome? (e.g. "radar") – What is base case? 0 or 1 letters is a palindrome : "d" If first letter != last, is NOT a palindrome : "ben" – What is one step? – What parameters do I need?
Palindrome Is a string a Palindrome? (e.g. "radar") – What is base case? 0 or 1 letters is a palindrome : "d" If first letter != last, is NOT a palindrome : "ben" – What is one step? "madamImadam" Test all but first and last – What parameters do I need?
Palindrome Is a string a Palindrome? (e.g. "radar") – What is base case? 0 or 1 letters is a palindrome : "d" If first letter != last, is NOT a palindrome : "ben" – What is one step? "madamImadam" Test all but first and last – What parameters do I need? Current string
Palindrome Is a string a Palindrome? (e.g. "radar")
Palindrome Is a string a Palindrome? (e.g. "radar") – What is base case? 0 or 1 letters is a palindrome : "d" If first letter != last, is NOT a palindrome : "ben" – What is one step? Test all but first and last – What parameters do I need? Current string Do I want/need extra ones to simplify problem? Indexes to point to "start" and "end"
Palindrome Extra parameters avoid making new strings: Use as helper function:
Binary Search – Pick middle of remaining search space – Too high? Eliminate middle and above – Too low? Eliminate middle and below
Binary Search Binary search – What is base case? – What is one step? – What parameters do I need?
Binary Search Binary search – What is base case? – What is one step? – What parameters do I need? List, value looking for, lowest possible location, highest possible location
Binary Search Binary search – What is base case? If lowest possible location > highest possible location, it can't be there – What is one step? – What parameters do I need? List, value looking for, lowest possible location, highest possible location
Binary Search Binary search – What is base case? If lowest possible location > highest possible location, it can't be there – What is one step? Find middle If key == middle, return middle location If key < middle, highest is now middle -1 If key > middle, lowest is now middle + 1 – What parameters do I need? …
Binary Search Binary search – recursive helper Non-recursive starter: