CSE 143 Lecture 14 More Recursive Programming; Grammars

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 13
Advertisements

Building Java Programs Chapter 11 Java Collections Framework.
CSE 143 Lecture 11 Recursive Programming reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 12 Lecture 12-2: recursive programming reading:
Building Java Programs Chapter 13 Searching reading: 13.3.
CSE 143 Lecture 11 Maps Grammars slides created by Alyssa Harding
CSE 143 Lecture 15 Binary Search; Comparable reading: 13.2; 10.2 slides created by Marty Stepp
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
CSE 143 Lecture 13 Recursive Programming reading: slides created by Marty Stepp
CSC 205 Programming II Lecture 9 More on Recursion.
CSE 143 Lecture 14 (B) Maps and Grammars reading: 11.3 slides created by Marty Stepp
CSE 143 Lecture 6 Inheritance; binary search reading: 9.1, ; 13.1 slides created by Marty Stepp
Building Java Programs Chapter 11 Java Collections Framework Copyright (c) Pearson All rights reserved.
CSE 143 Lecture 14 Maps/Sets; Grammars reading: slides adapted from Marty Stepp and Hélène Martin
Binary search and complexity reading:
1 Recursive algorithms Recursive solution: solve a smaller version of the problem and combine the smaller solutions. Example: to find the largest element.
Sorting Algorithms. Sorting Sorting is a process that organizes a collection of data into either ascending or descending order. public interface ISort.
CSE 143 Lecture 16 Iterators; Grammars reading: 11.1, 15.3, 16.5 slides created by Marty Stepp
Building Java Programs Chapter 11 Java Collections Framework Copyright (c) Pearson All rights reserved.
Building Java Programs Chapter 12: Recursive public/private pairs Chapter 13: Searching reading: 13.3.
Lecture 25: Searching and Sorting
Building Java Programs
Sort & Search Algorithms
Lecture 10: recursive programming reading:
COP 3503 FALL 2012 Shayan Javed Lecture 15
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
slides created by Marty Stepp and Hélène Martin
Adapted from slides by Marty Stepp and Stuart Reges
CSc 110, Autumn 2017 Lecture 37: searching and sorting
Building Java Programs
Road Map - Quarter CS Concepts Data Structures Java Language
Building Java Programs
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
Lecture 14: binary search and complexity reading:
Building Java Programs
Building Java Programs
slides adapted from Marty Stepp
Lecture 10: recursive programming reading:
CSE 143 Lecture 14 (A) More Recursive Programming
CSE 143 Lecture 11 Recursive Programming reading:
CSc 110, Spring 2017 Lecture 39: searching and sorting
Lecture 15: binary search reading:
Maps Grammars Based on slides by Marty Stepp & Alyssa Harding
Building Java Programs
CSE 143 Lecture 11 Recursive Programming slides created by Marty Stepp
Adapted from slides by Marty Stepp and Stuart Reges
CSE 373 Data Structures and Algorithms
Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection.
CSE 143 Lecture 16 (A) Searching and Comparable
slides created by Marty Stepp
slides created by Marty Stepp
Recursion Based on slides by Alyssa Harding
slides created by Alyssa Harding
Adapted from slides by Marty Stepp and Stuart Reges
slides created by Alyssa Harding
Plan for Lecture Review code and trace output
Suggested self-checks: Section 7.11 #1-11
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
CSE 143 Lecture 13 Recursive Programming reading:
Building Java Programs
CSE 143 Lecture 2 ArrayIntList, binary search
Lecture 21: Searching and Sorting
Lecture 10: recursive programming reading:
slides adapted from Marty Stepp and Hélène Martin
CSE 143 Lecture 14 Searching and Comparable reading: ; 10.2
slides created by Alyssa Harding
Searching.
Implementing ArrayIntList
Sorting Algorithms.
Presentation transcript:

CSE 143 Lecture 14 More Recursive Programming; Grammars reading: 12.2 - 12.3, 12.5; 13.3 slides created by Marty Stepp http://www.cs.washington.edu/143/

Exercise Write a method crawl accepts a File parameter and prints information about that file. If the File object represents a normal file, just print its name. If the File object represents a directory, print its name and information about every file/directory inside it, indented. cse143 handouts syllabus.doc lecture_schedule.xls homework 1-sortedintlist ArrayIntList.java SortedIntList.java index.html style.css recursive data: A directory can contain other directories.

File objects A File object (from the java.io package) represents a file or directory on the disk. Constructor/method Description File(String) creates File object representing file with given name canRead() returns whether file is able to be read delete() removes file from disk exists() whether this file exists on disk getName() returns file's name isDirectory() returns whether this object represents a directory length() returns number of bytes in file listFiles() returns a File[] representing files in this directory renameTo(File) changes name of file

