Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.

Slides:



Advertisements
Similar presentations
Base Conversion COP Practice Problem  Print a String in reverse order:  For example, if we want to print “HELLO” backwards,  we first print:
Advertisements

Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Sorting.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
2.2 General Positional-Number-System Conversion
Recursion Chapter 11 Chapter 11.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Unit 191 Recursion General Algorithm for Recursion When to use and not use Recursion Recursion Removal Examples Comparison of the Iterative and Recursive.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Algorithm Efficiency and Sorting
1 Section 2.5 Integers and Algorithms. 2 Euclidean algorithm for finding gcd Where L is a larger number, and S is a smaller number, to find gcd(L,S):
Data Representation in Computers
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Number Systems and Arithmetic
 Binary Binary  Binary Number System Binary Number System  Binary to Decimal Binary to Decimal  Decimal to Binary Decimal to Binary  Octal and Hexadecimal.
Number systems Converting numbers between binary, octal, decimal, hexadecimal (the easy way)
Analysis of Algorithms Dilemma: you have two (or more) methods to solve problem, how to choose the BEST? One approach: implement each algorithm in C, test.
Chapter 16 Binary and Hexadecimal Numbers. §16.2 thru 16.3 – Addition and Subtraction of Binary Numbers Binary = Base 2 Addition and subtraction are similar.
Lecture for Week Spring.  Numbers can be represented in many ways. We are familiar with the decimal system since it is most widely used in everyday.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Computers Organization & Assembly Language
Numbering Systems CS208.
Recursion James Wright St Peter’s Secondary School 733 Parkhill Road West Peterborough,Ontario K9J-8M4
20-1 Computing Fundamentals with C++ Object-Oriented Programming and Design, 2nd Edition Rick Mercer Franklin, Beedle & Associates, 1999 ISBN
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 Recursion.
CMSC 2021 Recursion Recursive Definition – one that defines something in terms of itself Recursion – A technique that allows us to break down a problem.
1 “Not all recursive solutions are better than iterative solutions…” “… recursion, however, can provide elegantly simple solutions to problems of great.
Chapter 11- Recursion. Overview n What is recursion? n Basics of a recursive function. n Understanding recursive functions. n Other details of recursion.
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 19 : Recursion King Fahd University of Petroleum & Minerals College of Computer.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
1 CS100J 09 March, 2006 More on Loops Reading: Secs 7.1–7.4 A Billion. The next time you hear someone in government rather casually use a number that includes.
Chapter 13 Recursion. Learning Objectives Recursive void Functions – Tracing recursive calls – Infinite recursion, overflows Recursive Functions that.
1 Chapter 3. Recursion Lecture 6. In functions and data structures.
Number systems, Operations, and Codes
Positional Notation 642 in base 10 positional notation is:
Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Chapter 17 Recursion Data Structures with Java © Rick Mercer Data Structures with Java © Rick Mercer.
Data Structure and Algorithms. Algorithms: efficiency and complexity Recursion Reading Algorithms.
Using Recursion to Convert Number to Other Number Bases Data Structures in Java with JUnit ©Rick Mercer.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
JAVA: An Introduction to Problem Solving & Programming, 7 th Ed. By Walter Savitch ISBN © 2015 Pearson Education, Inc., Upper Saddle River,
The Hexadecimal System is base 16. It is a shorthand method for representing the 8-bit bytes that are stored in the computer system. This system was chosen.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Number Systems and Binary Arithmetic Quantitative Analysis II Professor Bob Orr.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
Welcome to Recursion! Say what?!? Recursion is… the process of solving large problems by simplifying them into smaller ones. similar to processing using.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
CSC 205 Programming II Lecture 8 Recursion.
Digital Design Chapter One Digital Systems and Binary Numbers
Data Structures with Java © Rick Mercer
Recursion DRILL: Please take out your notes on Recursion
Recursion: The Mirrors
Discrete Mathematics Numbering System.
Number Systems and Binary Arithmetic
Convert Decimal to Binary
Data Structures in Java with JUnit ©Rick Mercer
Base ‘b’ number for i = 0 to n – 1 for an n digit quantity
Number System conversions
Rick Mercer, Allison Obourn, Marty Stepp
Number Systems and Binary Arithmetic
Digital Electronics and Microprocessors
Chapter 2: Number Systems
AB AC AD AE AF 5 ways If you used AB, then, there would be 4 remaining ODD vertices (C, D, E and F) CD CE CF 3 ways If you used CD, then, there.
CSC 143 Recursion.
ITEC324 Principle of CS III
Number systems Converting numbers between binary, octal, decimal, hexadecimal (the easy way)
Dr. Clincy Professor of CS
Presentation transcript:

Examples of Recursion Data Structures in Java with JUnit ©Rick Mercer

Recursion = Recursion( Again-1 ); A Combinatorial method  This example of a recursive solution comes from the field of Combinatorics  Problem: A D.J. plays 10 songs each hour. There are 40 different songs. How many different one hour sets are possible?  Or in general given 40 different things, how many sets of size 10 can be chosen  Or using the standard notation – n choose k

Recursive n choose k (con.)  In any given hour, we could play "Stairway"  All the possible sets are those that have "Stairway"and those that don't ( sum below)  After picking this one song--now a simpler problem--the DJ must pick 9 more from the remaining 39 songs (from 39 choose 9)  Possible sets are those with "Stairway", and those without

