©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 6, 15) Recursive Data Structures.

Slides:



Advertisements
Similar presentations
Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Recursive methods. Recursion A recursive method is a method that contains a call to itself Often used as an alternative to iteration when iteration is.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 4 Defining Your Own Classes.
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.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
Chapter 17 Recursion. Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display Mathematical Definition: RunningSum(1)
Chapter 15 Recursive Algorithms.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursion Gordon College CPS212
Intro to OOP with Java, C. Thomas Wu Characters and Strings
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Chapter 15 Recursive Algorithms. 2 Recursion Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Odds and Ends Strings (from Chapter 9) StringTokenizer.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
JavaScript, Third Edition
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
Characters and Strings. Representation of single characters Data type char is the data type that represents single characters, such as letters, numerals,
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
Lesson 3 – Regular Expressions Sandeepa Harshanganie Kannangara MBCS | B.Sc. (special) in MIT.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 9 Characters and Strings.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved ADT Implementation:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Characters In Java, single characters are represented.
Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Recursion Chapter 11. The Basics of Recursion: Outline Introduction to Recursion How Recursion Works Recursion versus Iteration Recursive Methods That.
Recursion Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Lecturer: Dr. AJ Bieszczad Chapter 11 COMP 150: Introduction to Object-Oriented Programming 11-1 l Basics of Recursion l Programming with Recursion Recursion.
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.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 9 Characters and Strings (adapted from.
Regular Expressions.
Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 1 Regular Expressions and String processing Animated Version.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
By: Lokman Chan Recursive Algorithm Recursion Definition: A function that is define in terms of itself. Goal: Reduce the solution to.
When you read a sentence, your mind breaks it into tokens—individual words and punctuation marks that convey meaning. Compilers also perform tokenization.
Chapter 12: Collections by Lewis and Loftus (Updated by Dan Fleck) Coming up: Collections.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
Chapter 11Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 11 l Basics of Recursion l Programming with Recursion Recursion.
By Mr. Muhammad Pervez Akhtar
Chapter 111 Recursion Chapter Objectives become familiar with the idea of recursion learn to use recursion as a programming tool become familiar.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
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:
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Chapter 15 Recursion.
Chapter 15 Recursive Algorithms
Going further Enumerated types Recursion Collections.
Chapter 17 Recursion.
Lecture 19 Strings and Regular Expressions
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Algorithm design and Analysis
Chapter 15 Recursive Algorithms Animated Version
Chapter 17 Recursion.
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 6, 15) Recursive Data Structures Pattern Matching (Chapter 9) Other languages: C# Using an IDE: Eclipse This material will not be on the final.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 2 Recursion Recursion solves a problem by breaking it down into one or more smaller problems with the same structure. Examples –Any problem that involves doing something with each element of a list can be thought of as processing the first element and then processing the rest of the list –Many mathematical functions are defined recursively –Data structures can be recursive Directory structures are recursive –The best sorting algorithms are recursive An recursive method is a method that makes a call to itself. Basic solution structure –If problem is small enough, solve it –If not, recur on a smaller version of the problem.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 3 Recursive Definitions The factorial of N is the product of the first N positive integers: N * (N – 1) * (N – 2 ) *... * 2 * 1 The factorial of N can be defined recursively as 1 if N = 1 factorial( N ) = N * factorial( N-1 ) otherwise

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 4 Recursive Method Implementing the factorial of N recursively will result in the following method. public int factorial( int N ) { if ( N == 1 ) { return 1; } else { return N * factorial( N-1 ); } } Test to stop or continue. Recursive case: recursion continues. End case: recursion stops.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 5 Anagrams List all anagrams of a given word. Word C A T C T A A T C A C T T C A T A C Anagrams

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 6 Recursive Solution The basic idea is to make recursive calls on a sub-word after every rotation. Here’s how: C C A A T T Recursion A A T T C C T T C C A A Rotate Left C A T C T A A T C A C T T C A T A C

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 7 Towers of Hanoi The goal of the Towers of Hanoi puzzle is to move N disks from peg 1 to peg 3: –You must move one disk at a time. –You must never place a larger disk on top of a smaller disk.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 8 Towers of Hanoi Solution

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 9 Recursive Sorting Algorithms There are two commonly used sorting methods that use recursion –Merge sort: sort each half of the array and merge the sorted halves together –Quicksort: use a pivot value to separate the array into two parts Both these methods are more efficient that the sorts we’ve seen before –N log N instead of N 2

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 10 Quicksort To sort an array from index low to high, we first select a pivot element p. –Move all elements less than the pivot to the first half of an array and all elements larger than the pivot to the second half. Put the pivot in the middle. –Recursively apply quicksort on the two halves.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 11 When Not to Use Recursion When recursive algorithms are designed carelessly, it can lead to very inefficient and unacceptable solutions. For example, consider the following: public int fibonacci( int N ) { if (N == 0 || N == 1) { return 1; } else { return fibonacci(N-1) + fibonacci(N-2); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 12 Excessive Repetition Recursive Fibonacci ends up repeating the same computation numerous times.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 13 Static vs. Dynamic Structures A static data structure has a fixed size Arrays are static –Once you instantiate it, the length is constant –To get a different size, you have to instantiate a new array –An ArrayList can change its size but it really just hides the work involved in changing the size of a static structure Dynamic data structures grow and shrink as needed at execution time A dynamic data structure is implemented using reference variables called links

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 14 References as Links Recall that an object reference is a variable that stores the address of an object Object references can be used to create links between objects Suppose a Student object contains a reference to another Student object John Smith Jane Jones

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 15 References as Links References can be used to create a variety of linked structures, such as a linked list: This is called a LinkedList –LinkedList class has same methods as ArrayList studentList

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 16 Other Dynamic List Representations doubly linked list, with next and previous references A queue adds items to the rear of the list and removes them from the front list enqueue dequeue

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 17 Trees and Binary Trees A tree is a non-linear data structure that consists of a root node and potentially many levels of additional nodes that form a hierarchy –Each node can be linked to at least two other nodes A binary tree is defined recursively. Either it is empty (the base case) or it consists of a root and two subtrees, each of which is a binary tree Directories form a tree-like structure

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 18 Other Languages There are a number of other languages with a syntax very similar to that of Java –Java's syntax is based on that of C which is not object- oriented –C++ was designed to be backwards-compatible with C –C# was based on C/C++ without the backwards- compatibility –Many of the scripting languages started from C's syntax JavaScript is a scripting language that is used inside web pages –It is not related to Java

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Other Languages: C# Like Java, C# is –A modern, general-purpose object-oriented language –Based on C and C++ C# is part of the.NET family of languages supported by MicroSoft –Multiple languages which can interoperate –Languages compile to a common intermediate language –Common Language Runtime runs programs from all the.NET languages

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C# Program Structure A program consists of one or more files A file can contain one or more classes and/or namespaces –name of file is not tied to name of class At least one class must contain Main –There are several allowed signatures return type is either int or void either no parameter or String array

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. First Program namespace FirstProgram { class First { static void Main() { System.Console.WriteLine( "Welcome to C#!"); }

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Console I/O System.Console.WriteLine( arg) –argument can be any type –for objects, ToString is invoked System.Console.ReadLine() –returns a String System is a namespace –using System; allows you to omit the namespace when calling the method

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C# Types Value types –simple types: primitive types from Java plus unsigned types and decimal Reference types - objects

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Operators Has same operators as Java with similar precedence and associativity == compares values for strings and simple types, addresses for all other objects

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. C# on onyx mono is an open-source project that provides facilities for running C# programs under Linux Compile a program by typing mcs First.cs Run a program by typing mono First.exe

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 26 Using an IDE IDE = Integrated Development Environment –Do all your editing, compiling and running from inside the IDE program –Debugger allows you to execute your program in steps and look at the variables to see what is happening. Eclipse is an IDE used for Java –available on onyx

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 27 Patterns and Regular Expressions Chapter 9, sections 3-4

Regular Expressions When you are searching for –words in a document –files with a particular name When you need to determine whether a string has the correct formatto be –a telephone number –a zip code –a student ID you need a way of describing what you want to see Regular expressions provide a special language that can be used for these types of tasks

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 29 Regular Expressions The pattern is called a regular expression. Rules –The brackets [ ] represent choices –The asterisk symbol * means zero or more occurrences. –The plus symbol + means one or more occurrences. –The hat symbol ^ means negation. –The hyphen – means ranges. –The parentheses ( ) and the vertical bar | mean a range of choices for multiple characters.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 30 Pattern Example Suppose students are assigned a three-digit code: –The first digit represents the major (5 indicates computer science); –The second digit represents either in-state (1), out-of-state (2), or foreign (3); –The third digit indicates campus housing: On-campus dorms are numbered 1-7. Students living off-campus are represented by the digit 8. The 3-digit pattern to represent computer science majors living on-campus is 5[123][1-7] first character is 5 second character is 1, 2, or 3 third character is any digit between 1 and 7

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 31 Regular Expression Examples Matches AZxx, CAxx, and COxx, where x is a single digit. (AZ|CA|CO)[0-9][0-9] Matches wad, weed, bad, and beed. [wb](ad|eed) A valid Java identifier consisting of alphanumeric characters, underscores, and dollar signs, with the first character being an alphabet. [a-zA-z][a-zA-Z0- 9_$]* A single digit 0, 1, or 3. [013] A single character that is either a lowercase letter or a digit. [a-z0-9] A single digit that is 0, 1, 2, 3, 8, or 9. [0-9&&[^4567]] Any two-digit number from 00 to 99. [0-9][0-9] DescriptionExpression

Java Classes for Pattern Matching The String class has replaceAll, split and matches that use strings representing patterns The Pattern and Matcher classes are provided to support pattern matching operations Scanner uses patterns for delimiters

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 33 The replaceAll Method The replaceAll method replaces all occurrences of a substring that matches a given regular expression with a given replacement string. String originalText, modifiedText; originalText =...; //assign string modifiedText = Replace all vowels with the

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 34 The Pattern and Matcher Classes The matches and replaceAll methods of the String class are shorthand for using the Pattern and Matcher classes from the java.util.regex package. If str and regex are String objects, then str.matches(regex); is equivalent to Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(str); matcher.matches();

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 35 The compile Method The compile method of the Pattern class converts the stated regular expression to an internal format to carry out the pattern-matching operation. This conversion is carried out every time the matches method of the String class is executed, so it is more efficient to use the compile method when we search for the same pattern multiple times. See the sample programs Ch9MatchJavaIdentifier2 and Ch9PMCountJava

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 36 The find Method The find method is another powerful method of the Matcher class. –It searches for the next sequence in a string that matches the pattern, and returns true if the pattern is found. When a matcher finds a matching sequence of characters, we can query the location of the sequence by using the start and end methods. See Ch9PMCountJava2