recursion
play

Recursion Tiziana Ligorio 1 Todays Plan Announcements Recursion - PowerPoint PPT Presentation

Recursion Tiziana Ligorio 1 Todays 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


  1. Recursion Tiziana Ligorio � 1

  2. Today’s Plan Announcements Recursion � 2

  3. What do these images have in common � 3

  4. They contain a SMALLER copy of THEMSELVES � 4

  5. Print String Backwards “Hello” � 5

  6. 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

  7. Print String Backwards Hello 
 o Active functions Hello Program Stack

  8. Print String Backwards Hello 
 o 
 Hell 
 Active functions Hell Hello Program Stack

  9. 
 
 Print String Backwards Hello 
 o 
 Hell 
 Active functions o l Hell Hello Program Stack � 9 � 9

  10. 
 Print String Backwards Hello 
 o 
 Hell 
 Active functions o l Hel Hell Hel 
 Hello Program Stack � 10 � 10

  11. 
 Print String Backwards Hello 
 o 
 Hell 
 Active functions o l Hel Hell Hel 
 Hello o l l Program Stack � 11 � 11

  12. 
 Print String Backwards Hello 
 o 
 Hell 
 Active functions He o l Hel Hell Hel 
 Hello o l l Program Stack He � 12 � 12

  13. 
 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

  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 
 � 14 � 14

  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 � 15 � 15

  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 BASE CASE � 16 � 16

  17. 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

  18. 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

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. Look in ? � 28

  29. 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

  30. How is this different from recursive solution to print backwards? � 30

  31. 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

  32. The images in the next slides were adapted from Keith Schwarz at Stanford University � 32

  33. � 33

  34. � 34

  35. � 35

  36. � 36

  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 . � 37

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

  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 size . 3. It has a di$erent size . 4. It has a di$erent order . 4. It has a di$erent order . � 39

  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 . � 40

  41. 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

  42. 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

  43. 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

  44. 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

  45. 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

  46. 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