Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #32."— Presentation transcript:

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

2 © Peter Andreae COMP 102 32:2 Outline Single recursion Multiple Recursion Pouring Paint Recursion v. Iteration Announcements:

3 © Peter Andreae COMP 102 32:3 Planning your degree: COMP COMP 102 COMP 103, MATH 161, SWEN 102, STAT 193/ MATH177 COMP 261SWEN 221, 222NWEN 241 SWEN 223 NWEN 242 SWEN 224NWEN 243 [MATH 261] GraphicsSE, (x2)Operating Systems AIDatabasesData Comms AlgorithmsHCINetwork Applications Prog LanguagesConcurrency Simulations [coding theory, complexity]

4 © Peter Andreae COMP 102 32:4 ImageProcessor: Zoom

5 © Peter Andreae COMP 102 32:5 zoomImage public void zoomImage(){ int rows = this.image.length; int cols = this.image[0].length; int[ ][ ] zoomed = new int[rows][cols]; for (int row = 0; row < rows ; row++){ int sourceRow = this.selectedRow + (row-selectedRow)*3/4; for (int col = 0; col<cols; col++){ int sourceCol = this.selectedCol + (col-selectedCol)*3/4; zoomed[row][col] = this.image[sourceRow][sourceCol]; } this.image = zoomed; }

6 © Peter Andreae COMP 102 32:6 Kinds of Recursion 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 Single recursion vs multiple recursion Single: method only makes one recursive call Multiple: method makes more than one recursive call Methods that just act vs methods that return a value Recursion on linear data vs recursion on recursive data

7 © Peter Andreae COMP 102 32:7 Recursion with arrays Find the longest word in an array: break into last and rest find longest in rest compare last and longest dogsealemudeerseagullbirdfoxzoodflybatmouseai

8 © Peter Andreae COMP 102 32:8 Recursion with arrays private String[ ] words …. // array of word // wrapper: Return longest word in whole array public String longest (){ return this.longestRec(words.length); } // return longest word in array from 0 up to (but not including) k public String longestRec (int k){ if (k == 1) return this.words[0]; else { String wd = this.longestRec(k-1); if (this.words[k-1].length() > wd.length() ) return this.words[k-1]; else return wd; } dogsealemudeer horse hengnuaidflyfoxfleazoo

9 © Peter Andreae COMP 102 32:9 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 0123456789

10 © Peter Andreae COMP 102 32:10 Recursion with arrays public String longestRec (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 0123456789

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

12 © Peter Andreae COMP 102 32:12 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 }

13 © Peter Andreae COMP 102 32:13 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

14 © Peter Andreae COMP 102 32:14 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 …

15 © Peter Andreae COMP 102 32:15 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); }

16 © Peter Andreae COMP 102 32:16 Recursion vs Iteration Which is better?

17 © Peter Andreae COMP 102 32:17 Recursive structures. Jane JeremyJulieJustin John Jules Jenny Jesse JordanJacky JennaJuanJacobJulia JamesJada JaredJake Joseph Jasmine How can you print a list of all the people under a manager?


Download ppt "Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #32."

Similar presentations


Ads by Google