Download presentation
Presentation is loading. Please wait.
Published byChester Charles Modified over 6 years ago
1
RECURSION COMP 103 Thomas Kuehne 2016-T2 Lecture 16
Marcus Frean Thomas Kuehne School of Engineering and Computer Science, Victoria University of Wellington 2016-T2 Lecture 16
2
RECAP-TODAY RECAP TODAY Recursion on recursive data structures
2 RECAP Recursion on recursive data structures TODAY Recursion with numbers to understand recursion, you must first understand recursion …
3
Recursion Methods that call themselves
3 Methods that call themselves not only works for recursive data structurs no different from calling another method public void drawBoxes(int c, int b , int width, int height) { if (height < 10) return; UI.fillRect(c - width/2, b - height, width, height); drawBoxes(c, b, width - 5, height - 5); } Base case When does it not call itself Recursive call Each call has its own set of arguments and local variables.
4
Another example 4 /** Draws a stream of bubbles from (x,y) to the top of the screen */ public void drawBubbles(int x, int y) { if ( y>0 ) { paint1Bubble(x, y); drawBubbles(x, y-15); } y=0 y=70 x=30 drawBubbles (30, 40); paint1Bubble(30,40); drawBubbles (30, 55); paint1Bubble(30,55); drawBubbles (30, 70); paint1Bubble(30,70);
5
Tracing the calls drawBubbles(30, 70) drawBubbles(30, 55)
public void drawBubbles(int x, int y) { if ( y>0 ) { paint1Bubble(x, y); drawBubbles(x, y-15); } return drawBubbles(30, 70) paint1Bubble(30, 70) drawBubbles(30, 55) return y=0 paint1Bubble(30, 55) drawBubbles(30, 40) return paint1Bubble(30, 40) drawBubbles(30, 25) return paint1Bubble(30, 25) drawBubbles(30, 10) return paint1Bubble(30, 10) drawBubbles(30, -5) return
6
Recursion with wrapper method
6 /** Draws a stream of bubbles from (x,y) to the top of the screen the bubbles should get larger as they go up. */ public void drawBubblesRecurse(int x, int y, int size) { if ( y>0 ) { UI.fillOval(x, y, size, size); drawBubblesRecurse(x, y-size-6, size+2); } public void drawBubbles(int x, int y) { UI.setColor(Color.blue); drawBubblesRecurse(x, y, 10); Condition Do First Do the Rest Arguments for the Rest Initial size
7
Tail recursion vs nested recursion.
7 Tail recursion: recursive call is the last step in the method can be easily replaced with iteration Nested recursion: actions follow the recursive call recursion has to “unroll” in reverse order Example: Print an “onion” : ( ( ( ( ( ( ( ) ) ) ) ) ) ) public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } open do the inside layers close
8
Nested Recursion at work
8 onion(4) ⇒ public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); ( ( ( ( ) ) ) ) public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 4 . public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 3 . ✔ public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 2 . ✔ ✔ public void onion (int layers) { UI.print( “(“ ); if (layers > 1) onion(layers-1); UI.print( “)“ ); } 1 . ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔ ✔
9
Alternative Visualisation
9 onion(4) ✔ ✔ print( “(” ) print( “)” ) onion(3) ✔ ✔ ✔ print( “(” ) print( “)” ) ✔ onion(2) ✔ print( “)” ) ✔ print( “(” ) onion(1) ✔ ✔ ✔ print( “(” ) print( “)” ) ✔ Drawing diagrams can help with understanding recursive methods
10
Recursive Arch-Wall Draw a recursive arch-wall:
10 Draw a recursive arch-wall: Consists of an arch with two half size arch-walls on top of it.
11
Recursive Arch-Wall Algorithm
11 To draw an ArchWall of given base size (width, height): draw a base arch of size (width, height) if width 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 width, int height) { drawArch(x, y, width, height); if ( width > 20 ) { int w = width / 2; // width of next smaller arch int h = height / 2; // height of next smaller arch archWall(x, y-h, w, h); // left half archWall(x+w, y-h, w, h); // right half }
12
Tracing the execution aw(10, 300, 80, 40) aw(50,280,40,20)
12 aw(10, 300, 80, 40) aw(50,280,40,20) drawArch aw(10, 280, 40, 20) drawArch aw(10,270,20,10) aw(30,270,20,10) drawArch aw(50,270,20,10) aw(70,270,20,10) drawArch drawArch drawArch drawArch
13
Recursively finding the maximum
14 1 2 3 4 5 6 7 8 9 29 10 11 12 13 30 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 31 public double findMax(double[] data, int start , int end) { if ( end - start < 1 ) return data[start]; else { int mid = ( start + end) / 2; double leftMax = findMax(data, start, mid); double rightMax = findMax(data, mid+1, end); return Math.max(leftMax, rightMax); } Mid-Split is silly here. Might as well just step one by one. However, shows how you could find a maximum in a binary tree & would be quicker if you could run the methods in parallel
14
Recursive Binary Search
15 private int findIndexOf(Comparable<E> item, int low, int high) { if (low > high) return low; // nothing left to search in int mid = (low + high) / 2; // calculate best guess int c = item.compareTo(data[mid]); if (c == 0) return mid; // found the item! if (c < 0) return findIndexOf(item, low, mid-1); // must be in lower half else return findIndexOf(item, mid+1, high); // must be in upper half } Same as the iterative binary search, but easier to design.
15
Iterative Binary Search
16 private int findIndexOf(Comparable<E> item, int low, int high) { while (low <= high) { int mid = (low + high) / 2; // calculate best guess int c = item.compareTo(data[mid]); if (c == 0) return mid; // found the item! if (c > 0) low = mid + 1; // item should be in [mid+1..high] else high = mid - 1; // item should be in [low..mid-1] } return low; // return insertion position
16
Towers of Hanoi Move all disks from the first to the middle pole
17 Move all disks from the first to the middle pole only move one disk at a time only disks at the very top can be moved never put a larger disk on a smaller disk There are monks in Hanoi doing this with 64 disks when they are finished, the world will end
17
Towers of Hanoi 1 3 2 void hanoi(int n, int from, int to ) {
18 1 3 2 void hanoi(int n, int from, int to ) { if there is one disk only move (1) "from" => "to" else { determine third pole "other" hanoi (n-1) "from" => "other" (1) move (1) "from" => "to" (2) hanoi (n-1) "other" => "to" (3) }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.