Download presentation
Presentation is loading. Please wait.
Published bySimon Glenn Modified over 9 years ago
1
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Recursion COMP 103 #14 2012
2
© Peter Andreae COMP 103 14:2 Menu Binary Search SortedArraySet: Implementing Set with Binary Search Recursion Notes:
3
© Peter Andreae COMP 103 14:3 Recursion Methods that call themselves No different from calling another method! Gives lots of power for designing algorithms Critical for dealing with some data structures public void drawBoxes(int x, int y, int wd, int ht){ if ( ht>10 ) { UI.fillRect(x-wd/2, y-ht, wd, ht); drawBoxes(x, y, wd-5, ht-5); } Base case: When does it not call itself? Recursive call Each call has its own set of arguments and local variables.
4
© Peter Andreae COMP 103 14:4 Recursion Doing stuff before or after the recursive call: public void drawBoxes(int x, int y, int wd, int ht){ if ( ht>10 ) { drawBoxes(x, y, wd-5, ht-5); UI.fillRect(x-wd/2, y-ht, wd, ht); } } public void drawBoxes(int x, int y, int wd, int ht){ if ( ht>10 ) { UI.fillRect(x-wd/2, y-ht, wd, ht); drawBoxes(x, y, wd-5, ht-5); UI.drawLine(x-wd/2, y-ht, x+wd/2, y); } } done "on the way back"
5
© Peter Andreae COMP 103 14:5 More recursion Draw fancy square: draw outline square draw top-left smaller fancy square draw top-right smaller fancy square draw bottom-left smaller fancy square draw bottom-right smaller fancy square public void drawFSquareint x, int y, int sz,){ if ( sz>=20 ) { UI.drawRect(x, y, sz, sz); int offset = size/2+2.5; int smallSz = (sz- 15/2); drawFSquare(x+5, y+5, smallSz); drawFSquare(x+offset, y+5, smallSz); drawFSquare(x+5, y+offset, smallSz); drawFSquare(x+offset, y+offset, smallSz); }
6
© Peter Andreae COMP 103 14:6 Recursion with an array public double findMax(double[] data, int start, int end){ if ( end-start == 1 ) { return data[start]; } else { int mid = (start+end)/2; double[] lmax = findMax(data, start, mid); double[] rmax = findMax(data, mid, end); return Math.max(lmax, rmax); } 012345678929101112133014151617181920212223242526272831 1 past the last item: items at :start … end-1
7
© Peter Andreae COMP 103 14:7 Recursive binary search. private boolean contains (Object item){ return BinarySearch((Comparable ) item, 0, count-1); } private boolean contains (Comparable item, int low, int high){ if (low > high) { return false; } int mid = (low + high) / 2; int c = item.compareTo(data[mid]); if (c < 0) { return contains (item, low, mid-1); } else if (c > 0) { return contains (item, mid+1, high); } else { return true; } } Same as the other binary search, Easier to design.
8
© Peter Andreae COMP 103 14:8 Recursive binary search with trail. List path = new ArrayList (); BinarySearch( item, 0, count-1, path); UI.println(path); private boolean search(String item, int low, int high, List path ){ if (low > high) { return false; } int mid = (low + high) / 2; path.add(data[mid]); int c = item.compareTo(data[mid]); if (c < 0) { return contains (item, low, mid-1, path); } else if (c > 0) { return contains (item, mid+1, high, path); } else { return true; } } Pass the path along, adding items to it as you go.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.