Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #30.

Slides:



Advertisements
Similar presentations
MiniDraw Testing COMP 102 # T1
Advertisements

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
Recursion. Idea: Some problems can be broken down into smaller versions of the same problem Example: n! 1*2*3*…*(n-1)*n n*factorial of (n-1)
The Power of Calling a Method from Itself Svetlin Nakov Telerik Corporation
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Images and 2D Graphics COMP # 16.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
COMP 103 Introduction to Trees.
Complex objects How might you design a class called NestedRects of graphical objects that look like this? Requirements for the constructor: –Like many.
TOPIC 7 MODIFYING PIXELS IN A MATRIX NESTED FOR LOOPS 1 Notes adapted from Introduction to Computing and Programming with Java: A Multimedia Approach by.
2014-T2 Lecture 24 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
NestedLoops-part11 Nested Loops – part 1 Barb Ericson Georgia Institute of Technology Nov 2009.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods with Parameters COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Recursion. Recursive Methods HAVE: 1.A base case or termination condition that causes the method to end 2.A non-base case whose actions move the algorithm.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #32.
CSC1401 Viewing a picture as a 2D image - 1. Review from the last week We used a getPixels() to get all of the pixels of a picture But this has been somewhat.
2013-T2 Lecture 22 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and Thomas.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Searching Dr. Jose Annunziato. Linear Search Linear search iterates over an array sequentially searching for a matching element int linearSearch(int haystack[],
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Call a Method,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArraySet and Binary Search.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Linked Structures.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Conditionals.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Methods COMP.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Exercise, printf,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Classes, Objects, Fields,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW AI for games: min-max search COMP #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Fields, Constructors.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae, VUW Images and 2D Graphics COMP # 17.
COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.
2015-T2 Lecture 17 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, John Lewis,
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Loops and Input COMP 102 #
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Methods Calling Methods Return.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington 2D arrays COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with Classes and.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More 2D arrays COMP 102 #27.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
2014-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Designing with While loops.
Recursion COMP x1 Sedgewick Chapter 5. Recursive Functions problems can sometimes be expressed in terms of a simpler instance of the same problem.
Int fact (int n) { If (n == 0) return 1; else return n * fact (n – 1); } 5 void main () { Int Sum; : Sum = fact (5); : } Factorial Program Using Recursion.
Xiaoying Gao Computer Science Victoria University of Wellington Copyright: Xiaoying Gao, Peter Andreae, Victoria University of Wellington Methods with.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Dealing with Files COMP 102.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
School of Engineering and Computer Science Victoria University of Wellington Copyright: Peter Andreae & david streader, VUW Images and 2D Graphics COMP.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 102 # T1.
Recursion -- Introduction
COMP 103 RECURSION.
COMP 103 Linked Structures Marcus Frean 2014-T2 Lecture 17
RECURSION COMP 103 Thomas Kuehne 2016-T2 Lecture 16
Presentation transcript:

Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #30 Tri 1, 2011

© Peter Andreae COMP :2 Outline Single recursion Multiple Recursion Pouring Paint Recursion v. Iteration Announcements: Engineering Students: Meeting, Work Experience Provisional Accreditation Other information

© Peter Andreae COMP :3 Kinds of Recursion Single recursion vs multiple recursion Single: method only makes one recursive call Multiple: method makes more than one recursive call Tail recursion vs Nested recursion Tail: recursive call is the last step in a method ⇒ don’t have to “go back” and do more. Nested: recursive call is in the middle ⇒ action; recursive call; more actions Methods that just act vs methods that return a value Recursion on linear data vs recursion on recursive data

© Peter Andreae COMP :4 Recursion with arrays Find the longest word in an array: break into first and rest find longest in rest compare first and longest dogsealemudeerseagullbirdfoxzoodflybatmouseai

© Peter Andreae COMP :5 Recursion with arrays private String[ ] words …. // array of words // return longest word in array between k and end public String longestRec (int k){ if (k >= this.words.length – 1) return this.words[k]; else { String wd = this.longestRec(k+1); if (this.words[k].length() > wd.length() ) return this.words[k]; else return wd; } // wrapper: Return longest word in whole array public String longest (){ return this.longestRec(0); } dogsealemudeer horse hengnuaidflyfoxfleazoo

© Peter Andreae COMP :6 Multiple Recursion with arrays Could break into two halves: // wrapper: Return longest word in whole array public String longest (){ return this.longestRec (0, this.words.length); } // return longest word in array from fr up to (but not including) to public String longestRec (int fr, int to){ if (fr >= to-1) return words[fr]; else { String wd1 = this.longestRec(fr, (fr+to)/2); // longest in left half String wd2 = this.longestRec((fr+to)/2, to);// longest in right half if ( wd1.length() >= wd2.length() ) return wd1; else return wd2; } } dogsealemudeerbeehorsegnubugaifly

© Peter Andreae COMP :7 Recursion with arrays public String longest (int fr, int to){ if (fr >= to-1) return words[fr]; String wd1 = this.longestRec(fr, (fr+to)/2); // longest in left half String wd2 = this.longestRec((fr+to)/2, to);// longest in right half if ( wd1.length() >= wd2.length() ) return wd1; else return wd2; } } L (0,10) dogsealemudeerbeehorsegnubugaifly

© Peter Andreae COMP :8 Multiple Recursion Draw a recursive arch-wall: Consists of an arch with two half size arch-walls on top of it.

© Peter Andreae COMP :9 Multiple Recursion: ArchWall to draw an ArchWall of given base size (wd,ht): draw a base arch of size (wd,ht) if wd is not too small draw a half size archwall on the left draw a half size archwall on the right public void archWall (int x, int y, int wd, int ht){ this.drawArch(x, y, wd, ht); if ( wd > 20 ) { int w = wd/2; // width of next smaller arch int h = ht/2; // height of next smaller arch this.archWall(x, y-h, w, h); // left half this.archWall(x+w, y-h, w, h); // right half }

© Peter Andreae COMP :10 Tracing the execution: archWall(10, 300, 80, 40) drawArch aw(10, 220, 40, 20) aw(50,220,40,20) drawArch aw(10,180,20,10) aw(30,180,20,10) drawArch aw(50,180,20,10) aw(70,180,20,10) drawArch

© Peter Andreae COMP :11 Multiple Recursion “Pouring” Paint in a painting program: colour this pixel spread to each of the neighbour pixels colour the pixel spread to its neighbours colour the pixel spread to its neighbours …

© Peter Andreae COMP :12 Spreading Paint private int rows = 25; private int cols = 35; private Color[ ][ ] grid = new Color[40][50]; // the grid of colours, /** Spread new colour to all adjacent cells with oldColour */ public void spread(int row, int col, Color newColour, Color oldColour){ if (row = this.rows || col = this.cols) return; if ( ! this.grid[row][col].equals(oldColour) ) return; this.setPixel(row, col, newColour); spread(row-1,col, newColour, oldColor); spread(row+1,col, newColour, oldColor); spread(row,col-1, newColour, oldColor); spread(row,col+1, newColour, oldColor); }

© Peter Andreae COMP :13 Recursion vs Iteration Which is better?

© Peter Andreae COMP :14 Spreading Recursion. Spread(…)

© Peter Andreae COMP :15 Recursive structures. Jane Jeremy John Julia JamesJada Jasmine Jules Jacob Jenny JesseJaredJake Julie Jordan JennaJuan Justin JackyJoseph How can you print a list of all the people under a manager?