Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design.

Slides:



Advertisements
Similar presentations
C++ Programming:. Program Design Including
Advertisements

CS252: Systems Programming Ninghui Li Program Interview Questions.
Stacks, Queues, and Deques. 2 A stack is a last in, first out (LIFO) data structure Items are removed from a stack in the reverse order from the way they.
COSC 2006 Data Structures I Recursion III
Factorial Recursion stack Binary Search Towers of Hanoi
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.
Scott Grissom, copyright 2004 Chapter 5 Slide 1 Analysis of Algorithms (Ch 5) Chapter 5 focuses on: algorithm analysis searching algorithms sorting algorithms.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 14: Recursion by.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. ETC - 1 What comes next? Recursion (Chapter 15) Recursive Data Structures.
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.
CS 206 Introduction to Computer Science II 10 / 15 / 2008 Instructor: Michael Eckmann.
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.
© 2006 Pearson Addison-Wesley. All rights reserved3-1 Chapter 3 Recursion: The Mirrors.
Chapter 12 Collections. © 2004 Pearson Addison-Wesley. All rights reserved12-2 Collections A collection is an object that helps us organize and manage.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 16: Recursion.
© 2004 Pearson Addison-Wesley. All rights reserved12-1 Chapter 12 : Collections Intermediate Java Programming Summer 2007.
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
CHAPTER 05 Compiled by: Dr. Mohammad Omar Alhawarat Stacks & Queues.
© 2006 Pearson Education Chapter 12: Data Structures Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
Data Structures Using C++ 2E Chapter 6 Recursion.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 17: Recursion.
Department of Computer Science Data Structures Using C++ 2E Chapter 6: Recursion Learn about recursive Definitions Algorithms Functions Explore the base.
Chapters 7, 8, & 9 Quiz 3 Review 1. 2 Algorithms Algorithm A set of unambiguous instructions for solving a problem or subproblem in a finite amount of.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Data Structures and Abstractions with Java, 4e Frank Carrano
CHAPTER 02 Recursion Compiled by: Dr. Mohammad Omar Alhawarat.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
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.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
Review Introduction to Searching External and Internal Searching Types of Searching Linear or sequential search Binary Search Algorithms for Linear Search.
Edited by Malak Abdullah Jordan University of Science and Technology Data Structures Using C++ 2E Chapter 6 Recursion.
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.
Foundation of Computing Systems Lecture 3 Stacks and Queues.
ISOM MIS 215 Module 4 – Recursion. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 15 * Recursive Algorithms.
Lecture 7. Solution by Substitution Method T(n) = 2 T(n/2) + n Substitute n/2 into the main equation 2T(n/2) = 2(2(T(n/4)) + n/2) = 4T(n/4) + n And T(n)
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-solving.
CSE205 Review Session SAMUEL & JESSA. Recursion WHAT IS RECURSION?  Recursion is a tool a programmer can use to invoke a function call on itself. 
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Recursion.
Chapter 9 Recursion © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Understanding General Software Development Lesson 3.
chap10 Chapter 10 Recursion chap10 2 Recursive Function recursive function The recursive function is a kind of function that calls.
Recursion Chapter What is recursion? Recursion occurs when a method calls itself, either directly or indirectly. Used to solve difficult, repetitive.
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:
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
1 CSC 143 Recursion [Reading: Chapter 17]. 2 Recursion  A recursive definition is one which is defined in terms of itself.  Example:  Sum of the first.
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.
Recursion Chapter 2 Objectives Upon completion you will be able to:
Mohammed I DAABO COURSE CODE: CSC 355 COURSE TITLE: Data Structures.
Chapter 19: Recursion.
Chapter 15 Recursion.
Lecture 7 Queues Stacks Trees.
COSC160: Data Structures: Lists and Queues
Chapter 15 Recursion.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Chapter 8: Recursion Java Software Solutions
Data Structures Recursion CIS265/506: Chapter 06 - Recursion.
Recursion Chapter 11.
Chapter 14: Recursion Starting Out with C++ Early Objects
Chapter 8: Recursion Java Software Solutions
ITEC 2620M Introduction to Data Structures
Recursion Data Structures.
Chapter 8: Recursion Java Software Solutions
Presentation transcript:

Chapter 15: Advanced Topics: Introducing Data Structures and Recursion Visual Basic.NET Programming: From Problem Analysis to Program Design

