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

Slides:



Advertisements
Similar presentations
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 Recursive Algorithms.
Advertisements

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
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.
Chapter 9: Data Structures I
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Recursion CSC 220: Data Structure Winter Introduction A programming technique in which a function calls itself. One of the most effective techniques.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 6, 15) Recursive Data Structures.
Computer Science II Recursion Professor: Evan Korth New York University.
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.
CISH 4960 Introduction to Programming Lecture 101 Introduction to Programming Lecture 10 Recursion/Data Structures Tom Blough
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 12: Data Structures
Chapter 15 Recursive Algorithms.
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++
Recursion. 2 CMPS 12B, UC Santa Cruz Solving problems by recursion How can you solve a complex problem? Devise a complex solution Break the complex problem.
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.
1 Data Structures  We can now explore some advanced techniques for organizing and managing information  Chapter 12 of the book focuses on: dynamic structures.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 15 Recursive Algorithms.
19-Aug-15 Simple Recursive Algorithms. 2 A short list of categories Algorithm types we will consider include: Simple recursive algorithms Backtracking.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Chapter 12 Data Structure Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
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.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
Data Structures and Abstractions with Java, 4e Frank Carrano
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.
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.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
ELC 310 Day 24. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Agenda Questions? Problem set 5 Parts A & B Corrected  Good results  2 A’s, 1.
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.
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.
CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself. 
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
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:
Recursion Chapter 10 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Recursion Chapter 10 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Chapter 19: Recursion.
12 Collections Software Solutions Lewis & Loftus java 5TH EDITION
Chapter 15 Recursion.
Chapter 15 Recursive Algorithms
Going further Enumerated types Recursion Collections.
Chapter 15 Recursion.
Chapter 12: Data Structures
Chapter 12 Collections.
Algorithm design and Analysis
Chapter 15 Recursive Algorithms Animated Version
Chapter 12 Collections.
Java Programming: Chapter 9: Recursion Second Edition
Presentation transcript:

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures 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 is a process that proceeds by breaking a problem 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 contains a statement (or statements) that makes a call to itself. Basic solution structure –If problem is the base case (the simplest version of the problem), solve it –If not the base case, recur on a smaller version of the problem.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 3 Recursion 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 ) N>1

©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 Anagram 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 Anagram 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 define the number of elements it can hold, the number doesn’t change –An ArrayList can change its size but it really just hides the work involved in changing the size of a static structure A dynamic data structure grows and shrinks at execution time as required by its contents A dynamic data structure is implemented using 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 (or stack) –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 Related Languages C is a procedural language –Not object-oriented –Efficient –Provides low-level access to hardware C++ is an object-oriented language which is backwards compatible with C

©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