Download presentation
Presentation is loading. Please wait.
Published byMelina Logan Modified over 9 years ago
1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington More Recursion COMP 102 #30 Tri 1, 2011
2
© Peter Andreae COMP 102 30:2 Outline Single recursion Multiple Recursion Pouring Paint Recursion v. Iteration Announcements: Engineering Students: Meeting, Work Experience Provisional Accreditation Other information
3
© Peter Andreae COMP 102 30: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
4
© Peter Andreae COMP 102 30: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
5
© Peter Andreae COMP 102 30: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
6
© Peter Andreae COMP 102 30: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 0123456789
7
© Peter Andreae COMP 102 30: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 0123456789
8
© Peter Andreae COMP 102 30:8 Multiple Recursion Draw a recursive arch-wall: Consists of an arch with two half size arch-walls on top of it.
9
© Peter Andreae COMP 102 30: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 }
10
© Peter Andreae COMP 102 30: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
11
© Peter Andreae COMP 102 30: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 …
12
© Peter Andreae COMP 102 30: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); }
13
© Peter Andreae COMP 102 30:13 Recursion vs Iteration Which is better?
14
© Peter Andreae COMP 102 30:14 Spreading Recursion. Spread(…)
15
© Peter Andreae COMP 102 30: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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.