LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

David Notkin Autumn 2009 CSE303 Lecture 13 This space for rent.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
Arrays, A1 COMP 401, Fall 2014 Lecture 4 8/28/2014.
CSC 212 – Data Structures. Using Stack Stack Limitations  Great for Pez dispensers, JVMs,& methods  All of these use most recent item added only 
CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.
CSC 213 – Large Scale Programming. Today’s Goals  Look at how Dictionary s used in real world  Where this would occur & why they are used there  In.
CS-I Final Review Hao Jiang Computer Science Department Boston College.
CS 177 Recitation Week 8 – Methods. Questions? Announcements  Project 3 milestone due next Thursday 10/22 9pm  Turn in with: turnin –c cs177=xxxx –p.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Review of Recursion What is a Recursive Method? The need for Auxiliary (or Helper) Methods How Recursive Methods work Tracing of Recursive Methods.
Chapter 3: Arrays, Linked Lists, and Recursion
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.
CSC 212 – Data Structures Lecture 13: Linked Lists.
Options for User Input Options for getting information from the user –Write event-driven code Con: requires a significant amount of new code to set-up.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
CSC 107 – Programming For Science. Today’s Goal  Learn how arrays normally used in real programs  Why a function returning an array causes bugs  How.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
CSC 213 – Large Scale Programming. Today’s Goal  Consider what will be important when searching  Why search in first place? What is its purpose?  What.
Stacks & Recursion. Stack pushpop LIFO list - only top element is visible top.
Humorous Asides “A journey begins with single step”
CSC 213 – Large Scale Programming. Today’s Goals  Look at how Dictionary s used in real world  Where this would occur & why they are used there  In.
Question of the Day While walking across a bridge I saw a boat filled with people. Nobody boarded or left the boat, but on board the boat there was not.
COSC 2006 Data Structures I Recursion II
LECTURE 37: ORDERED DICTIONARY CSC 212 – Data Structures.
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.
Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,
Humorous Asides “A journey begins with single step”
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
CMSC 202 Arrays. Aug 6, Introduction to Arrays An array is a data structure used to process a collection of data that is all of the same type –An.
Problem of the Day  Why are manhole covers round?
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
LECTURE 26: QUEUES CSC 212 – Data Structures. Using Stack.
CSC 211 Data Structures Lecture 13
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
ECE122 Feb. 22, Any question on Vehicle sample code?
LECTURE 34: MAPS & HASH CSC 212 – Data Structures.
CSC 107 – Programming For Science. The Week’s Goal.
LECTURE 24: STACK ADTS CSC 212 – Data Structures.
CSC 107 – Programming For Science. Today’s Goal  Better understand arrays and how they work  Using array variable & its entries  When calling function,
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
CSC 212 Sequences & Iterators. Announcements Midterm in one week  Will cover through chapter 5 of book  Midterm will be open book, open note (but, closed.
Question of the Day A friend tells the truth when saying: A road near my house runs directly north-south; I get on the road facing north, drive for a mile,
CSC 212 – Data Structures Lecture 5: Variables. Problem of the Day Why do underground subway stations always have more escalators going up than down?
LECTURE 20: RECURSION CSC 212 – Data Structures. Humorous Asides.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
Question of the Day You overhear a boy & his mother talking: Mom:What is ? Boy: That's easy, 33. Mom: Good. What's ? Boy:Simple. It's 40. Mom:Excellent!
Question of the Day  How can you change the position of 1 toothpick and leave the giraffe in exactly the same form, but possibly mirror-imaged or oriented.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSC Programming for Science Lecture 27: Arrays as Parameters, Searching, & Sorting.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
Recursion ITI 1121 N. El Kadri. Reminders about recursion In your 1 st CS course (or its equivalent), you have seen how to use recursion to solve numerical.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
LECTURE 22: BIG-OH COMPLEXITY CSC 212 – Data Structures.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists (cont) [Chapter 4; Chapter 6, pp ]
Recursion -- Introduction
CSE 116/504 – Intro. To Computer Science for Majors II
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
Unit 3 Test: Friday.
CS100J Lecture 8 Previous Lecture This Lecture Programming Concepts
Java Programming Language
Presentation transcript:

LECTURE 21: RECURSION & LINKED LIST REVIEW CSC 212 – Data Structures

 Easy to create & use  Java support built-in  Immediate access  Can get any entry  Size is fixed from start  Recursive structure  Need DNode & Dlist  Slower to access  Usually need traversal  Size changes w/ data ArraysLinked Lists Linked Lists v. Arrays

 Linked lists not always obvious to code  Lots of references must be set (“aliased”) ‏  Easy to screw-up, especially by mixing ordering  Very easy solution to help do this, however  Begin by drawing picture of method does  Write method skeleton (declaration & braces {} ) ‏  Start method by aliasing variable to each instance  Now assign fields making them equal to your picture  Double-check by comparing trace with your picture Using Linked Lists

 Identify two parts of all recursive solutions  Cases when problem is trivial to solve “base case(s)”  Split into identical, smaller problems at RECURSIVE step(s) ‏  When both parts cannot be determined, stop  Do not always have recursive solution for a problem  Make sure you will always reach base case  Infinite recursion occurs when no base case reached… Recursive Solutions

Infinite Recursion

 Handles when only 1 possible solution remains  0 or 1 array entries remain to be processed  Linked list node’s next null or trailer (sentinel) ‏  At the last value to be processed by the method Writing Base Case

 Performs simple operation with 1 or 2 data items  Before recursive call(s) could perform the operation…  … or the recursive call(s) provide (some of) the data  Ultimately solves problem for data it considers  Mostly, uses recursive calls to do the work  Something is wrong if always solving entire problem  Question becomes: WHAT DATA TO CONSIDER?  Needs to shrink with each recursive call  Should be defined by method’s parameters Writing Recursive Step(s)‏

 Public method only has required parameters  Linked list instance to be processed  Reference to the array to be evaluated  Important value whose result the user is interested  Private method may have more parameters  Value or array index where method starts working  Value or array index where method stop working  Range or indices where method starts & stops Which Case is Which?

public int findMin(int[] arr) { return findMin(arr, 0); } private int findMin(int[] arr, int loc) { if (loc == arr.length – 1) { return arr[loc]; } else { int minAfter = findMin(arr, loc + 1); if (minAfter < arr[loc]) { return minAfter; } else { return arr[loc]; } } Where Method Starts Work

public void printTri(int[] arr) { return printTri(arr, arr.length - 1); } private int printTri(int[] arr, int loc) { if (loc == 0) { System.out.println(arr[loc]); } else { for (int i = 0; i < loc; i++) { System.out.print(arr[i] + “ ”); } System.out.println(); printTri(arr, loc - 1); } } Where Method Stops Work

public boolean pal(String str) { return pal(str, 0, str.length() - 1); } private boolean pal(String str, int start, int end){ if (start > end) { return true; } else { boolean plndrm = pal(str, start + 1, end - 1); char fst = str.charAt(start); char lst = str.charAt(end); return plndrm && (fst == lst); } } Range To Use

 Start week #8 assignment  Due by 5PM next Tuesday  Start programming assignment #3  Messages are not always sent to everyone!  Read sections in book before class  Is all code really equal?  How can we determine how fast code runs? Before Next Lecture… NO!