Public/private pairs We cannot vary the indentation without an extra parameter: public static void crawl(File f, String indent) { Often the parameters we need for our recursion do not match those the client will want to pass. In these cases, we instead write a pair of methods: 1) a public, non-recursive one with the parameters the client wants 2) a private, recursive one with the parameters we really need

Exercise solution 2 // Prints information about this file, // and (if it is a directory) any files inside it. public static void crawl(File f) { crawl(f, ""); // call private recursive helper } // Recursive helper to implement crawl/indent behavior. private static void crawl(File f, String indent) { System.out.println(indent + f.getName()); if (f.isDirectory()) { // recursive case; print contained files/dirs for (File subFile : f.listFiles()) { crawl(subFile, indent + " ");

Recursive binary search (13.3) Write a method binarySearch that accepts a sorted array of integers and a target integer value and returns the index of an occurrence of that target value in the array. If the target value is not found, return a negative number. Write the method recursively. int index = binarySearch(data, 42); // 10 int index2 = binarySearch(data, 66); // -1 or -14 index 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 value -4 20 22 25 30 36 42 50 56 68 85 92 103

Exercise solution // Returns the index of an occurrence of the given value in // the given array, or a negative number if not found. // Precondition: a is sorted public static int binarySearch(int[] a, int target) { return binarySearch(a, target, 0, a.length - 1); } // Recursive helper to implement search behavior. private static int binarySearch(int[] a, int target, int min, int max) { if (min > max) { return -1; // not found } else { int mid = (min + max) / 2; if (a[mid] == target) { return mid; // found it! } else if (a[mid] < target) { // middle element too small; search right half return binarySearch(a, target, mid + 1, max); } else { // a[mid] < target // middle element too large; search left half

Languages and grammars (formal) language: A set of words or symbols. grammar: A description of a language that describes which sequences of symbols are allowed in that language. describes language syntax (rules) but not semantics (meaning) can be used to generate strings from a language, or to determine whether a given string belongs to a given language

Backus-Naur (BNF) Backus-Naur Form (BNF): A syntax for describing language grammars in terms of transformation rules, of the form: <symbol> ::= <expression> | <expression> ... | <expression> terminal: A fundamental symbol of the language. non-terminal: A high-level symbol describing language syntax, which can be transformed into other non-terminal or terminal symbol(s) based on the rules of the grammar. developed by two Turing-award-winning computer scientists in 1960 to describe their new ALGOL programming language

An example BNF grammar <s> ::= <n> <v> <n> ::= Marty | Victoria | Stuart | Jessica <v> ::= cried | slept | belched Some sentences that could be generated from this grammar: Marty slept Jessica belched Stuart cried

BNF grammar version 2 <s> ::= <np> <v> <np> ::= <pn> | <dp> <n> <pn> ::= Marty | Victoria | Stuart | Jessica <dp> ::= a | the <n> ::= ball | hamster | carrot | computer <v> ::= cried | slept | belched Some sentences that could be generated from this grammar: the carrot cried Jessica belched a computer slept

BNF grammar version 3 <s> ::= <np> <v> <np> ::= <pn> | <dp> <adj> <n> <pn> ::= Marty | Victoria | Stuart | Jessica <dp> ::= a | the <adj> ::= silly | invisible | loud | romantic <n> ::= ball | hamster | carrot | computer <v> ::= cried | slept | belched Some sentences that could be generated from this grammar: the invisible carrot cried Jessica belched a computer slept a romantic ball belched

Grammars and recursion <s> ::= <np> <v> <np> ::= <pn> | <dp> <adjp> <n> <pn> ::= Marty | Victoria | Stuart | Jessica <dp> ::= a | the <adjp> ::= <adj> <adjp> | <adj> <adj> ::= silly | invisible | loud | romantic <n> ::= ball | hamster | carrot | computer <v> ::= cried | slept | belched Grammar rules can be defined recursively, so that the expansion of a symbol can contain that same symbol. There must also be expressions that expand the symbol into something non-recursive, so that the recursion eventually ends.

Grammar, final version <s>::=<np> <vp> <np>::=<dp> <adjp> <n>|<pn> <dp>::=the|a <adjp>::=<adj>|<adj> <adjp> <adj>::=big|fat|green|wonderful|faulty|subliminal <n>::=dog|cat|man|university|father|mother|child <pn>::=John|Jane|Sally|Spot|Fred|Elmo <vp>::=<tv> <np>|<iv> <tv>::=hit|honored|kissed|helped <iv>::=died|collapsed|laughed|wept Could this grammar generate the following sentences? Fred honored the green wonderful child big Jane wept the fat man fat Generate a random sentence using this grammar.

Sentence generation <s> <np> <vp> <pn> Fred <tv> honored <dp> <adjp> <n> the <adj> child green wonderful