2 Objectives Create linked lists Create a stack Create a queue Understand recursion Write recursive methods

Visual Basic.NET Programming: From Problem Analysis to Program Design3 Introducing Data Structures Linked list –List of object instances that are linked together –Objects may be added to: Beginning End Somewhere in between

Visual Basic.NET Programming: From Problem Analysis to Program Design4

5 Introducing Data Structures (continued) Stack –Can be implemented as specialized case of linked list –Objects are added and removed only at beginning of list –Employs LIFO logic Last in, first out

Visual Basic.NET Programming: From Problem Analysis to Program Design6

7 Introducing Data Structures (continued) Queue –Implemented as linked list –Objects added at end and removed from beginning –Employ FIFO logic First in, first out

Visual Basic.NET Programming: From Problem Analysis to Program Design8

9 Creating Linked Lists Node –Each instance in linked list contains Data Reference attribute linking it to next node Maintain references to first and last item in list –To facilitate access

Visual Basic.NET Programming: From Problem Analysis to Program Design10 Creating Linked Lists (continued) Self-referencing class –Class that references instances of itself

Visual Basic.NET Programming: From Problem Analysis to Program Design11

Visual Basic.NET Programming: From Problem Analysis to Program Design12

Visual Basic.NET Programming: From Problem Analysis to Program Design13

Visual Basic.NET Programming: From Problem Analysis to Program Design14

Visual Basic.NET Programming: From Problem Analysis to Program Design15 Creating a Stack Stack can be implemented as LinkedList –LIFO structure: Add and remove nodes from only beginning of Stack –Employ inheritance to create Stack class As subclass of LinkedList

Visual Basic.NET Programming: From Problem Analysis to Program Design16

Visual Basic.NET Programming: From Problem Analysis to Program Design17 Exploring the FCL Stack Class Stores nodes as instances of Object Methods: –Push –Pop –Peek –ToArray

Visual Basic.NET Programming: From Problem Analysis to Program Design18 Creating a Queue Queue can be implemented as linked list Objects added only at end Removed only from beginning –Employing FIFO logic Enqueue –Add object Dequeue –Remove object

Visual Basic.NET Programming: From Problem Analysis to Program Design19

Visual Basic.NET Programming: From Problem Analysis to Program Design20 Creating a Queue (continued) Placing common attributes and methods in superclass eliminates redundancy in subclasses –Count and IsEmpty added to superclass

Visual Basic.NET Programming: From Problem Analysis to Program Design21 Exploring the FCL Queue class Queue class –Stores nodes as instances of Object Methods: –Enqueue –Dequeue –Peek –ToArray

Visual Basic.NET Programming: From Problem Analysis to Program Design22 Programming Example: Reservation Application Implements reservation system Reservation class methods: –MakeReservation –CancelReservation –ListReservations

Visual Basic.NET Programming: From Problem Analysis to Program Design23

Visual Basic.NET Programming: From Problem Analysis to Program Design24 Understanding Recursion Programming technique in which method calls itself For some problems recursive approach leads to simple and elegant solution Recursive problem-solving approach –Divides complex problem into successively smaller but similar subproblems –Solves each subproblem –Assembles result

Visual Basic.NET Programming: From Problem Analysis to Program Design25 Example 15-8: Using a Recursive Method to Simulate Opening Boxes Module OpenBoxesRecursively Sub Main() 'open largest box (size 10) OpenBox(10) System.Console.WriteLine(”Done”) End Sub

Visual Basic.NET Programming: From Problem Analysis to Program Design26 Example 15-8: Using a Recursive Method to Simulate Opening Boxes (continued) Sub OpenBox(ByVal boxSize As Integer) If boxSize = 0 Then System.Console.WriteLine(”No more boxes”) Else System.Console.WriteLine(”Box “ & boxSize & _ “ opened”) OpenBox(boxSize - 1) End If

Visual Basic.NET Programming: From Problem Analysis to Program Design27

Visual Basic.NET Programming: From Problem Analysis to Program Design28 (Continued)

Visual Basic.NET Programming: From Problem Analysis to Program Design29 Example 15-8: Using a Recursive Method to Simulate Opening Boxes (continued) Recursive algorithm must have stopping point –Called base case –When encountered, recursion stops

