Building Java Programs

Slides:



Advertisements
Similar presentations
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Advertisements

Chapter 1: INTRODUCTION TO DATA STRUCTURE
Introduction to Recursion and Recursive Algorithms
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 12: Recursion.
Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Unit 181 Recursion Definition Recursive Methods Constructing Recursion Benefits and Usage Infinite Recursion Recursion Removal Examples Exercises.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
CSE 143 Lecture 11 Recursive Programming reading: slides created by Marty Stepp and Hélène Martin
Week 2 - Monday.  What did we talk about last time?  Exceptions  Threads  OOP  Interfaces.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 8 Recursion Modified.
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
Building Java Programs Chapter 12 Recursion Copyright (c) Pearson All rights reserved.
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
CSE 143 Lecture 10 Recursion reading: slides created by Marty Stepp and Hélène Martin
1 Java's Collection Framework Map and Sets. 2 Collection Framework  A collections framework is a unified architecture for representing and manipulating.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
CSE 143 Lecture 9 Recursion slides created by Alyssa Harding
CSE 143 Lecture 9: introduction to recursion reading: 12.1.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Lecture 11 Recursion. A recursive function is a function that calls itself either directly, or indirectly through another function; it is an alternative.
Lecture 4 CS140 Dick Steflik. Reading Keyboard Input Import java.util.Scanner – A simple text scanner which can parse primitive types and strings using.
CMSC201 Computer Science I for Majors Lecture 19 – Recursion
[ 8.00 ] [ Today’s Date ] [ Instructor Name ]
Building Java Programs
Recursion Topic 5.
Lecture 24: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Lecture 9: introduction to recursion reading: 12.1
Building Java Programs
Recursion DRILL: Please take out your notes on Recursion
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
CSC 221: Computer Programming I Spring 2010
Chapter 19 Java Data Structures
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
CSC 221: Computer Programming I Fall 2005
CMSC201 Computer Science I for Majors Lecture 18 – Recursion
Week 15 – Monday CS221.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Building Java Programs
Road Map CS Concepts Data Structures Java Language Java Collections
Road Map CS Concepts Data Structures Java Language Java Collections
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Road Map - Quarter CS Concepts Data Structures Java Language
Building Java Programs
slides adapted from Marty Stepp and Hélène Martin
Building Java Programs
Adapted from slides by Marty Stepp, Stuart Reges & Allison Obourn.
Building Java Programs
CSE 142 vs CSE 143 CSE 142 CSE 143 You learned how to write programs and decompose large problems with: Print statements Methods Control Structures.
CSE 143 Lecture 11 Recursive Programming reading:
Building Java Programs
Road Map - Quarter CS Concepts Data Structures Java Language
Welcome to CSE 143! Go to pollev.com/cse143.
slides created by Marty Stepp and Alyssa Harding
Building Java Programs
Recursion based on slides by Alyssa Harding
Recursion Based on slides by Alyssa Harding
Lecture 19: Recursion Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Java Programming: Chapter 9: Recursion Second Edition
Road Map - Quarter CS Concepts Data Structures Java Language
Road Map CS Concepts Data Structures Java Language Java Collections
Building Java Programs
CSE 143 Lecture 13 Recursive Programming reading:
slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 13 Recursive Programming reading:
Building Java Programs
Building Java Programs
slides created by Marty Stepp
Building Java Programs
Presentation transcript:

Building Java Programs Chapter 12 introduction to recursion reading: 12.1

Road Map - Quarter CS Concepts Data Structures Java Language Client/Implementer Efficiency Recursion Regular Expressions Grammars Sorting Backtracking Hashing Huffman Compression Data Structures Lists Stacks Queues Sets Maps Priority Queues Java Language Exceptions Interfaces References Comparable Generics Inheritance/Polymorphism Abstract Classes Java Collections Arrays ArrayList 🛠 LinkedList 🛠 Stack TreeSet / TreeMap HashSet / HashMap PriorityQueue

Road Map - Week Monday Tuesday Wednesday Thursday Friday Introduce idea of “recursion” Goal: Understand idea of recursion and read recursive code. Tuesday Practice reading recursive code Wednesday More complex recursive examples Goal: Identify recursive structure in problem and write recursive code Thursday Practice writing recursive code Friday Exam logistics Set-up for A5

Recursion recursion: The definition of an operation in terms of itself. Solving a problem using recursion depends on solving smaller occurrences of the same problem. recursive programming: Writing methods that call themselves to solve problems recursively. An equally powerful substitute for iteration (loops) Particularly well-suited to solving certain types of problems

Getting down stairs Need to know two things: Most code will look like: Getting down one stair Recognizing the bottom Most code will look like: if (simplest case) { compute and return solution } else { divide into similar subproblem(s) solve each subproblem recursively assemble the overall solution }

Recursion and cases Every recursive algorithm involves at least 2 cases: base case: A simple occurrence that can be answered directly. recursive case: A more complex occurrence of the problem that cannot be directly answered, but can instead be described in terms of smaller occurrences of the same problem. Some recursive algorithms have more than one base or recursive case, but all have at least one of each. A crucial part of recursive programming is identifying these cases.

Recursion vs Iteration public static void writeStars(int n) { while (n > 0) { System.out.print("*"); n--; } System.out.println(); if (n == 0) { } else { writeStars(n – 1);

Recursion vs Iteration public static void writeStars(int n) { while (n > 0) { System.out.print("*"); n--; } System.out.println(); // base case. assert: n == 0 if (n == 0) { System.out.println(); // base case } else { writeStars(n – 1);

Recursion vs Iteration public static void writeStars(int n) { while (n > 0) { // "recursive" case System.out.print("*"); // small piece of problem n--; } System.out.println(); if (n == 0) { } else { // "recursive" case. assert: n > 0 writeStars(n – 1);

Recursion vs Iteration public static void writeStars(int n) { while (n > 0) { // "recursive" case System.out.print("*"); n--; // make the problem smaller } System.out.println(); if (n == 0) { } else { // "recursive" case. assert: n > 0 writeStars(n – 1); // make the problem smaller

Exercise Note: We did reverseDeck in lecture but they are the exact same problem Write a recursive method reverseLines that accepts a file Scanner and prints the lines of the file in reverse order. Example input file: Expected console output: I have eaten the icebox the plums that were in that were in the plums the icebox I have eaten What are the cases to consider? How can we solve a small part of the problem at a time? What is a file that is very easy to reverse?

Tracing our algorithm call stack: The method invocations currently running reverseLines(new Scanner("poem.txt")); public static void reverseLines(Scanner input) { if (input.hasNextLine()) { String line = input.nextLine(); // "I have eaten" reverseLines(input); System.out.println(line); } public static void reverseLines(Scanner input) { if (input.hasNextLine()) { String line = input.nextLine(); // "the plums" reverseLines(input); System.out.println(line); } public static void reverseLines(Scanner input) { if (input.hasNextLine()) { String line = input.nextLine(); // "that were in" reverseLines(input); System.out.println(line); } public static void reverseLines(Scanner input) { if (input.hasNextLine()) { String line = input.nextLine(); // "the icebox" reverseLines(input); System.out.println(line); } public static void reverseLines(Scanner input) { if (input.hasNextLine()) { // false ... } input file: output: I have eaten the plums that were in the icebox the icebox that were in the plums I have eaten