From 5 choose 2  Consider a simpler problem, from 5 letters choose 2 A B C D E A B C D E  All the sets with A (from 4 choose 1):  AB AC AD AE  Those without A (from 4 choose 2, no A):  BC BD BE CD CE DE

Recursive n choose k (con.)  Or, in general, we get  Rewritten as a method named c that has two parameters n and k:  We're getting closer but where is the base case?

The base cases for n choose k  First, n must be at least as big as k:  We cannot choose a 10 song set from 9 songs  When n == k, there is only one choice  only one possible 10 song set from 10 songs  To be meaningful, k must be at least 1  We're not interested in sets with 0 songs  When k is 1, there are n ways to choose  If we only play 1 song sets, and we have 10 songs to choose from, we get n, or 10, possible sets

Finally, here is the recursive definition of n choose k  The recursive definition of n choose k summarizes all of these points: What is c(5, 2)? ____________ What is c(4, 1)? ____________ What is c(4, 2)? ____________ What is c(6, 3)? ____________

Another Example  Next example does not need recursion either  but it does provide further insight into recursive solutions  and insights into a RecursionFun assignment

Converting Decimal Numbers to other bases  Problem: Convert a decimal (base 10) number into other bases method Call Return method Call Return convert(99, 2) convert(99, 2) convert(99, 3) convert(99, 3) convert(99, 4) 1203 convert(99, 4) 1203 convert(99, 5) 344 convert(99, 5) 344 convert(99, 6) 243 convert(99, 6) 243 convert(99, 7) 201 convert(99, 7) 201 convert(99, 8) 143 convert(99, 8) 143 convert(99, 9) 120 convert(99, 9) 120

Digits are multiplied by powers of the base 10, 8, 2, or whatever  First: converting from other bases to decimal  Decimal numbers multiply digits by powers of = 9 x x x x 10 0  Octal numbers powers of = 1 x x x x 8 0 = = = =  Binary numbers powers of = 1 x x x x 2 0 = = = = 13 10

Converting base 10 to base 2 1) divide number (5) by new base(2), write remainder (1) 2) divide quotient (2), write new remainder (0) to left 3) divide quotient (1), write new remainder (1) to left _ _ 2_ _ _ 2_ 2 ) 5 Remainder = 1 2 ) 5 Remainder = 1 __1_ __1_ 2 ) 2 Remainder = 0 2 ) 2 Remainder = 0 __0_ __0_ 2 ) 1 Remainder = 1 2 ) 1 Remainder = 1 Stop when the quotient is = Stop when the quotient is = 101 2

Converting base 10 to base 8 1) divide number by new base (8), write remainder (1) 2) divide quotient (2), write new remainder (0) to left 3) divide quotient (1), write new remainder (1) to left _12_ _12_ 8 )99 Remainder = 3 8 )99 Remainder = 3 __1_ __1_ 8 )12 Remainder = 4 8 )12 Remainder = 4 __0_ __0_ 8 ) 1 Remainder = 1 8 ) 1 Remainder = 1 Stop when the quotient is = 143 8

Possible Solutions  We could either  store remainders in an array and reverse it or  write out the remainders in reverse order  have to postpone the output until we get quotient = 0  store result as a String (like a Recursion assignment)  Iterative Algorithm while the decimal number > 0 { while the decimal number > 0 { Divide the decimal number by the new base Divide the decimal number by the new base Set the decimal number = decimal number divided by the base Set the decimal number = decimal number divided by the base Store the remainder to the left of any preceding remainders Store the remainder to the left of any preceding remainders }

Recursive algorithm Base Case -- Recursive Case  Base case  if decimal number being converted = 0  do nothing (or return "")  Recursive case  if decimal number being converted > 0  solve a simpler version of the problem –use the quotient as the argument to the next call  store the current remainder (number % base) in the correct place

One solution assertEquals("14", rf.convert(14, 10)); assertEquals(" ", rf.convert(99, 2)); assertEquals("143", rf.convert(99, 8)); assertEquals("98", rf.convert(98, 10)); // 9*10+8 public String convert(int num, int base) { if (num == 0) if (num == 0) return ""; return ""; else else return convert(num/base, base) + (num%base); return convert(num/base, base) + (num%base);}

Hexadecimal, something we see as Computer Scientists  Convert this algorithm to handle all base up through hexadecimal (base 16)  10 = A  11 = B  12 = C  13 = D  14 = E  15 = F

Array Processing with recursion Needs a private helper  Modify the given array so that all the even numbers come before all the odd numbers. Other than that, the numbers can be in any order. Use recursion. Do not use a loop evensLeft({1, 0, 1, 0, 0, 1}) → {0, 0, 0, 1, 1, 1} evensLeft({3, 3, 2}) → {2, 3, 3} evenLeft({2, 2, 2}) → {2, 2, 2}

Private Helper  Like binaraySearch in the current project, evensLeft does not have enough information passed to the public method  Solution: Call a recursive private helper method with additional arguments public void evensLeft(int[] a) { evensLeft(a, 0, 0); // Maintain where to swap evensLeft(a, 0, 0); // Maintain where to swap} public void evensLeft(int[] a, int i, int swapIndex) { int i, int swapIndex) { //... //...}