Visual Basic.NET Programming: From Problem Analysis to Program Design30 Example 15-8: Using a Recursive Method to Simulate Opening Boxes (continued) Each time recursive method calls itself –Fresh copy of the method created –Each copy has own set of values and parameters –Remains unfinished until base case is recognized

Visual Basic.NET Programming: From Problem Analysis to Program Design31 Example 15-8: Using a Recursive Method to Simulate Opening Boxes (continued) Upon each return –Execution resumes within that copy at line of code immediately following recursive call Recursion is similar to iteration –Any problem that can be solved iteratively can also be solved recursively, and vice versa –Recursive solutions are less efficient

Visual Basic.NET Programming: From Problem Analysis to Program Design32 Writing Recursive Methods Write recursive method: –Must think recursively –Think of problem in terms of series of successively smaller but virtually identical subproblems –Smallest subproblem must be one that can be solved directly

Visual Basic.NET Programming: From Problem Analysis to Program Design33 Computing Factorials Classic example of recursion involves computing factorials Definition: –n! = n * (n – 1) * (n – 2) *... * 1, where n is a positive integer –0! = 1 Thinking recursively: –4! can be defined as 4 * 3!

Visual Basic.NET Programming: From Problem Analysis to Program Design34 Computing Factorials (continued) Calculating factorials can produce a number that is too large to be represented in memory space allocated to a particular variable –12! is largest factorial that can be computed using Integer data type to hold result

Visual Basic.NET Programming: From Problem Analysis to Program Design35

Visual Basic.NET Programming: From Problem Analysis to Program Design36

Visual Basic.NET Programming: From Problem Analysis to Program Design37

Visual Basic.NET Programming: From Problem Analysis to Program Design38 (Continued)

Visual Basic.NET Programming: From Problem Analysis to Program Design39 Displaying a Directory Tree Generate and display tree containing directories and subdirectories on hard drive Depending on size of your hard drive and speed of your computer –Initially creating directory tree may take a few minutes

Visual Basic.NET Programming: From Problem Analysis to Program Design40

Visual Basic.NET Programming: From Problem Analysis to Program Design41 Sorting an Array Recursive methods are frequently used for array processing Quicksort –Recursive technique for sorting one-dimensional array –Begins by selecting array element to serve as pivot

Visual Basic.NET Programming: From Problem Analysis to Program Design42 Sorting an Array (continued) Quicksort (continued) –Position pivot such that All array elements to right are greater than or equal to pivot All array elements to left are less than or equal to pivot –Elements to right and left of pivot form subarrays Sort them using Quicksort

Visual Basic.NET Programming: From Problem Analysis to Program Design43 Programming Example: Towers of Hanoi Classic computer science problem Priests in Far Eastern temple were given task of moving stack of 64 disks from one pole to another Each disk was slightly different size –Disks were arranged on pole from top to bottom in order of increasing size –Intermediate pole also available to hold disks

Visual Basic.NET Programming: From Problem Analysis to Program Design44 Programming Example: Towers of Hanoi (continued) Priests were to move all 64 disks to third pole using the following rules: –Only one disk can be moved at a time –Removed disk must be placed on one of other poles –Under no circumstance may larger disk be placed upon smaller disk Priests were told that world would end when they completed their task

Visual Basic.NET Programming: From Problem Analysis to Program Design45

Visual Basic.NET Programming: From Problem Analysis to Program Design46

Visual Basic.NET Programming: From Problem Analysis to Program Design47 Programming Example: Towers of Hanoi (continued) Recursive algorithm: –Move top n-1 disks from pole 1 to pole 2, using pole 3 to temporarily hold disks –Move bottom (nth) disk from pole 1 to pole 3 –Move n-1 disks on pole 2 to pole 3, using pole 1 to temporarily hold disks Number of moves required to move the disks is

Visual Basic.NET Programming: From Problem Analysis to Program Design48 Summary Linked list –List of object instances that are linked together –Each instance is called a node Stack –Can be implemented as special form of LinkedList –LIFO structure –FCL provides Stack class

Visual Basic.NET Programming: From Problem Analysis to Program Design49 Summary (continued) Queue –Implemented as linked list with objects added at end and removed from beginning –Employ FIFO logic –FCL includes Queue class Recursive problem-solving approach –Divides problem into series of smaller problems –Solves each subproblem –Assembles result