Recursion Tiziana Ligorio � 1
Today’s Plan Announcements Recursion � 2
What do these images have in common � 3
They contain a SMALLER copy of THEMSELVES � 4
Print String Backwards “Hello” � 5
Print String Backwards “Hello” Procedure: If there are characters to print Print the last character and reverse the rest Recursive Call Notice it’s the last thing it does � 6
Print String Backwards Hello o Active functions Hello Program Stack
Print String Backwards Hello o Hell Active functions Hell Hello Program Stack
Print String Backwards Hello o Hell Active functions o l Hell Hello Program Stack � 9 � 9
Print String Backwards Hello o Hell Active functions o l Hel Hell Hel Hello Program Stack � 10 � 10
Print String Backwards Hello o Hell Active functions o l Hel Hell Hel Hello o l l Program Stack � 11 � 11
Print String Backwards Hello o Hell Active functions He o l Hel Hell Hel Hello o l l Program Stack He � 12 � 12
Print String Backwards Hello o Hell Active functions He o l Hel Hell Hel Hello o l l Program Stack He o l l e � 13 � 13
Print String Backwards Hello o Hell H Active functions o l He Hel Hell Hel Hello o l l Program Stack He o l l e H � 14 � 14
Print String Backwards Hello o Hell H Active functions o l He Hel Hell Hel Hello o l l Program Stack He o l l e H o l l e H � 15 � 15
Print String Backwards Hello o Hell H Active functions o l He Hel Hell Hel Hello o l l Program Stack He o l l e H o l l e H BASE CASE � 16 � 16
Print String Backwards Hello o Hell H Active functions o l He Hel Hell Hel Hello o l l Program Stack He o l l e H o l l e H
Print String Backwards Hello o Hell Active functions o l He Hel Hell Hel Hello o l l Program Stack He o l l e H o l l e H
Print String Backwards Hello o Hell Active functions o l He Hel Hell Hel Hello o l l Program Stack He o l l e H o l l e H
Print String Backwards Hello o Hell Active functions o l Hel Hell Hel Hello o l l Program Stack He o l l e H o l l e H
Print String Backwards Hello o Hell Active functions o l Hel Hell Hel Hello o l l Program Stack He o l l e H o l l e H
Print String Backwards Hello o Hell Active functions o l Hell Hel Hello o l l Program Stack He o l l e H o l l e H
Print String Backwards Hello o Hell Active functions o l Hell Hel Hello o l l Program Stack He o l l e H o l l e H
Print String Backwards Hello o Hell Active functions o l Hel Hello o l l Program Stack He o l l e H o l l e H
Print String Backwards Hello o Hell Active functions o l Hel Hello o l l Program Stack He o l l e H o l l e H
Print String Backwards Hello o Hell o l Hel o l l Program Stack He o l l e H o l l e H
Lecture Activity If I hand you a printed dictionary (an actual book) and ask you to find the word “Kalimba”, what do you do? Write down precise steps (a procedure) as if someone who doesn’t know what a dictionary is must follow your instructions. � 27
Look in ? � 28
LOOK FOR WORD “Kalimba” IN DICTIONARY - Open dictionary at random page _ If “Kalimba” is on page FOUND!!! - Else if “Kalimba” is lexicographically < first word on page LOOK FOR WORD “Kalimba” IN LOWER HALF Recursive Call - Else if “Kalimba” is lexicographically > last word on page LOOK FOR WORD “Kalimba” IN UPPER HALF Recursive Call � 29
How is this different from recursive solution to print backwards? � 30
How is this different from recursive solution to print backwards? - Two recursive calls - Execute either one or the other - Cuts problem in 1/2 � 31
The images in the next slides were adapted from Keith Schwarz at Stanford University � 32
� 33
� 34
� 35
� 36
What di$erentiates the smaller What di$erentiates the smaller tree from the bigger one? tree from the bigger one? 1. It’s at a di$erent position . 1. It’s at a di$erent position . 2. It’s at a di$erent orientation . 2. It’s at a di$erent orientation . 3. It has a di$erent size . 3. It has a di$erent size . 4. It has a di$erent order . 4. It has a di$erent order . � 37
What di$erentiates the smaller What di$erentiates the smaller tree from the bigger one? tree from the bigger one? 1. It’s at a di$erent position . 1. It’s at a di$erent position . 2. It’s at a di$erent orientation . 2. It’s at a di$erent orientation . 3. It has a di$erent size . 3. It has a di$erent size . 4. It has a di$erent order . 4. It has a di$erent order . � 38
What di$erentiates the smaller What di$erentiates the smaller tree from the bigger one? tree from the bigger one? 1. It’s at a di$erent position . 1. It’s at a di$erent position . 2. It’s at a di$erent size . 2. It’s at a di$erent size . 3. It has a di$erent size . 3. It has a di$erent size . 4. It has a di$erent order . 4. It has a di$erent order . � 39
What di$erentiates the smaller What di$erentiates the smaller tree from the bigger one? tree from the bigger one? 1. It’s at a di$erent position . 1. It’s at a di$erent position . 2. It’s at a di$erent size . 2. It’s at a di$erent size . 3. It has a di$erent orientation . 3. It has a di$erent orientation . 4. It has a di$erent order . 4. It has a di$erent order . � 40
What di$erentiates the smaller What di$erentiates the smaller tree from the bigger one? tree from the bigger one? 1. It’s at a di$erent position . 1. It’s at a di$erent position . 2. It’s at a di$erent size . 2. It’s at a di$erent size . 3. It has a di$erent orientation . 3. It has a di$erent orientation . 4. It has a di$erent order . 4. It has a di$erent order . � 41
What di$erentiates the smaller What di$erentiates the smaller tree from the bigger one? tree from the bigger one? Recursive fractals are often Recursive fractals are often 1. It’s at a di$erent position . 1. It’s at a di$erent position . described in terms of some described in terms of some 2. It’s at a di$erent size . 2. It’s at a di$erent size . parameter called the order of parameter called the order of 3. It has a di$erent orientation . 3. It has a di$erent orientation . the fractal. the fractal. 4. It has a di$erent order . 4. It has a di$erent order . � 42
An order-0 tree. What di$erentiates the smaller What di$erentiates the smaller tree from the bigger one? tree from the bigger one? Recursive fractals are often Recursive fractals are often 1. It’s at a di$erent position . 1. It’s at a di$erent position . described in terms of some described in terms of some 2. It’s at a di$erent size . 2. It’s at a di$erent size . parameter called the order of parameter called the order of 3. It has a di$erent orientation . 3. It has a di$erent orientation . the fractal. the fractal. 4. It has a di$erent order . 4. It has a di$erent order . � 43
An order-1 tree. What di$erentiates the smaller What di$erentiates the smaller tree from the bigger one? tree from the bigger one? Recursive fractals are often Recursive fractals are often 1. It’s at a di$erent position . 1. It’s at a di$erent position . described in terms of some described in terms of some 2. It’s at a di$erent size . 2. It’s at a di$erent size . parameter called the order of parameter called the order of 3. It has a di$erent orientation . 3. It has a di$erent orientation . the fractal. the fractal. 4. It has a di$erent order . 4. It has a di$erent order . � 44
An order-2 tree. What di$erentiates the smaller What di$erentiates the smaller tree from the bigger one? tree from the bigger one? Recursive fractals are often Recursive fractals are often 1. It’s at a di$erent position . 1. It’s at a di$erent position . described in terms of some described in terms of some 2. It’s at a di$erent size . 2. It’s at a di$erent size . parameter called the order of parameter called the order of 3. It has a di$erent orientation . 3. It has a di$erent orientation . the fractal. the fractal. 4. It has a di$erent order . 4. It has a di$erent order . � 45
An order-3 tree. What di$erentiates the smaller What di$erentiates the smaller tree from the bigger one? tree from the bigger one? Recursive fractals are often Recursive fractals are often 1. It’s at a di$erent position . 1. It’s at a di$erent position . described in terms of some described in terms of some 2. It’s at a di$erent size . 2. It’s at a di$erent size . parameter called the order of parameter called the order of 3. It has a di$erent orientation . 3. It has a di$erent orientation . the fractal. the fractal. 4. It has a di$erent order . 4. It has a di$erent order . � 46
Recommend
More recommend