From Barb Ericson Georgia Institute of Technology June 2008

Slides:



Advertisements
Similar presentations
First of all – lets look at the windows you are going to use. At the top you have a toolbar, with all your various tools you can use when customising your.
Advertisements

Chapter 16 Recursion: Another Control Mechanism. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. a.
Recursion1 Barb Ericson Georgia Institute of Technology March 2010.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 11 Recursion. © 2004 Pearson Addison-Wesley. All rights reserved11-2 Recursion Recursion is a fundamental programming technique that can provide.
RECURSION Recursion Towers of Hanoi To Recurse or to Loop? Fractals Do something!
Log on and Download from website:
Recursive Methods Noter ch.2. QUIZ What is the result of the method call foo(4) ? 1.Prints 4 2.Prints 1 3.Prints Prints Compiler error:
Recursion. What is recursion? Solving a problem in terms of itself or repeating objects in a “self-similar” way A recursive function is a function that.
B.A. (Mahayana Studies) Introduction to Computer Science November March Logo (Part 2) More complex procedures using parameters,
Main Points: - Python Turtle - Fractals
Georgia Institute of Technology Barb Ericson Georgia Institute of Technology May 2006 Teaching Java using Turtles part 2.
Agent P, I have been hearing some rumours about a Python Turtle.
Recursion Recursion Chapter 12. Outline n What is recursion n Recursive algorithms with simple variables n Recursion and the run-time stack n Recursion.
The Internet October 30, The Internet URL’s Search Engines Boolean Operators Internet Searches Scavenger Hunt.
Georgia Institute of Technology Movies part 2 Barb Ericson Georgia Institute of Technology April 2006.
Creating Art With Simple Shapes By carefully implementing simple shapes to an HTML canvas we can create useful graphics. With enough time, lines, ellipses.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Georgia Institute of Technology Pictures and Alice Barb Ericson Georgia Institute of Technology September 2006.
Trees CS 105. L9: Trees Slide 2 Definition The Tree Data Structure stores objects (nodes) hierarchically nodes have parent-child relationships operations.
Subtraction Strategies. draw a picture = Step 1, draw 85. Step 2, mark off 12. Step 3, what is left? = 73.
Data Structures TREES.
Georgia Institute of Technology Simulations Barb Ericson Jan 2005.
ET710 HTML Paths: Dot Dot Slash Notation. Directory (folder) Hierarchy. We can think of a computer’s file structure as a tree with branches. The trunk.
Georgia Institute of Technology Creating Classes part 4 Barb Ericson Georgia Institute of Technology May 2006.
CreatingClasses-SlideShow-part41 Creating Classes part 4 Barb Ericson Georgia Institute of Technology Dec 2009.
Making a Sprite Dance Barb Ericson Georgia Tech June 2011.
Drawing with the Pen Barb Ericson Georgia Tech June 2011.
 Make sure you are subscribed to announcements on Moodle.  Activity 5 will be due before the beginning of lab next week.  Check Moodle for complete.
BINARY TREES Objectives Define trees as data structures Define the terms associated with trees Discuss tree traversal algorithms Discuss a binary.
Digital Art in Scratch part 2 Barb Ericson Georgia Tech Nov 2010.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Trees CSIT 402 Data Structures II 1. 2 Why Do We Need Trees? Lists, Stacks, and Queues are linear relationships Information often contains hierarchical.
Functions. functions: a collection of lines of code with a name that one can call. Functions can have inputs and outputs.
Trees A non-linear implementation for collection classes.
Data Structures.
Data Structures and Design in Java © Rick Mercer
Week 3 DO NOW QUESTIONS.
LOGO BY Kaotip 9S.
Main Points: - Python Turtle - Fractals
Appendix A Barb Ericson Georgia Institute of Technology May 2006
Recursion- Fibonacci mpack.
Recursion CSC 202.
Stage 11: Artist: Nested Loops
B+ Tree.
Barb Ericson Georgia Institute of Technology May 2006
Creating and Modifying Text part 2
Trees.
Tonga Institute of Higher Education
i206: Lecture 13: Recursion, continued Trees
Agent P, I have been hearing some rumours about a Python Turtle.
TREES General trees Binary trees Binary search trees AVL trees
Representing Structure and Behavior with Trees
Trees Part 2!!! By JJ Shepherd.
A colorful way to find the factors of given numbers
Main Points: - Python Turtle - Fractals
Recursion Think ESCHER.
Chapter 11 Recursion.
Trees and Colored Edge Detection
11 Recursion Software Solutions Lewis & Loftus java 5TH EDITION
Recursion Taken from notes by Dr. Neil Moore
Third 100 Words Fry Instant Word List.
Turn to Page S.89 Challenging Question
Algorithms, Part 3 of 3 Topics Top down-design Structure charts
Java Software Solutions Foundations of Program Design Sixth Edition
Teaching Java using Turtles part 2
Algorithms, Part 3 of 3 Topics Top down-design Reading
CMPT 120 Lecture 18 – Unit 3 – Graphics and Animation
Bell Work Title: Turtle Intro Date:
Presentation transcript:

From Barb Ericson Georgia Institute of Technology June 2008 Recursion The only way to understand recursion is to understand recursion From Barb Ericson Georgia Institute of Technology June 2008 13-Recursion

An example of recursion Have you ever read the Cat in the Hat Comes Back? In it a cat visits two children and messes up the house. When the time come to fix up the house. The cat takes off his hat to show another cat (A) and asks that cat (A) to clean up That cat takes off his hat to show another cat (B) and asks this cat (B) to clean up This goes on and on until the final cat Z cleans up the mess using Voom and all the cats go back in the hats 13-Recursion

Definition of Recursion A method that calls itself A recursive procedure should have some way to stop There is an ending (terminal) condition It is often used to break a complex problem into a simpler problem of the same type Which should eventually lead to the ending condition 13-Recursion

Loop vs. recursion While (not at door) Method step Take a step Method step if not at door, step 13-Recursion

Recursive Definitions Definition of Recursion See recursion This is the kind of recursion that never stops Definition of an ancestor A person's parents and their ancestors (see the definition of ancestor) You can list your ancestors by listing your parents and then calling list ancestor on them and so on 13-Recursion

Drawing a Tree A tree has a recursive structure. The trunk of a tree has other branches off of it which in turn have branches off of them, and so on. If we treat the trunk of the tree like a branch. We can draw a tree by drawing the trunk Then create turn one pen left and one pen right (30 degrees) and each draw a smaller tree Call the same method with a smaller branch size Stop when the length of the branch gets too small 13-Recursion

Drawing a Tree with Turtles After 1 branch is drawn (the trunk) After 4 branches are drawn This is like an ancestor tree. Each person has 2 parents and they each have two parents and they each have two parents and so on. Lots of branches 13-Recursion

Recursive Triangles If you have a triangle you can break it into 4 new triangles Create a point at the mid point of each side and connect them with line segments Now you can break up the resulting 4 triangles 13-Recursion

Create a Triangle Class Each triangle can be defined by 3 points You can use the Point class in java. Write a method to subdivide a triangle Create 3 new points and 4 new triangles You can call the method on the new triangles p1 p1 p4 p5 p3 p2 p2 p3 p6 13-Recursion

Applications of Recursion In 3D graphics we break up a sphere into triangles to display If the sphere is small on the display you don't need many triangles If the sphere is bigger on the display you need more triangles Use recursion to get the right amount of triangles based on the displayed size 13-Recursion

Processing Files A directory can have other directories So you can use a recursive method to list all the files in each directory Or to create an HTML page with thumbnails of all of the pictures in a directory Or to follow links on a page (crawl web pages like a spider) 13-Recursion

Summary Recursion features a method that calls itself With some way to end the recursion It is used on data structures that are recursive Like trees It can also be used for methods that have a recursive definition Like fibonacci numbers http://www.mcs.surrey.ac.uk/ Personal/R.Knott/Fibonacci/fibnat.html Fibonacci of 0 is 0 and of 1 is 1 and of n is Fib(n-1) + Fib(n-2) 13-Recursion